generated from john/python-template
Compare commits
128
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be3d0b7ee0 | ||
|
|
7eca9fe7dc | ||
|
|
e54c2d9f26 | ||
|
|
065acad125 | ||
|
|
eeb1888aa3 | ||
|
|
321c454a4f | ||
|
|
6f4accf275 | ||
|
|
27ec81ca5b | ||
|
|
4410d23f5c | ||
|
|
d5e798825d | ||
|
|
02dca888d2 | ||
|
|
a896a11d2e | ||
|
|
0b48c80d87 | ||
|
|
70f8d6182e | ||
|
|
fc4288ff88 | ||
|
|
e5ef4d4422 | ||
|
|
88cef169c4 | ||
|
|
15a4814e23 | ||
|
|
16391463d6 | ||
|
|
96bb80d91f | ||
|
|
494f378e48 | ||
|
|
75dc946123 | ||
|
|
929f8d6de9 | ||
|
|
72909c1fd5 | ||
|
|
a5ec9f40a8 | ||
|
|
0d5206fe97 | ||
|
|
32b6b5f29a | ||
|
|
9b6bb9ae66 | ||
|
|
4c877dd6a2 | ||
|
|
9990583345 | ||
|
|
daa1642933 | ||
|
|
d198cc0c68 | ||
|
|
34c7b16675 | ||
|
|
ca29bc8b74 | ||
|
|
89ed83239e | ||
|
|
2c6ef46f5f | ||
|
|
c2ed98c16d | ||
|
|
c85cc6be20 | ||
|
|
234476ba6d | ||
|
|
faa30fd27b | ||
|
|
867cc9eb78 | ||
|
|
0e43094b80 | ||
|
|
c05c054b94 | ||
|
|
c0112c2714 | ||
|
|
96af7b2dc0 | ||
|
|
9a7970c533 | ||
|
|
f15c9834e4 | ||
|
|
2c59cbd2c7 | ||
|
|
5ff66a8c40 | ||
|
|
626b5d4b10 | ||
|
|
12f125761a | ||
|
|
803237371e | ||
|
|
d4ae97c1b1 | ||
|
|
c52d41ec33 | ||
|
|
6c3eac0a44 | ||
|
|
e5410708e4 | ||
|
|
efbae26f16 | ||
|
|
3873810022 | ||
|
|
2093eb6fb3 | ||
|
|
f9261a1af3 | ||
|
|
86cdb4035c | ||
|
|
f193b2800b | ||
|
|
736d0c06f4 | ||
|
|
26f9c83f54 | ||
|
|
a2bb1acd6b | ||
|
|
2a56365847 | ||
|
|
4aaa9bd581 | ||
|
|
de18c2e9da | ||
|
|
8d3c60fce1 | ||
|
|
8a30231adf | ||
|
|
67feeb28af | ||
|
|
c6ed3126e0 | ||
|
|
5566f48fc0 | ||
|
|
ed6998d8da | ||
|
|
ebf659b26c | ||
|
|
ae3483ec2e | ||
|
|
141ee1fa85 | ||
|
|
0f30d902b9 | ||
|
|
86b8e83ff4 | ||
|
|
efe7785392 | ||
|
|
94db493756 | ||
|
|
0d554c0648 | ||
|
|
63c21d4a14 | ||
|
|
cf49c3c127 | ||
|
|
bf2f3ac09c | ||
|
|
eaeb0bc806 | ||
|
|
8b08478c9d | ||
|
|
450d33d507 | ||
|
|
796216087c | ||
|
|
afd1dba4d4 | ||
|
|
7daa0b9808 | ||
|
|
7c4300f9c2 | ||
|
|
443a1e29c8 | ||
|
|
cdd846fe29 | ||
|
|
30fcef3892 | ||
|
|
de8cdb6e1a | ||
|
|
b6a5a89a84 | ||
|
|
c261fbb3bd | ||
|
|
5404224079 | ||
|
|
2c26177d0c | ||
|
|
edcfba9cb2 | ||
|
|
fca959fa5d | ||
|
|
110f40a28b | ||
|
|
7dd0d2c9bf | ||
|
|
11097b9cfe | ||
|
|
7285a87dfb | ||
|
|
f86c0ff27b | ||
|
|
246d7f9434 | ||
|
|
22d47574f2 | ||
|
|
4488280097 | ||
|
|
012dc15042 | ||
|
|
66e2dce465 | ||
|
|
597be2691c | ||
|
|
4e8c562f92 | ||
|
|
0b63b53f53 | ||
|
|
6a3ee26733 | ||
|
|
97b3d0fd62 | ||
|
|
7b9715b3f1 | ||
|
|
3e418a0889 | ||
|
|
2ccea77520 | ||
|
|
b3d8eb6e97 | ||
|
|
1ee9ebbffc | ||
|
|
aec89b3a7a | ||
|
|
d1321fd709 | ||
|
|
7054cd8af9 | ||
|
|
bdb1b31b0a | ||
|
|
5b97c759fe | ||
|
|
7db4df1729 |
@@ -1,54 +0,0 @@
|
|||||||
# --- NiceGUI Server ---
|
|
||||||
# HOST=`0.0.0.0` (default)
|
|
||||||
# PORT=8000 (default)
|
|
||||||
# LOG_LEVEL: [`critical`, `error`, `warning`, `info` (default), `debug`, `trace`]
|
|
||||||
# RELOAD=false (default)
|
|
||||||
|
|
||||||
# --- AI provider ---
|
|
||||||
# PROVIDER=[`openrouter`(default), `google_genai`]
|
|
||||||
PROVIDER=openrouter
|
|
||||||
# OPENROUTER_API_KEY - Required when `PROVIDER=openrouter`
|
|
||||||
OPENROUTER_API_KEY=your-api-key-goes-here
|
|
||||||
# GEMINI_API_KEY - Required when `PROVIDER=google_genai`
|
|
||||||
# PROVIDER_MODEL= specify model. If left blank OpenRouter will supply default.
|
|
||||||
PROVIDER_MODEL=google/gemini-2.5-flash
|
|
||||||
# OPENROUTER_HTTP_REFERER=https://example.com
|
|
||||||
# OPENROUTER_APP_TITLE="Google: Gemini 2.5 Flash (openrouter)"
|
|
||||||
|
|
||||||
# --- runtime environment ---
|
|
||||||
# ENVIRONMENT: [`development`(default), `test`, `production`]
|
|
||||||
|
|
||||||
# --- persistence ---
|
|
||||||
# Use nested settings with double underscore because env_nested_delimiter="__".
|
|
||||||
# SQLite example:
|
|
||||||
# DATABASE__DRIVER=sqlite
|
|
||||||
# DATABASE__PATH=app.db
|
|
||||||
#
|
|
||||||
# SQLite with custom relative path:
|
|
||||||
# DATABASE__DRIVER=sqlite
|
|
||||||
DATABASE__PATH=./data/transcription.db
|
|
||||||
#
|
|
||||||
# Postgres example:
|
|
||||||
# DATABASE__DRIVER=postgres
|
|
||||||
# DATABASE__HOST=localhost
|
|
||||||
# DATABASE__PORT=5432
|
|
||||||
# DATABASE__DATABASE=transcription
|
|
||||||
# DATABASE__USER=postgres
|
|
||||||
# DATABASE__PASSWORD=change-me
|
|
||||||
#
|
|
||||||
# Optional persistence flags:
|
|
||||||
# BOOTSTRAP_SCHEMA_ON_STARTUP=false
|
|
||||||
# SQLITE_CHECK_SAME_THREAD=false
|
|
||||||
|
|
||||||
# --- filesystem paths ---
|
|
||||||
UPLOAD_DIR="./data"
|
|
||||||
PROMPT_DIR="./prompts"
|
|
||||||
|
|
||||||
# --- worker reliability ---
|
|
||||||
WORKER_MAX_RETRIES=0
|
|
||||||
WORKER_RETRY_BACKOFF_SECONDS=0
|
|
||||||
# WORKER_PROVIDER_TIMEOUT_SECONDS=[0-20]
|
|
||||||
WORKER_PROVIDER_TIMEOUT_SECONDS=20
|
|
||||||
WORKER_MIN_TRANSCRIPTION_CHARS=0
|
|
||||||
WORKER_MIN_TRANSCRIPTION_LINES=0
|
|
||||||
WORKER_FAIL_ON_FINISH_REASON_LENGTH=false
|
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
# Production environment example for docker-compose.production.yml
|
||||||
|
|
||||||
|
# --- NiceGUI Server ---
|
||||||
|
HOST=0.0.0.0
|
||||||
|
PORT=8000
|
||||||
|
LOG_LEVEL=info
|
||||||
|
RELOAD=false
|
||||||
|
ENVIRONMENT=production
|
||||||
|
# TRANSCRIPTION_COMMIT=
|
||||||
|
RUN_EMBEDDED_WORKER=false
|
||||||
|
LOG_DIR=/app/data/logs
|
||||||
|
LOG_FILE_NAME=transcription.log
|
||||||
|
LOG_FILE_MAX_BYTES=10485760
|
||||||
|
LOG_FILE_BACKUP_COUNT=5
|
||||||
|
|
||||||
|
# --- AI provider ---
|
||||||
|
PROVIDER=openrouter
|
||||||
|
OPENROUTER_API_KEY=replace-with-real-key
|
||||||
|
PROVIDER_MODEL=google/gemini-2.5-flash
|
||||||
|
# PROVIDER_MODELS=["google/gemini-2.5-flash","anthropic/claude-sonnet-4"]
|
||||||
|
# OPENROUTER_HTTP_REFERER=
|
||||||
|
# OPENROUTER_APP_TITLE=
|
||||||
|
DEFAULT_PROMPT_NAME=transcribe_document.md
|
||||||
|
# TRANSCRIPTION_TEMPERATURE=
|
||||||
|
# TRANSCRIPTION_TOP_P=
|
||||||
|
|
||||||
|
# --- persistence ---
|
||||||
|
# Common database settings:
|
||||||
|
DATABASE__DRIVER=postgres
|
||||||
|
DATABASE__DATABASE=transcription
|
||||||
|
DATABASE__USER=transcription
|
||||||
|
DATABASE__PASSWORD=replace-with-strong-password
|
||||||
|
BOOTSTRAP_SCHEMA_ON_STARTUP=false
|
||||||
|
|
||||||
|
# SQLite-specific settings:
|
||||||
|
# DATABASE__PATH=./data/transcription.db
|
||||||
|
# SQLITE_CHECK_SAME_THREAD=false
|
||||||
|
|
||||||
|
# Postgres-specific settings:
|
||||||
|
DATABASE__HOST=postgres
|
||||||
|
DATABASE__PORT=5432
|
||||||
|
|
||||||
|
# --- filesystem paths ---
|
||||||
|
UPLOAD_DIR=/app/uploads
|
||||||
|
PROMPT_DIR=/app/prompts
|
||||||
|
# --- backup workflow helpers (not Runtime Settings model fields) ---
|
||||||
|
BACKUP_DIR=/backup
|
||||||
|
BACKUP_RETENTION_DAYS=14
|
||||||
|
|
||||||
|
# --- worker reliability ---
|
||||||
|
WORKER_MAX_RETRIES=0
|
||||||
|
WORKER_PROVIDER_TIMEOUT_SECONDS=30.0
|
||||||
|
WORKER_STALE_JOB_SECONDS=90.0
|
||||||
|
WORKER_RETRY_BACKOFF_SECONDS=1.0
|
||||||
|
WORKER_SHUTDOWN_GRACE_SECONDS=5.0
|
||||||
|
WORKER_POLL_INTERVAL_SECONDS=1.0
|
||||||
|
WORKER_MIN_TRANSCRIPTION_CHARS=0
|
||||||
|
WORKER_MIN_TRANSCRIPTION_LINES=0
|
||||||
|
WORKER_FAIL_ON_FINISH_REASON_LENGTH=false
|
||||||
|
|
||||||
|
# --- cloudflare tunnel ---
|
||||||
|
# Required for token-based tunnel startup.
|
||||||
|
CLOUDFLARE_TUNNEL_TOKEN=replace-with-cloudflare-tunnel-token
|
||||||
|
|
||||||
|
# --- deployment wiring helpers ---
|
||||||
|
# Runtime Settings writes target this file path inside the app container.
|
||||||
|
RUNTIME_SETTINGS_ENV_FILE=/app/.env.production
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
# Production environment
|
||||||
|
|
||||||
|
# --- NiceGUI Server ---
|
||||||
|
HOST=0.0.0.0
|
||||||
|
PORT=8000
|
||||||
|
LOG_LEVEL=info
|
||||||
|
RELOAD=false
|
||||||
|
ENVIRONMENT=production
|
||||||
|
# TRANSCRIPTION_COMMIT=
|
||||||
|
RUN_EMBEDDED_WORKER=false
|
||||||
|
LOG_DIR=/app/data/logs
|
||||||
|
LOG_FILE_NAME=transcription.log
|
||||||
|
LOG_FILE_MAX_BYTES=10485760
|
||||||
|
LOG_FILE_BACKUP_COUNT=5
|
||||||
|
|
||||||
|
# --- AI provider ---
|
||||||
|
PROVIDER=openrouter
|
||||||
|
OPENROUTER_API_KEY=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
|
||||||
|
PROVIDER_MODEL=google/gemini-2.5-flash
|
||||||
|
PROVIDER_MODELS=["google/gemini-2.5-pro","google/gemini-2.5-flash","anthropic/claude-opus-5","anthropic/claude-sonnet-4","openai/gpt-5.6","openai/gpt-4o"]
|
||||||
|
# OPENROUTER_HTTP_REFERER=
|
||||||
|
# OPENROUTER_APP_TITLE=
|
||||||
|
DEFAULT_PROMPT_NAME=transcribe_document.md
|
||||||
|
# TRANSCRIPTION_TEMPERATURE=
|
||||||
|
# TRANSCRIPTION_TOP_P=
|
||||||
|
|
||||||
|
# --- persistence ---
|
||||||
|
# Common database settings:
|
||||||
|
DATABASE__DRIVER=postgres
|
||||||
|
DATABASE__DATABASE=transcription
|
||||||
|
DATABASE__USER=transcription
|
||||||
|
DATABASE__PASSWORD=<password>
|
||||||
|
BOOTSTRAP_SCHEMA_ON_STARTUP=false
|
||||||
|
|
||||||
|
# SQLite-specific settings:
|
||||||
|
# DATABASE__PATH=./data/transcription.db
|
||||||
|
# SQLITE_CHECK_SAME_THREAD=false
|
||||||
|
|
||||||
|
# Postgres-specific settings:
|
||||||
|
DATABASE__HOST=postgres
|
||||||
|
DATABASE__PORT=5432
|
||||||
|
|
||||||
|
# --- filesystem paths ---
|
||||||
|
UPLOAD_DIR=/app/uploads
|
||||||
|
PROMPT_DIR=/app/prompts
|
||||||
|
# --- backup workflow helpers (not Runtime Settings model fields) ---
|
||||||
|
BACKUP_DIR=/backup
|
||||||
|
BACKUP_RETENTION_DAYS=14
|
||||||
|
|
||||||
|
# --- worker reliability ---
|
||||||
|
WORKER_MAX_RETRIES=0
|
||||||
|
WORKER_PROVIDER_TIMEOUT_SECONDS=30.0
|
||||||
|
WORKER_STALE_JOB_SECONDS=30.0
|
||||||
|
WORKER_RETRY_BACKOFF_SECONDS=1.0
|
||||||
|
WORKER_SHUTDOWN_GRACE_SECONDS=5.0
|
||||||
|
WORKER_POLL_INTERVAL_SECONDS=1.0
|
||||||
|
WORKER_MIN_TRANSCRIPTION_CHARS=0
|
||||||
|
WORKER_MIN_TRANSCRIPTION_LINES=0
|
||||||
|
WORKER_FAIL_ON_FINISH_REASON_LENGTH=false
|
||||||
|
|
||||||
|
# --- cloudflare tunnel ---
|
||||||
|
# Required for token-based tunnel startup.
|
||||||
|
CLOUDFLARE_TUNNEL_TOKEN=CLOUDFLARE_TUNNEL_TOKEN=eyJhIjoiYTRhNjM0NzNhNzBiZjhhYmY3OWUyNjE4ZTcyNjgwZmMiLCJ0IjoiZWY1MjFkNWItYzY1ZS00ZGFmLTlmYTMtMzQyOGYzMGUyMDY4IiwicyI6IlpHWm1NVGRsTVRVdFpEYzNaaTAwWkRJeUxXRmhPRFV0TmpKallXRmhPRFJrWXpSaSJ9
|
||||||
|
|
||||||
|
# --- deployment wiring helpers ---
|
||||||
|
# Runtime Settings writes target this file path inside the app container.
|
||||||
|
RUNTIME_SETTINGS_ENV_FILE=/app/.env.production
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
# Production environment
|
||||||
|
|
||||||
|
# --- NiceGUI Server ---
|
||||||
|
HOST=0.0.0.0
|
||||||
|
PORT=8000
|
||||||
|
LOG_LEVEL=info
|
||||||
|
RELOAD=false
|
||||||
|
ENVIRONMENT=production
|
||||||
|
# TRANSCRIPTION_COMMIT=
|
||||||
|
RUN_EMBEDDED_WORKER=false
|
||||||
|
LOG_DIR=/app/data/logs
|
||||||
|
LOG_FILE_NAME=transcription.log
|
||||||
|
LOG_FILE_MAX_BYTES=10485760
|
||||||
|
LOG_FILE_BACKUP_COUNT=5
|
||||||
|
|
||||||
|
# --- AI provider ---
|
||||||
|
PROVIDER=openrouter
|
||||||
|
OPENROUTER_API_KEY=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
|
||||||
|
PROVIDER_MODEL=google/gemini-2.5-flash
|
||||||
|
PROVIDER_MODELS=["google/gemini-2.5-pro","google/gemini-2.5-flash","anthropic/claude-opus-5","anthropic/claude-sonnet-4","openai/gpt-5.6","openai/gpt-4o"]
|
||||||
|
# OPENROUTER_HTTP_REFERER=
|
||||||
|
# OPENROUTER_APP_TITLE=
|
||||||
|
DEFAULT_PROMPT_NAME=transcribe_document.md
|
||||||
|
# TRANSCRIPTION_TEMPERATURE=
|
||||||
|
# TRANSCRIPTION_TOP_P=
|
||||||
|
|
||||||
|
# --- persistence ---
|
||||||
|
# Common database settings:
|
||||||
|
DATABASE__DRIVER=sqlite
|
||||||
|
# DATABASE__DATABASE=transcription
|
||||||
|
# DATABASE__USER=transcription-local
|
||||||
|
# DATABASE__PASSWORD=My!3sons
|
||||||
|
# BOOTSTRAP_SCHEMA_ON_STARTUP=false
|
||||||
|
|
||||||
|
# SQLite-specific settings:
|
||||||
|
DATABASE__PATH=./data-local/transcription-local.db
|
||||||
|
SQLITE_CHECK_SAME_THREAD=false
|
||||||
|
|
||||||
|
# Postgres-specific settings:
|
||||||
|
# DATABASE__HOST=postgres
|
||||||
|
# DATABASE__PORT=5432
|
||||||
|
|
||||||
|
# --- filesystem paths ---
|
||||||
|
UPLOAD_DIR=./data-local
|
||||||
|
PROMPT_DIR=/data/prompts
|
||||||
|
# --- backup workflow helpers (not Runtime Settings model fields) ---
|
||||||
|
BACKUP_DIR=/backup
|
||||||
|
BACKUP_RETENTION_DAYS=14
|
||||||
|
|
||||||
|
# --- worker reliability ---
|
||||||
|
WORKER_MAX_RETRIES=0
|
||||||
|
WORKER_PROVIDER_TIMEOUT_SECONDS=30.0
|
||||||
|
WORKER_STALE_JOB_SECONDS=30.0
|
||||||
|
WORKER_RETRY_BACKOFF_SECONDS=1.0
|
||||||
|
WORKER_SHUTDOWN_GRACE_SECONDS=5.0
|
||||||
|
WORKER_POLL_INTERVAL_SECONDS=1.0
|
||||||
|
WORKER_MIN_TRANSCRIPTION_CHARS=0
|
||||||
|
WORKER_MIN_TRANSCRIPTION_LINES=0
|
||||||
|
WORKER_FAIL_ON_FINISH_REASON_LENGTH=false
|
||||||
|
|
||||||
|
# --- cloudflare tunnel ---
|
||||||
|
# Required for token-based tunnel startup.
|
||||||
|
CLOUDFLARE_TUNNEL_TOKEN=CLOUDFLARE_TUNNEL_TOKEN=eyJhIjoiYTRhNjM0NzNhNzBiZjhhYmY3OWUyNjE4ZTcyNjgwZmMiLCJ0IjoiZWY1MjFkNWItYzY1ZS00ZGFmLTlmYTMtMzQyOGYzMGUyMDY4IiwicyI6IlpHWm1NVGRsTVRVdFpEYzNaaTAwWkRJeUxXRmhPRFV0TmpKallXRmhPRFJrWXpSaSJ9
|
||||||
|
|
||||||
|
# --- deployment wiring helpers ---
|
||||||
|
# Runtime Settings writes target this file path inside the app container.
|
||||||
|
RUNTIME_SETTINGS_ENV_FILE=/app/.env.production
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
---
|
||||||
|
name: Python Architect Reviewer
|
||||||
|
description: Evidence-based senior architect reviewer for FastAPI, NiceGUI, and SQLModel codebases.
|
||||||
|
skills:
|
||||||
|
- python-code-reviewer
|
||||||
|
---
|
||||||
|
|
||||||
|
# Python Architect Reviewer
|
||||||
|
|
||||||
|
You are a Senior Python Architect performing an evidence-based, read-only code review.
|
||||||
|
|
||||||
|
> No `tools:` allowlist is declared here on purpose. Tool identifiers differ between the runtimes
|
||||||
|
> this agent is invoked from, so a hard-coded list silently under-tools the agent in one of them.
|
||||||
|
> Read-only discipline is enforced by the **Read-Only Scope** rule below, not by the frontmatter.
|
||||||
|
|
||||||
|
## Operating Principles
|
||||||
|
|
||||||
|
- **Stack Context:** Python 3.12+, FastAPI, NiceGUI, SQLModel, SQLAlchemy (SQLite/PostgreSQL), Pydantic V2, asyncio workers, and OpenRouter adapters.
|
||||||
|
- **Evidence-Based:** Always inspect real files. Every finding must reference concrete file paths and line numbers (e.g., `app/services/worker.py:45-78`). Do not speculate.
|
||||||
|
- **Tool Verification:** This is a `uv` project; the toolchain is not on `PATH`. Verify with `uv run ruff check .`, `uv run ty check`, and `uv run pytest -q -m "not external"`, and record the exact commands and outcomes. Never report a lint, type, or test claim you did not run.
|
||||||
|
- **Verify Recommendations, Not Just Findings:** Before recommending a change to a shared symbol, enumerate its consumers and confirm the fix is safe for each. See the skill's consumer-tracing step and `Blast Radius` field.
|
||||||
|
- **Skill Is Canonical:** The `python-code-reviewer` skill defines the review workflow, deterministic checks, severity and reachability rubrics, report location, and report template. Follow it exactly. Where this file and the skill disagree, the skill wins — do not restate its specifics here.
|
||||||
|
- **Read-Only Scope:** Do not modify source, tests, docs, instructions, or configuration. The review report is the only artifact you produce.
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
---
|
||||||
|
description: Require documentation updates whenever code changes alter contracts, behavior, or scope.
|
||||||
|
applyTo: 'src/transcription/**/*.py'
|
||||||
|
---
|
||||||
|
|
||||||
|
# Documentation Sync Requirements
|
||||||
|
|
||||||
|
Keep docs in sync in the same change whenever implementation alters a documented contract, behavior, or roadmap decision.
|
||||||
|
|
||||||
|
Documentation targets below always refer to the **current** baseline. `docs/index.md` states which
|
||||||
|
baseline that is; resolve any version-specific document from there. Never cite a superseded version
|
||||||
|
tree by name in this file or in the docs you update — retired revision trees are not authority, and
|
||||||
|
`tests/test_meta_contract_guards.py` fails active contract files that route authority through them.
|
||||||
|
|
||||||
|
## Update documentation when any of these change
|
||||||
|
|
||||||
|
1. **Schema/Data contract**
|
||||||
|
- Models, fields, enums, constraints, indexes, relationships, loading semantics.
|
||||||
|
- **Required doc update:** `docs/schema.md`.
|
||||||
|
|
||||||
|
2. **Configuration contract**
|
||||||
|
- `Settings` keys, defaults, required/optional environment values.
|
||||||
|
- **Required doc update:** `.env.production.example` and any directly related setup docs.
|
||||||
|
|
||||||
|
3. **User-visible UI behavior**
|
||||||
|
- Page flow, routes, button/action behavior, labels, status wording, empty/error states.
|
||||||
|
- **Required doc update:** relevant `docs/ui/pages/*.md` docs and feature docs when applicable.
|
||||||
|
|
||||||
|
4. **Error handling semantics**
|
||||||
|
- Error categories, retry behavior, envelope structure, translation boundaries.
|
||||||
|
- **Required doc update:** `docs/error_handling.md` and `docs/invariant/error_handling.md`.
|
||||||
|
|
||||||
|
5. **Roadmap/scope decisions**
|
||||||
|
- Version targets, sequencing, deferrals, and accepted alternatives.
|
||||||
|
- **Required doc update:** `docs/roadmap_plan.md`, plus any backlog or feature document for the
|
||||||
|
current baseline. Locate it through `docs/index.md` rather than assuming a version-named path.
|
||||||
|
|
||||||
|
## Working rule
|
||||||
|
|
||||||
|
If none of the categories above changed, documentation edits are optional.
|
||||||
|
If any category changed, update docs in the same PR/change set rather than deferring.
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
---
|
||||||
|
description: Cross-cutting error handling rules for services, API, and UI.
|
||||||
|
applyTo: 'src/transcription/**/*.py'
|
||||||
|
---
|
||||||
|
|
||||||
|
# Error Handling (Cross-cutting)
|
||||||
|
|
||||||
|
Primary references:
|
||||||
|
|
||||||
|
- `docs/error_handling.md`
|
||||||
|
- `docs/invariant/error_handling.md`
|
||||||
|
- `docs/requirements.md`
|
||||||
|
|
||||||
|
## Taxonomy and Categories
|
||||||
|
|
||||||
|
Use category-driven semantics aligned to canonical policy:
|
||||||
|
|
||||||
|
- `validation`
|
||||||
|
- `not_found`
|
||||||
|
- `conflict`
|
||||||
|
- `external`
|
||||||
|
- `timeout`
|
||||||
|
- `internal`
|
||||||
|
|
||||||
|
Do not invent ad hoc categories in user/API-facing envelopes unless canonical docs are updated.
|
||||||
|
|
||||||
|
Runtime/internal categories may be more specific for diagnostics and persistence, but they must map
|
||||||
|
deterministically to the canonical envelope categories through the centralized mapper in
|
||||||
|
`transcription.errors.canonical_error_category`.
|
||||||
|
|
||||||
|
Current internal categories:
|
||||||
|
|
||||||
|
- `validation_error`
|
||||||
|
- `user_input_error`
|
||||||
|
- `not_found_error`
|
||||||
|
- `conflict_error`
|
||||||
|
- `external_provider_error`
|
||||||
|
- `external_timeout_error`
|
||||||
|
- `processing_error`
|
||||||
|
- `infrastructure_transient_error`
|
||||||
|
- `infrastructure_persistent_error`
|
||||||
|
- `internal_unexpected_error`
|
||||||
|
|
||||||
|
Required internal -> canonical mapping:
|
||||||
|
|
||||||
|
- `validation_error`, `user_input_error` -> `validation`
|
||||||
|
- `not_found_error` -> `not_found`
|
||||||
|
- `conflict_error` -> `conflict`
|
||||||
|
- `external_provider_error` -> `external`
|
||||||
|
- `external_timeout_error`, `infrastructure_transient_error` -> `timeout`
|
||||||
|
- `processing_error`, `infrastructure_persistent_error`, `internal_unexpected_error` -> `internal`
|
||||||
|
|
||||||
|
## Translation Boundaries
|
||||||
|
|
||||||
|
- **Provider/adapters:** raise provider/domain exceptions; do not emit UI text.
|
||||||
|
- **Services:** map raw exceptions into internal categories and preserve causal chain (`raise ... from ...`).
|
||||||
|
- **UI/API:** map internal category -> canonical envelope category and emit user-safe, actionable messages.
|
||||||
|
|
||||||
|
## Retry Rules
|
||||||
|
|
||||||
|
- No auto-retry for `validation`, `not_found`, `conflict`.
|
||||||
|
- `external`/`timeout` may be retried when operation semantics are safe.
|
||||||
|
- Preserve each retry as new evidence where applicable (no history rewrite).
|
||||||
|
|
||||||
|
## Job/Page Failure Semantics
|
||||||
|
|
||||||
|
- Page-level (`JobSource`): `pending`, `transcribed`, `failed`, `cancelled`.
|
||||||
|
- Job terminals: `transcribed`, `partial_success`, `failed`.
|
||||||
|
- Cancellation must keep job-level and page-level semantics explicit and consistent.
|
||||||
|
- Do not emit legacy terminal state language such as `completed` in active user/API lifecycle contracts.
|
||||||
|
|
||||||
|
## User-Safe Messaging
|
||||||
|
|
||||||
|
- Never leak stack traces, credentials, auth headers, or local filesystem paths in user-facing output.
|
||||||
|
- Include actionable remediation guidance aligned to category.
|
||||||
|
- Keep envelope structure consistent across API endpoints.
|
||||||
|
|
||||||
|
### `AppError.message` vs `AppError.detail`
|
||||||
|
|
||||||
|
`AppError` carries two texts with different audiences, and they must not be collapsed. Getting this
|
||||||
|
wrong has already caused a real defect in this repository, in both directions.
|
||||||
|
|
||||||
|
| Attribute | Audience | Reaches | Rule |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `message` | User and API clients | `ErrorEnvelope.message`, UI notifications | Stays generic. Never embed exception text, provider payloads, or filesystem paths. |
|
||||||
|
| `detail` | Internal only | Logs, and `format_error_detail` -> `ExecutionAttempt.error_detail` and `MaintenanceRun.error_detail` | Carries the root cause. Never rendered to users or serialized into an envelope without a sanitizing projection. |
|
||||||
|
|
||||||
|
- Putting root-cause data in `message` leaks infrastructure detail to users.
|
||||||
|
- Omitting it from `detail` silently degrades the provenance record this system exists to preserve —
|
||||||
|
a failed attempt whose `error_detail` says nothing is an attempt that cannot be diagnosed later.
|
||||||
|
- Any render boundary that displays persisted `error_detail` must apply the same no-local-path rule
|
||||||
|
as `message`: sanitize machine-local absolute paths before the text becomes user-visible.
|
||||||
|
- When you raise from a caught exception, populate **both**: a generic `message` and a `detail`
|
||||||
|
carrying `type(exc).__name__` and the exception text, with `raise ... from exc`.
|
||||||
|
- `detail` is optional (`None`). A read path that assumes it is populated must handle its absence.
|
||||||
|
- Before changing either attribute, or any helper that formats them, enumerate every consumer —
|
||||||
|
evidence writes, maintenance runs, logging, API envelopes, and UI presentation all read these
|
||||||
|
fields, and tests assert on the persisted text.
|
||||||
|
|
||||||
|
Canonical definitions live in `src/transcription/errors.py`; see also `docs/error_handling.md`.
|
||||||
|
|
||||||
|
## Logging and Diagnostics
|
||||||
|
|
||||||
|
- Log operation identifiers and error IDs where available.
|
||||||
|
- Preserve category + cause-chain context.
|
||||||
|
- Distinguish no-response timeout/network failures from returned provider error responses.
|
||||||
|
|
||||||
|
## Guardrails
|
||||||
|
|
||||||
|
- No broad catch-and-swallow patterns.
|
||||||
|
- No success-shaped fallback values after exceptions.
|
||||||
|
- Category mapping must remain deterministic and testable.
|
||||||
|
|
||||||
|
## Contract Sync Rule
|
||||||
|
|
||||||
|
If taxonomy, retries, or envelope semantics change:
|
||||||
|
|
||||||
|
1. Update canonical docs (`docs/error_handling.md`, and invariant docs if needed).
|
||||||
|
2. Update tests in the same change.
|
||||||
|
3. Update related instruction/skill references.
|
||||||
|
4. If change affects persisted status/category fields, update `docs/schema.md` when applicable.
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
---
|
||||||
|
description: Provider adapter rules for evidence capture, secret safety, and client lifecycle.
|
||||||
|
applyTo: 'src/transcription/providers/**/*.py'
|
||||||
|
---
|
||||||
|
|
||||||
|
# Provider Adapters
|
||||||
|
|
||||||
|
Primary references:
|
||||||
|
|
||||||
|
- `docs/invariant/ai_evidence_and_provenance.md` (canonical; provider adapters own provider-boundary evidence capture)
|
||||||
|
- `docs/architecture.md`
|
||||||
|
- `docs/schema.md`
|
||||||
|
|
||||||
|
The provider layer is where an external API becomes application data. It is also the only place
|
||||||
|
that can capture what actually crossed the wire — once a response reaches a service, the evidence
|
||||||
|
it did not preserve is gone permanently. Treat capture correctness as the primary job of this
|
||||||
|
layer and text extraction as secondary.
|
||||||
|
|
||||||
|
## Layer Boundary
|
||||||
|
|
||||||
|
- Adapters may depend on `transcription.config`, `transcription.providers.*`, the HTTP client, and
|
||||||
|
the provider SDK. They must not import `services`, `db`, `ui`, or `api`.
|
||||||
|
- Provider specifics — headers, model slugs, payload shapes, SDK types, error classes — stop here.
|
||||||
|
Callers receive `TranscriptionResult` and `ProviderError` subclasses only.
|
||||||
|
- Adapters raise provider/domain exceptions. They must not emit user-facing text, notifications,
|
||||||
|
or remediation wording; that translation belongs to services and UI. See
|
||||||
|
[error-handling instructions](./error-handling.instructions.md).
|
||||||
|
- Adapters do not persist. They return evidence; services decide what is written and when.
|
||||||
|
|
||||||
|
Enforced by `tests/test_provider_boundaries.py`.
|
||||||
|
|
||||||
|
## Contract Surface
|
||||||
|
|
||||||
|
- Every adapter satisfies the `TranscriptionProvider` protocol in `base.py`. Failed-call evidence is
|
||||||
|
returned through the caller-owned `ProviderCallEvidence` sink passed to `transcribe()`, so
|
||||||
|
evidence stays scoped to one invocation instead of living on mutable adapter instance state.
|
||||||
|
- `TranscriptionResult`, `RequestManifest`, and `TransportEvidence` are `extra="forbid"` and frozen.
|
||||||
|
Add a field to the contract rather than smuggling data through an untyped dict.
|
||||||
|
- Evidence contracts in `evidence.py` are versioned (`schema_name` + `schema_version`). A change to
|
||||||
|
the meaning or shape of a captured field requires a version bump, not a silent redefinition —
|
||||||
|
stored evidence must keep its original meaning.
|
||||||
|
|
||||||
|
## Transport Evidence
|
||||||
|
|
||||||
|
The rules below implement `docs/invariant/ai_evidence_and_provenance.md` §3.4-3.5. That document
|
||||||
|
wins if this file drifts from it.
|
||||||
|
|
||||||
|
- Capture the response body **at the HTTP boundary, before SDK parsing**, so fields the SDK does
|
||||||
|
not model are not lost. `_CapturingAsyncClient` exists for this; do not replace it with a
|
||||||
|
post-parse `model_dump()` and call the result transport evidence.
|
||||||
|
- Reset per-call capture state at the start of every call. Without it, a connection failure can
|
||||||
|
attach the *previous* call's response as evidence for this one. Guarded by
|
||||||
|
`tests/test_evidence_provenance.py::test_openrouter_does_not_reuse_prior_response_on_connection_failure`.
|
||||||
|
- Keep transport capture scoped to the call, not the adapter instance. Concurrent `transcribe()`
|
||||||
|
calls on one adapter must not be able to overwrite each other's response evidence.
|
||||||
|
- Handle the streamed-body case (`httpx.ResponseNotRead`) rather than assuming `response.content`
|
||||||
|
is always available.
|
||||||
|
- When no response arrives — timeout, DNS, connection reset — emit
|
||||||
|
`TransportEvidence(response_received=False)`. Absence of a response is itself evidence and must
|
||||||
|
be explicit, never an empty body or a missing record.
|
||||||
|
- Preserve safe response evidence for **unsuccessful** calls too, whenever a response was received.
|
||||||
|
- Never relabel an SDK snapshot or normalized metadata as transport evidence, and never backfill
|
||||||
|
it into an execution that predates capture.
|
||||||
|
|
||||||
|
## Secret Safety
|
||||||
|
|
||||||
|
- Persist response headers only through `filter_safe_response_headers` and the
|
||||||
|
`SAFE_RESPONSE_HEADERS` allowlist. Allowlist, never denylist: capture-then-redact is prohibited,
|
||||||
|
because an unknown header is unsafe by default.
|
||||||
|
- Adding a header to the allowlist is a deliberate evidence decision. Confirm it carries no
|
||||||
|
credential, cookie, or session material, and state why it is needed for correlation, content
|
||||||
|
interpretation, rate-limit diagnosis, or audit.
|
||||||
|
- API keys, `Authorization`, and cookies must never appear in a manifest, evidence record, log
|
||||||
|
line, or exception message.
|
||||||
|
- The request manifest references source content by identity (digest, size, media type, page).
|
||||||
|
Do not duplicate base64 source bytes into it — `_replace_embedded_media` exists for this.
|
||||||
|
|
||||||
|
## Execution Specification
|
||||||
|
|
||||||
|
The manifest must let a reader reconstruct what was asked, per invariant §3.3:
|
||||||
|
|
||||||
|
- Provider, requested model, full effective prompt text, and prompt digest.
|
||||||
|
- Every explicitly supplied parameter, and — separately — which optional parameters were
|
||||||
|
**omitted**. Omission is not the same as a null value or an assumed provider default; the
|
||||||
|
`optional_parameter_states` distinction between `omitted`, `null`, and `value` is deliberate.
|
||||||
|
- Timeout budget, retry policy, source reference, and `SoftwareContext` versions.
|
||||||
|
- Manifest digests use `canonical_json_bytes`. Do not hash a plain `json.dumps()`; key order and
|
||||||
|
separators must stay deterministic or digests become uncomparable.
|
||||||
|
|
||||||
|
## Client Lifecycle and Async Safety
|
||||||
|
|
||||||
|
- Reuse one pooled `AsyncClient` per adapter instance; do not construct a client per request.
|
||||||
|
- Accept an injected client so tests can drive the adapter without network access.
|
||||||
|
- Derive timeouts from `Settings` (`worker_provider_timeout_seconds`) rather than hard-coding, and
|
||||||
|
keep the client timeout aligned with the configured budget so the SDK cannot expire first and
|
||||||
|
hide the real failure.
|
||||||
|
- Implement `aclose()` and release pooled resources. An adapter that creates a client owns closing
|
||||||
|
it; one given a client must not close a caller-owned resource it did not create.
|
||||||
|
- Never block the event loop. Offload CPU-bound work (hashing large payloads, image encoding) with
|
||||||
|
`asyncio.to_thread`.
|
||||||
|
- Propagate `asyncio.CancelledError` untouched — do not convert cancellation into a provider error.
|
||||||
|
|
||||||
|
## Failure Handling
|
||||||
|
|
||||||
|
- Raise `ProviderAuthError` for authentication, `ProviderResponseError` for malformed or unusable
|
||||||
|
responses, and `ProviderError` otherwise.
|
||||||
|
- Always attach `request_manifest`, `transport_evidence`, and an accurate `failure_phase` to raised
|
||||||
|
errors. `failure_phase` must distinguish a received-but-failed response from a call that never
|
||||||
|
reached the provider.
|
||||||
|
- Validate responses with Pydantic rather than indexing into raw dicts.
|
||||||
|
- Invalid *optional* metadata (for example unparsable token counts) must not discard an otherwise
|
||||||
|
valid transcript. Degrade the metadata, not the result.
|
||||||
|
|
||||||
|
## Contract Sync Rule
|
||||||
|
|
||||||
|
If capture behavior, evidence schema, or the header allowlist changes:
|
||||||
|
|
||||||
|
1. Update `docs/invariant/ai_evidence_and_provenance.md` only if the durable preservation contract
|
||||||
|
itself is changing — that revision is deliberate and reviewed, not incidental.
|
||||||
|
2. Update `docs/schema.md` when persisted evidence fields change.
|
||||||
|
3. Update or add tests in the same change (`tests/providers/`, `tests/test_evidence_provenance.py`).
|
||||||
|
4. Bump the affected evidence `schema_version` when a field's meaning changes.
|
||||||
@@ -7,29 +7,135 @@ applyTo: 'src/transcription/services/*.py'
|
|||||||
|
|
||||||
## Structure
|
## Structure
|
||||||
|
|
||||||
- Project core data models defined in [models](../../src/transcription/models.py)
|
- Project core data models are defined in [models](../../src/transcription/db/models.py)
|
||||||
- 1 service class per data model
|
- One service class per **aggregate**, not per table. An aggregate is a root model plus
|
||||||
- Only services directly interact with the database, and only through async methods
|
the models that have no independent lifecycle of their own. `DocumentType` has no
|
||||||
- Services are completely independent of one another. Any operation that needs to use more than a single service, which is most of them, needs to have a separate orchestration function.
|
meaning without `Document`, so it belongs to `DocumentService`; it does not get its
|
||||||
|
own service. Splitting per table produces services that must reach across each other
|
||||||
|
for every real operation, which is what line 13 forbids.
|
||||||
|
- Only services interact with the database, and only through async methods.
|
||||||
|
- **A service module must not import another service module.** This is enforced by
|
||||||
|
[test_service_boundaries](../../tests/test_service_boundaries.py). Shared types go in a
|
||||||
|
neutral module that defines no service class (see [errors](../../src/transcription/services/errors.py)).
|
||||||
|
- Not every module in this package is a service. Modules fall into three kinds:
|
||||||
|
- **Aggregate services** own models and define a `*Service` class: `documents.py`, `sources.py`,
|
||||||
|
`jobs.py`, `people.py`, `photos.py`, `maintenance.py`, and `evidence.py` (read/projection only,
|
||||||
|
owns nothing).
|
||||||
|
- **Orchestration modules** define no service class and compose writes across aggregates:
|
||||||
|
`store.py`, `workflows.py`. They are the sanctioned place to create or delete rows owned by more
|
||||||
|
than one service — see [Service Composition](#service-composition).
|
||||||
|
- **Shared infrastructure and free-function helpers** are exempt from the service rules below:
|
||||||
|
`base.py` (`ServiceBase`), `registry.py` (`RegistryService`, a generic base for lookup tables —
|
||||||
|
not an aggregate owner itself), `unit_of_work.py`, `errors.py`, `normalization.py`, `prompts.py`,
|
||||||
|
`quality.py`, `media_storage.py`, `source_media.py`. `__init__.py` exposes `ServiceBundle`.
|
||||||
|
- Cross-cutting error behavior must follow
|
||||||
|
[error-handling instructions](./error-handling.instructions.md).
|
||||||
|
|
||||||
|
## Model Ownership
|
||||||
|
|
||||||
|
Every model has exactly one owning service. The owner defines that model's invariants and
|
||||||
|
is the only service that may **create or delete** its rows.
|
||||||
|
|
||||||
|
| Model | Owner |
|
||||||
|
| --- | --- |
|
||||||
|
| `Document`, `DocumentType`, `DocumentTag` | `DocumentService` |
|
||||||
|
| `Source`, `JobSource` | `SourceService` |
|
||||||
|
| `Job` | `JobService` |
|
||||||
|
| `Person`, `PersonRole`, `DocumentPerson`, `PersonTag` | `PeopleService` |
|
||||||
|
| `GenealogyPerson`, `GenealogyFamily`, `GenealogyFamilyChild`, `GenealogyCitation` | `MaintenanceService` |
|
||||||
|
| `Photo` | `PhotosService` |
|
||||||
|
| `MaintenanceRun` | `MaintenanceService` |
|
||||||
|
| `ExecutionAttempt` | `SourceService` |
|
||||||
|
| `Tag` | shared — see below |
|
||||||
|
|
||||||
|
Keep this table complete: every table in `src/transcription/db/models.py` appears exactly once,
|
||||||
|
except `Tag`. When you add a model, add its owner here in the same change.
|
||||||
|
|
||||||
|
### `Tag` is deliberately shared
|
||||||
|
|
||||||
|
`Tag` is one table reached through two `RegistryService[Tag]` facades that differ only in the
|
||||||
|
reference model they count usage through: `TagRegistry` (`documents.py`, via `DocumentTag`) and
|
||||||
|
`PersonTagRegistry` (`people.py`, via `PersonTag`). Both create and delete `Tag` rows through the
|
||||||
|
generic registry. This is the single sanctioned exception to one-owner-per-model — do not "fix" it by
|
||||||
|
assigning `Tag` to one service, because the other facade would then be creating rows it does not own.
|
||||||
|
Any change to `Tag` semantics, labels, or normalization must be validated against **both** facades
|
||||||
|
and the junction table each one counts.
|
||||||
|
|
||||||
|
`DocumentTag` and `PersonTag` follow the junction rule below: each is created and deleted only by the
|
||||||
|
service on its own side.
|
||||||
|
|
||||||
|
### Registries
|
||||||
|
|
||||||
|
`DocumentTypeRegistry`, `TagRegistry`, `PersonRoleRegistry`, and `PersonTagRegistry` are
|
||||||
|
`RegistryService` subclasses, not independent services. A registry belongs to the aggregate service
|
||||||
|
whose module declares it and shares that service's ownership. Registry CRUD uses `<operation>_entry`
|
||||||
|
naming (see [CRUD Methods](#crud-methods)).
|
||||||
|
|
||||||
|
### Junction tables
|
||||||
|
|
||||||
|
A junction table is owned by the service that **creates and deletes its rows** — its
|
||||||
|
lifecycle owner. The service on the other side may read through the junction (via
|
||||||
|
`selectinload`) but must not create rows in it.
|
||||||
|
|
||||||
|
- `document_person` -> `PeopleService`. Every write is there; `DocumentService` only
|
||||||
|
eager-loads through it.
|
||||||
|
- `job_source` -> `SourceService`, which creates the row, records each page's outcome,
|
||||||
|
and deletes it.
|
||||||
|
|
||||||
|
Two consequences follow, and both are deliberate:
|
||||||
|
|
||||||
|
- **Cascade deletion is not a violation.** A service deleting the aggregate root it owns
|
||||||
|
may delete rows referencing that root which cannot outlive it
|
||||||
|
(`JobService.delete_job_with_guardrails` deletes the job's `job_source` rows).
|
||||||
|
- **Evidence deletion is an explicit workflow, not a runtime path.** `JobService.delete_job_and_evidence`
|
||||||
|
deletes `ExecutionAttempt` rows owned by `SourceService`. That is sanctioned because it is the
|
||||||
|
named retention workflow that `delete_job_with_guardrails` refuses to perform implicitly — that
|
||||||
|
method *blocks* deletion when attempts exist. Append-only means runtime code never rewrites or
|
||||||
|
removes history to represent a new outcome; it does not forbid a deliberate, operator-invoked
|
||||||
|
retention operation. Do not add a second path that deletes attempts.
|
||||||
|
- **Ownership governs creation and deletion, not every state transition.** `job_source` is
|
||||||
|
both a link and the transcription work queue. `JobService.cancel_job` and
|
||||||
|
`resubmit_failed_sources` transition `job_source.status` across a whole job, because that
|
||||||
|
transition is a Job lifecycle event, not a per-page outcome. They create and delete
|
||||||
|
nothing.
|
||||||
|
|
||||||
|
`EvidenceService` is read-focused and projection-focused. It may coordinate selection
|
||||||
|
flows, but append-only attempt creation remains in `SourceService` write paths.
|
||||||
|
|
||||||
|
If a new operation cannot be expressed within one owner, it belongs in an orchestration
|
||||||
|
module, not in a cross-service import.
|
||||||
|
|
||||||
## Error Handling
|
## Error Handling
|
||||||
|
|
||||||
- Service-specific errors defined at the top of the respective module and inherit from `AppError`
|
- Errors used by a single service are defined at the top of that module and inherit from `AppError`.
|
||||||
- Use a context manager for large `try/except` blocks like in [transcription](../../src/transcription/services/transcription.py)
|
- Errors shared by more than one service go in [errors](../../src/transcription/services/errors.py),
|
||||||
|
which defines no service class and is therefore importable by any of them.
|
||||||
|
- Use a context manager for large `try/except` blocks, like `handle_transcription_errors` in
|
||||||
|
[sources](../../src/transcription/services/sources.py).
|
||||||
|
- Category mapping, retry behavior, and translation boundaries are defined in
|
||||||
|
[error-handling instructions](./error-handling.instructions.md).
|
||||||
|
- Service-edge exception translation must be deterministic: map to canonical categories and preserve clear provider->service->API/UI boundaries.
|
||||||
|
|
||||||
## Checklist
|
## Checklist
|
||||||
|
|
||||||
- [ ] Uses `ServiceBase` for common logic
|
- [ ] Uses `ServiceBase` for common logic
|
||||||
- [ ] CRUD methods created at the top
|
- [ ] Session kwarg for `AsyncSession` to pass a session object into each method
|
||||||
- [ ] Session kwarg for `AsyncSession` to pass in a session object to each method
|
- [ ] Services use `self._session_scope` in their methods to pass the session through
|
||||||
- [ ] Services use `self._session_scope` in their methods to pass the session thru.
|
- Multiple operations on the same object(s) require sharing a session between all the methods used
|
||||||
- Multiple operations on the same object(s) require sharing a session between all the methods used.
|
- [ ] Every model the module touches is either owned by it or reached read-only
|
||||||
|
- [ ] Evidence writes preserve append-only semantics
|
||||||
|
|
||||||
## CRUD Methods
|
## CRUD Methods
|
||||||
|
|
||||||
- Create, read, update, and delete, created in that order
|
- Name format `<operation>_<model>`, for example `create_document` or `update_job`.
|
||||||
- Name format `<operation>_<model >`, for example `create_document` or `update_job`
|
- Where a service exposes create/read/update/delete for its root model, define them at the
|
||||||
- All services must define these 4 methods first, and in that order
|
top of the class in that order, before derived reads and workflow helpers.
|
||||||
|
- Not every aggregate needs all four. `ExecutionAttempt` is append-only evidence written by
|
||||||
|
`SourceService` workflow-facing methods, so `EvidenceService` deliberately exposes reads and
|
||||||
|
no create or delete.
|
||||||
|
Do not add unused CRUD methods to satisfy symmetry.
|
||||||
|
- `RegistryService` is generic across small lookup models and uses `<operation>_entry`
|
||||||
|
naming instead.
|
||||||
|
|
||||||
## Transaction Finalization
|
## Transaction Finalization
|
||||||
|
|
||||||
@@ -39,18 +145,9 @@ When a service method accepts an optional `session` kwarg, write methods must us
|
|||||||
- If `session` is provided: the method must **not** commit; it should `flush()` so IDs and FK values are available to the caller's transaction.
|
- If `session` is provided: the method must **not** commit; it should `flush()` so IDs and FK values are available to the caller's transaction.
|
||||||
- Use `refresh()` on returned ORM objects when the caller needs DB-populated values (defaults, triggers, merged state).
|
- Use `refresh()` on returned ORM objects when the caller needs DB-populated values (defaults, triggers, merged state).
|
||||||
|
|
||||||
Recommended helper behavior:
|
|
||||||
|
|
||||||
- Inputs: active session object, original `session` arg (or a boolean ownership flag), and an optional list of objects to refresh.
|
|
||||||
- Logic: `commit` when service-owned session, `flush` when caller-owned session, then refresh requested objects.
|
|
||||||
|
|
||||||
This keeps orchestration functions atomic: they can pass one shared session across multiple services and commit exactly once at the workflow boundary.
|
|
||||||
|
|
||||||
## Workflow Transaction Boundaries
|
## Workflow Transaction Boundaries
|
||||||
|
|
||||||
For multi-step job lifecycles (for example queued transcription jobs), orchestration functions must use explicit transaction phases.
|
For multi-step job lifecycles, orchestration functions must use explicit transaction phases.
|
||||||
|
|
||||||
Required boundary model:
|
|
||||||
|
|
||||||
- **Transaction A (claim):** transition `JobStatus.QUEUED -> JobStatus.PROCESSING` and commit immediately.
|
- **Transaction A (claim):** transition `JobStatus.QUEUED -> JobStatus.PROCESSING` and commit immediately.
|
||||||
- Perform provider/network work **outside** database transactions.
|
- Perform provider/network work **outside** database transactions.
|
||||||
@@ -64,14 +161,55 @@ Atomicity rules:
|
|||||||
- Terminal state (`TRANSCRIBED` or `FAILED`) and transcript row changes must succeed or roll back together.
|
- Terminal state (`TRANSCRIBED` or `FAILED`) and transcript row changes must succeed or roll back together.
|
||||||
- Retry persistence (`QUEUED` + retry increment + error detail) must succeed or roll back together.
|
- Retry persistence (`QUEUED` + retry increment + error detail) must succeed or roll back together.
|
||||||
|
|
||||||
Separation of concerns:
|
### Multi-page batches
|
||||||
|
|
||||||
- Worker modules should stay lightweight and delegate lifecycle transitions to service/workflow orchestration functions.
|
These two requirements are in tension for multi-page jobs: each page should be durable as
|
||||||
- In `workflows.py`, `process_queued_job` should own one complete attempt lifecycle: `QUEUED -> PROCESSING -> TRANSCRIBED|FAILED`.
|
soon as its provider call returns, but the last page must commit together with the terminal
|
||||||
- In `workflows.py`, `advance_job` should coordinate broader status progression around attempts (for example retry scheduling from `FAILED -> QUEUED`).
|
status. `process_queued_job` resolves it by committing every page except the last one
|
||||||
- Services should expose session-aware write helpers (flush on caller-owned session) so orchestration controls commit boundaries.
|
individually, then deferring the final page's write into `_finalize_batch_outcome` so it
|
||||||
- Backoff/sleep behavior must run outside transactional scopes.
|
shares the terminal transaction.
|
||||||
|
|
||||||
|
Both paths are shielded against cancellation, so the final page is no less durable than the
|
||||||
|
pages before it. Enforced by `tests/integration/test_pipeline_atomicity.py`; per-page
|
||||||
|
durability is separately enforced by
|
||||||
|
`tests/services/test_workflows_reliability.py::TestWorkflowReliability::test_transcribed_page_is_committed_before_next_provider_call_finishes`.
|
||||||
|
|
||||||
|
### Stale-reclaim safety
|
||||||
|
|
||||||
|
- `WORKER_STALE_JOB_SECONDS` must remain **greater than** `WORKER_PROVIDER_TIMEOUT_SECONDS`; stale
|
||||||
|
recovery must not be able to fire before one provider call can legitimately finish.
|
||||||
|
- Long-running multi-page orchestration must refresh job liveness explicitly between intermediate
|
||||||
|
page commits. Do not rely on incidental row updates or provider metadata writes to keep
|
||||||
|
`Job.date_updated` fresh.
|
||||||
|
- Enforced by `tests/test_config.py` and
|
||||||
|
`tests/services/test_workflows_reliability.py::TestWorkflowReliability::test_intermediate_page_commit_advances_job_liveness_timestamp`.
|
||||||
|
|
||||||
|
## Contract Alignment
|
||||||
|
|
||||||
|
- Treat `docs/` as the active architecture and requirements baseline.
|
||||||
|
- Legacy revision trees are out of scope for active implementation decisions and must not be referenced as authoritative service guidance.
|
||||||
|
- Treat `src/transcription/db/models.py` as runtime schema ground truth and `docs/schema.md` as the field-accurate contract mirror.
|
||||||
|
- `Job.status` success path is `TRANSCRIBED`.
|
||||||
|
- `JobSource.status` is queue/projection state only (`PENDING`, `TRANSCRIBED`, `FAILED`, `CANCELLED`).
|
||||||
|
- Source ingest may normalize media before persistence; persisted bytes/hash are canonical for processing and provenance.
|
||||||
|
- `ExecutionAttempt` is append-only evidence history; do not mutate historical attempt rows in runtime code.
|
||||||
|
- `Source.raw_transcription` is a projection, not authoritative history.
|
||||||
|
- Service/UI read paths that touch relationships must be eager-loaded for `lazy="raise"` compatibility.
|
||||||
|
- If model fields, enums, constraints, indexes, or relationship-loading semantics change, update `docs/schema.md` in the same change.
|
||||||
|
- If `Settings` fields or defaults change in `src/transcription/config.py`, update `.env.production.example` in the same change so keys/defaults remain synchronized and no stale settings remain documented.
|
||||||
|
|
||||||
|
## Schema Drift and Legacy Compatibility Policy
|
||||||
|
|
||||||
|
- Prefer schema migration over startup reconciliation or runtime compatibility paths in service writes.
|
||||||
|
- Do not add legacy read/write compatibility code in service workflows by default.
|
||||||
|
- If drift is discovered and a migration decision is ambiguous (for example, one-way destructive DDL, uncertain data retention impact, or unknown deployment sequence), pause and ask the user to choose migration vs compatibility before coding.
|
||||||
|
- If a temporary compatibility path is explicitly approved, document an expiration/removal plan in the same change.
|
||||||
|
|
||||||
# Service Composition
|
# Service Composition
|
||||||
|
|
||||||
Some operations, like uploading a picutre, require modifications to multiple tables, which can be done by composing methods from the service object into a separate function.
|
A service method may read across models it does not own, using eager loads from its own
|
||||||
|
aggregate root. What it may not do is import another service.
|
||||||
|
|
||||||
|
Operations that must **write** models owned by more than one service are composed in an orchestration module
|
||||||
|
([store](../../src/transcription/services/store.py),
|
||||||
|
[workflows](../../src/transcription/services/workflows.py)).
|
||||||
|
|||||||
@@ -0,0 +1,119 @@
|
|||||||
|
---
|
||||||
|
description: Authoring rules for the test suite, including markers, async discipline, and guard-test design.
|
||||||
|
applyTo: 'tests/**/*.py'
|
||||||
|
---
|
||||||
|
|
||||||
|
# Tests
|
||||||
|
|
||||||
|
Primary references:
|
||||||
|
|
||||||
|
- `AGENTS.md` (Change Protocol — failing test first)
|
||||||
|
- `docs/index.md` and `docs/invariant/*`
|
||||||
|
- `.github/skills/test-effectiveness-auditor/skill.md` (periodic audit of this suite)
|
||||||
|
|
||||||
|
The suite is not only regression protection here — it is where several architectural rules are
|
||||||
|
*defined*. `tests/test_service_boundaries.py`, `tests/test_ui_boundaries.py`,
|
||||||
|
`tests/test_provider_boundaries.py`, `tests/test_model_contract_guards.py`, and
|
||||||
|
`tests/test_meta_contract_guards.py` are the enforcement layer named in the `AGENTS.md` authority
|
||||||
|
order. A weak test in this repository does not merely fail to catch a bug; it can silently repeal a
|
||||||
|
documented invariant.
|
||||||
|
|
||||||
|
The baseline is green. `uv run pytest -q -m "not external"` must report zero failures and zero
|
||||||
|
errors, and there is no tolerated set of known-failing tests.
|
||||||
|
|
||||||
|
## Write the Failing Test First
|
||||||
|
|
||||||
|
For any behavioral fix, write the test before the fix and confirm it fails *for the reason you
|
||||||
|
expect*. A test that passes against the broken code proves nothing, and several defects in this
|
||||||
|
repository were subtle enough that a test written afterward would have done exactly that. If the
|
||||||
|
new test passes immediately, you have not reproduced the defect yet.
|
||||||
|
|
||||||
|
## Runner Configuration
|
||||||
|
|
||||||
|
Configured in `pyproject.toml`; do not work around these:
|
||||||
|
|
||||||
|
- `--strict-markers` — an unregistered marker is an error. Register new markers in
|
||||||
|
`[tool.pytest.ini_options] markers` with a description rather than inventing one at the call site.
|
||||||
|
- `asyncio_mode = "strict"` — every async test needs an explicit `@pytest.mark.asyncio`, and async
|
||||||
|
fixtures use `@pytest_asyncio.fixture`. There is no implicit promotion.
|
||||||
|
- `filterwarnings = ["error:coroutine .* was never awaited:RuntimeWarning"]` — an un-awaited
|
||||||
|
coroutine is an error, not a warning. This usually means a mock replaced an async callable with a
|
||||||
|
sync one, or an `await` was dropped. Fix the call; never silence the warning.
|
||||||
|
|
||||||
|
## Markers and Layout
|
||||||
|
|
||||||
|
- `unit` — pure logic, no framework or database.
|
||||||
|
- `integration` — touches framework, database, or multi-component contracts.
|
||||||
|
- `external` — calls live services; slow and credential-dependent.
|
||||||
|
|
||||||
|
`external` tests must also carry their own `skipif` so the suite stays green without credentials
|
||||||
|
(see `tests/services/test_transcription_external.py`). Local and documented runs use
|
||||||
|
`-m "not external"`; CI intentionally runs unfiltered, which is equivalent because those tests skip
|
||||||
|
themselves. Never let an unmarked test reach the network.
|
||||||
|
|
||||||
|
Place tests by the layer under test: `tests/services/`, `tests/ui/`, `tests/api/`,
|
||||||
|
`tests/providers/`, `tests/integration/`, with cross-cutting guards at the top level.
|
||||||
|
|
||||||
|
## Fixtures and Isolation
|
||||||
|
|
||||||
|
- Prefer the shared fixtures in `tests/conftest.py` (`default_settings`, `async_session`,
|
||||||
|
`default_session_factory`, and the per-aggregate service fixtures) over building settings or
|
||||||
|
engines by hand.
|
||||||
|
- `Settings` is isolated suite-wide by the session-scoped autouse fixture in `conftest.py`, because
|
||||||
|
`env_file` resolves against the working directory. Tests that need env-file loading pass
|
||||||
|
`_env_file=` explicitly; tests asserting declared defaults need nothing. Do not reintroduce
|
||||||
|
reliance on a developer's local env file. Guarded by `tests/test_config_isolation.py`.
|
||||||
|
- Database fixtures refuse to run against anything but the per-test path, and that refusal is
|
||||||
|
deliberate. Never relax it to point a destructive fixture at a real database.
|
||||||
|
- Tests must not leave artifacts outside `tmp_path`.
|
||||||
|
|
||||||
|
## Assertion Strength
|
||||||
|
|
||||||
|
Assert on the domain effect, not on the fact that code ran.
|
||||||
|
|
||||||
|
- Prefer persisted state, status transitions, error categories, and evidence records over
|
||||||
|
"no exception raised", "not None", or a bare status code.
|
||||||
|
- **Read committed state through a separate session.** Asserting against the same session that
|
||||||
|
performed the write can pass on unflushed in-memory state and prove nothing about durability.
|
||||||
|
This is how the atomicity guarantees in `tests/services/test_workflows_reliability.py` and
|
||||||
|
`tests/integration/test_pipeline_atomicity.py` are made real.
|
||||||
|
- Critical paths need negative-path coverage — timeouts, provider failures, validation errors,
|
||||||
|
cancellation. Happy-path-only coverage of a critical module is a gap, not a suite.
|
||||||
|
- Avoid count-threshold assertions as a proxy for correctness. A test asserting "at least N items
|
||||||
|
were discovered" passes indefinitely while the thing it was meant to protect rots; assert on a
|
||||||
|
specific known member instead.
|
||||||
|
|
||||||
|
## Guard Tests
|
||||||
|
|
||||||
|
Structural guards carry extra obligations, because they are cited as proof that a rule holds.
|
||||||
|
|
||||||
|
- **Guard the guard.** Every scanning guard needs a companion assertion that the scan actually found
|
||||||
|
something, following the existing `test_*_are_discovered` pattern. A guard that silently scans an
|
||||||
|
empty set passes forever.
|
||||||
|
- **Scope must match the claim.** A guard's name and docstring must describe only what it actually
|
||||||
|
verifies. A test covering one function while appearing to enforce a repo-wide rule is worse than
|
||||||
|
no test, because it stops anyone from writing the real one.
|
||||||
|
- **Prove non-vacuity by injected fault.** Temporarily introduce the violation, confirm the guard
|
||||||
|
fails with a comprehensible message, then revert. Do this whenever you add or materially change a
|
||||||
|
guard. Revert with an explicit edit if the file has uncommitted changes — `git checkout --` will
|
||||||
|
discard them.
|
||||||
|
- **Prefer structural analysis to substring matching.** AST inspection of imports and definitions is
|
||||||
|
resistant to false negatives; a bare-name search across the repository is not, since an unrelated
|
||||||
|
mention anywhere makes dead code look reachable.
|
||||||
|
- Failure messages should name the offending file, symbol, and the remedy. These fire for people who
|
||||||
|
did not write the guard.
|
||||||
|
- Any new file under `.github/**` must be added to `ACTIVE_CONTRACT_FILES` in
|
||||||
|
`tests/test_meta_contract_guards.py`, or the completeness guard fails by design.
|
||||||
|
|
||||||
|
## Redundancy
|
||||||
|
|
||||||
|
Duplicate coverage across layers costs runtime and dilutes signal. Pick the canonical layer for a
|
||||||
|
behavior — unit for logic, integration for wiring — and let the other layer assert only what is
|
||||||
|
unique to it. Retire tests superseded by a stronger guard instead of accumulating both, and record
|
||||||
|
deliberate retentions with a rationale rather than leaving them unexplained.
|
||||||
|
|
||||||
|
## Contract Sync Rule
|
||||||
|
|
||||||
|
When a test encodes or relaxes a documented rule, update the corresponding instruction file or
|
||||||
|
`docs/*` page in the same change. When a guard test is the enforcement for a rule stated in
|
||||||
|
`AGENTS.md` or an instruction file, cite the test by name there so the link survives refactoring.
|
||||||
@@ -11,6 +11,9 @@ Keep dependencies flowing in this direction:
|
|||||||
|
|
||||||
Pages may depend on application services and framework-provided dependencies. Components may depend on smaller components and shared presentation helpers. Services and domain modules must never depend on the UI.
|
Pages may depend on application services and framework-provided dependencies. Components may depend on smaller components and shared presentation helpers. Services and domain modules must never depend on the UI.
|
||||||
|
|
||||||
|
Cross-cutting error behavior must follow
|
||||||
|
[error-handling instructions](./error-handling.instructions.md).
|
||||||
|
|
||||||
## Package Root
|
## Package Root
|
||||||
|
|
||||||
- Keep `ui/__init__.py` as the UI composition root: register global assets, register pages, and mount NiceGUI on FastAPI.
|
- Keep `ui/__init__.py` as the UI composition root: register global assets, register pages, and mount NiceGUI on FastAPI.
|
||||||
@@ -37,17 +40,39 @@ Pages may depend on application services and framework-provided dependencies. Co
|
|||||||
- Keep app-wide navigation and layout primitives in `components/app_shell.py`.
|
- Keep app-wide navigation and layout primitives in `components/app_shell.py`.
|
||||||
- Keep generic table/event adaptation in `components/table/common.py`; feature-specific columns, row read models, and formatting belong in the feature table module.
|
- Keep generic table/event adaptation in `components/table/common.py`; feature-specific columns, row read models, and formatting belong in the feature table module.
|
||||||
- Keep exception normalization and user-facing error display in `components/error_presenter.py`; preserve `AppError` details and operation identifiers at page/component boundaries.
|
- Keep exception normalization and user-facing error display in `components/error_presenter.py`; preserve `AppError` details and operation identifiers at page/component boundaries.
|
||||||
|
- Use `components/media_urls.py` for media URL generation; do not hand-build upload/static paths in page code.
|
||||||
|
|
||||||
## CSS Assets
|
## CSS Assets
|
||||||
|
|
||||||
- Keep all application CSS in `ui/static/theme.css`; do not add page- or component-specific stylesheets or embed style blocks in Python components.
|
- Keep all application CSS in `ui/static/theme.css`; do not add page- or component-specific stylesheets or embed style blocks in Python components.
|
||||||
- Load `theme.css` once from the composition root with `ui.add_css(..., shared=True)`.
|
- Load `theme.css` once from the composition root with `ui.add_css(..., shared=True)`.
|
||||||
- Read stylesheet text through `importlib.resources.files(...)` so loading works from installed packages and is independent of the working directory.
|
- Read stylesheet text through `importlib.resources.files(...)` so loading works from installed packages and is independent of the working directory.
|
||||||
- Centralize CSS reading in one typed helper cached by resource path with `functools.cache` or an equivalent unbounded `lru_cache`. Cache the immutable stylesheet text to avoid repeated resource I/O; keep NiceGUI registration at the composition root.
|
- Centralize CSS reading in one typed helper cached by resource path.
|
||||||
- Do not encode application behavior in CSS or other static assets.
|
- Do not encode application behavior in CSS or other static assets.
|
||||||
|
|
||||||
## State and Side Effects
|
## State and Side Effects
|
||||||
|
|
||||||
- Limit component state to ephemeral interaction state such as loading flags, form values, dialogs, and expansion state.
|
- Limit component state to ephemeral interaction state such as loading flags, form values, dialogs, and expansion state.
|
||||||
- Application and worker state must be resolved at the page or application boundary and passed through narrow interfaces such as callbacks or notifier protocols.
|
- Application and worker state must be resolved at the page or application boundary and passed through narrow interfaces.
|
||||||
- Keep filesystem, network, provider, and worker orchestration behind application services or dedicated adapters. UI code may trigger those operations but must not implement them.
|
- Keep filesystem, network, provider, and worker orchestration behind application services or dedicated adapters.
|
||||||
|
|
||||||
|
## Media Route Safety Rules
|
||||||
|
|
||||||
|
Two patterns are approved:
|
||||||
|
|
||||||
|
1. **Record-validated API routes** for print/export contexts.
|
||||||
|
2. **Controlled upload URL resolver** (`components/media_urls.py`) for general UI media.
|
||||||
|
|
||||||
|
Prohibited patterns:
|
||||||
|
|
||||||
|
- Direct `file://` links or exposing local filesystem paths.
|
||||||
|
- Manual URL construction from raw `Path` values in pages/components.
|
||||||
|
- User-facing payloads containing local absolute paths.
|
||||||
|
|
||||||
|
## Contract Alignment
|
||||||
|
|
||||||
|
- Treat `docs/` as the active baseline.
|
||||||
|
- Resolve lifecycle and status semantics against `src/transcription/db/models.py` and `docs/schema.md`; do not introduce alternate status labels or implied legacy states in UI behavior.
|
||||||
|
- Use status vocabulary exactly as modeled (`queued`, `processing`, `transcribed`, `partial_success`, `failed`; and `pending`, `transcribed`, `failed`, `cancelled`).
|
||||||
|
- Print/export media flows must use record-validated routes; direct local filesystem paths are prohibited.
|
||||||
|
- If lifecycle wording/behavior changes, update corresponding `docs/ui/pages/*.md` contracts in the same change.
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
---
|
||||||
|
name: Review Python Architecture
|
||||||
|
description: Run an evidence-based architectural code review using the Python Architect Reviewer agent and python-code-reviewer skill.
|
||||||
|
agent: Python Architect Reviewer
|
||||||
|
---
|
||||||
|
|
||||||
|
# Instructions
|
||||||
|
|
||||||
|
Execute a comprehensive, evidence-based code review of the target codebase.
|
||||||
|
|
||||||
|
## Target Scope
|
||||||
|
- **Review Target:** the repository root, unless the invoker names a narrower path; review that path instead.
|
||||||
|
- **Source Root:** `src/`
|
||||||
|
- **Docs Root:** `docs/`
|
||||||
|
- **Focus Areas:** FastAPI endpoints, NiceGUI components, SQLModel persistence, asyncio workers, Pydantic V2 models, and OpenRouter provider adapters.
|
||||||
|
|
||||||
|
## Execution Rules
|
||||||
|
1. Map repository layout, dependency manifests, and configuration files from the project root before inspecting modules.
|
||||||
|
2. Read real code modules under `src/` (or the specified target path); cite exact file paths and line ranges for every finding.
|
||||||
|
3. Validate issues by running `uv run ruff check .`, `uv run ty check`, and `uv run pytest -q -m "not external"`. Nothing is on `PATH` in this `uv` project, so bare `ruff`/`ty`/`pytest` will fail.
|
||||||
|
4. Check for duplication, divergent implementations, and extractable helpers.
|
||||||
|
5. Format the entire review following the standardized 10-section template defined in the `python-code-reviewer` skill.
|
||||||
|
6. Write the final report to `./docs/reviews/<YYYY-MM-DD>-code-review.md`, using today's date. This path is defined by the skill; do not write the report anywhere else.
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
---
|
||||||
|
name: evidence-provenance-auditor
|
||||||
|
description: Deterministic reviewer for transcription evidence/provenance guarantees. Use when changes touch execution attempts, source storage, retries, transport evidence, artifact provenance, or evidence exports.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Evidence & Provenance Auditor
|
||||||
|
|
||||||
|
Perform focused, deterministic audits of evidence integrity and provenance behavior.
|
||||||
|
|
||||||
|
## When to Use
|
||||||
|
|
||||||
|
- Reviewing changes in:
|
||||||
|
- `src/transcription/services/sources.py`
|
||||||
|
- `src/transcription/services/store.py`
|
||||||
|
- `src/transcription/services/workflows.py`
|
||||||
|
- `src/transcription/services/evidence.py`
|
||||||
|
- `src/transcription/db/models.py`
|
||||||
|
- Auditing evidence exports/imports or evidence-display behavior.
|
||||||
|
- Verifying no drift from canonical provenance invariants.
|
||||||
|
|
||||||
|
## Normative References (must be used)
|
||||||
|
|
||||||
|
1. `docs/invariant/ai_evidence_and_provenance.md`
|
||||||
|
2. `docs/schema.md`
|
||||||
|
3. `docs/requirements.md`
|
||||||
|
4. `docs/error_handling.md`
|
||||||
|
|
||||||
|
## Deterministic Pass/Fail Checks
|
||||||
|
|
||||||
|
### A. Append-only history
|
||||||
|
- Every provider call results in a new `ExecutionAttempt`.
|
||||||
|
- Runtime paths do not mutate historical attempts to represent new outcomes.
|
||||||
|
- Retry behavior appends attempts rather than rewriting prior rows.
|
||||||
|
|
||||||
|
### B. Projection vs authority separation
|
||||||
|
- `Source.raw_transcription` and preferred pointers are mutable projection surfaces.
|
||||||
|
- Attempt rows remain authoritative historical evidence.
|
||||||
|
- Candidate promotion updates projection pointers without rewriting history.
|
||||||
|
|
||||||
|
### C. Transport evidence semantics
|
||||||
|
- Transport evidence is correctly labeled as application-boundary capture.
|
||||||
|
- SDK snapshots/normalized metadata are not mislabeled as native upstream payload.
|
||||||
|
- No-response timeout/network states are explicit.
|
||||||
|
|
||||||
|
### D. Canonical source identity
|
||||||
|
- Canonical stored bytes/hash/size are internally consistent.
|
||||||
|
- If ingest normalization is applied, code/docs consistently represent resulting canonical identity.
|
||||||
|
- Post-ingest derivatives do not overwrite canonical source bytes.
|
||||||
|
|
||||||
|
### E. Secret safety
|
||||||
|
- No credentials/auth headers/cookies/unrestricted headers persisted.
|
||||||
|
- Header persistence uses explicit allowlist semantics.
|
||||||
|
|
||||||
|
### F. Route/path safety
|
||||||
|
- Print/export source access is record-validated.
|
||||||
|
- UI/media path construction does not expose local filesystem paths.
|
||||||
|
|
||||||
|
### G. Schema/docs alignment
|
||||||
|
- Evidence-related model fields and semantics align with canonical docs.
|
||||||
|
- Evidence model changes require same-change doc updates.
|
||||||
|
|
||||||
|
### H. Canonical authority boundaries
|
||||||
|
- Active guidance resolves against `docs/*` and current instruction files.
|
||||||
|
|
||||||
|
## Review Workflow
|
||||||
|
|
||||||
|
1. Read normative references first.
|
||||||
|
2. Inspect model + service + workflow write paths.
|
||||||
|
3. Inspect evidence read/display/export paths.
|
||||||
|
4. Report high-confidence findings with concrete path/line evidence.
|
||||||
|
5. Classify each finding by invariant family (A-H).
|
||||||
|
|
||||||
|
## Output Format
|
||||||
|
|
||||||
|
Use this structure:
|
||||||
|
|
||||||
|
- Verdict by invariant family (A-H)
|
||||||
|
- Findings with `Location`, `Observed Behavior`, `Risk`, `Recommended Fix`
|
||||||
|
- Drift table (`Doc claim` vs `Code reality` vs `Action`)
|
||||||
|
- Regression guards needed
|
||||||
@@ -0,0 +1,276 @@
|
|||||||
|
---
|
||||||
|
name: python-code-reviewer
|
||||||
|
description: Perform an evidence-based, senior architect code review for Python codebases using FastAPI, NiceGUI, SQLModel, SQLAlchemy, Pydantic V2, asyncio, and OpenRouter. Use when asked to review Python repositories, perform architectural or code audits, or evaluate code against Python 3.12+ best practices.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Python Code Reviewer
|
||||||
|
|
||||||
|
Perform thorough, evidence-based code reviews for Python projects. Every finding must cite concrete file paths and line ranges, avoid speculation, and include recommended fixes.
|
||||||
|
|
||||||
|
## When to Use
|
||||||
|
|
||||||
|
- Performing an architectural or code quality review of a Python codebase.
|
||||||
|
- Auditing applications using FastAPI, NiceGUI, SQLModel/SQLAlchemy, Pydantic V2, or asyncio workers.
|
||||||
|
- Generating structured Markdown review reports in `./docs/reviews`.
|
||||||
|
|
||||||
|
## Technical Stack Scope
|
||||||
|
|
||||||
|
- **Runtime:** Python 3.12+
|
||||||
|
- **Web Application:** FastAPI and NiceGUI
|
||||||
|
- **Persistence:** SQLModel, SQLAlchemy (SQLite and PostgreSQL support)
|
||||||
|
- **Validation & Settings:** Pydantic V2 and pydantic-settings
|
||||||
|
- **Concurrency:** Python asyncio workers
|
||||||
|
- **Vision/LLM Integration:** OpenRouter / provider adapters
|
||||||
|
- **Image & Print Pipeline:** Pillow-backed media handling and print/export rendering
|
||||||
|
- **Quality & Testing:** pytest, pytest-asyncio, Ruff, and ty
|
||||||
|
|
||||||
|
NiceGUI is pinned to an exact version (`nicegui==3.13.0` in `pyproject.toml`); API guidance
|
||||||
|
must be correct for that release rather than for the latest published version. The exact pin is
|
||||||
|
a deliberate release-stability decision recorded in `docs/production-runbook.md` ("Dependency
|
||||||
|
upgrade policy") — do not report it as a defect or recommend widening it.
|
||||||
|
|
||||||
|
## Review Workflow
|
||||||
|
|
||||||
|
1. **Map the Repository First:** Inspect entry points, package layout, configurations, dependency manifests, and any project-specific rule files (`AGENTS.md`, `.github/instructions/`, `.github/skills/`). Project-specific conventions override generic advice.
|
||||||
|
2. **Establish Canonical Authority First:** Read architecture/contracts (`docs/*`, `docs/invariant/*`, UI docs) and active instructions/skills before evaluating source behavior.
|
||||||
|
3. **Read Representative Modules:** Sample across all layers (routes/pages, UI components, services, workers, persistence, provider adapters, settings, tests) before drawing conclusions.
|
||||||
|
4. **Run Drift Analysis:** Compare documented intended behavior versus repository ground truth; identify both implementation drift and undocumented-but-repeatable conventions that should be formalized.
|
||||||
|
5. **Run Dead-Code/Orphan Sweep:** Identify candidate orphan modules/functions/classes with zero inbound references, then verify expected exceptions (entrypoints, framework/plugin registration, dynamic imports/reflection, CLI hooks, test-only utilities) before marking as orphaned.
|
||||||
|
6. **Assess Boundary and Coupling Health:** Evaluate UI/service/persistence/provider dependency flow, identify circular dependencies, leaky abstractions, and transaction ownership ambiguity.
|
||||||
|
7. **Assess Invariant Placement:** For each hard rule, decide whether it belongs in docs (rationale), instructions (active steering), skills (periodic audit procedure), or deterministic tests (enforcement).
|
||||||
|
8. **Verify Claims:** This is a `uv` project (`uv.lock`, root `ruff.toml`). Run `uv run ruff check .`, `uv run ty check`, and `uv run pytest -q -m "not external"` rather than guessing, and record the exact commands and their outcomes in the report.
|
||||||
|
9. **Validate Recommendations Against Consumers:** A recommendation is a claim about the future and must be verified like any other. Before recommending a change to a shared symbol — a model field, an exception attribute, a helper's return value, a function signature — enumerate **every** consumer of that symbol (`grep` the whole repo, including tests) and confirm the fix is safe for each one. Record the consumers in the finding's **Blast Radius**. A fix that is correct for the path that produced the finding can silently break a second consumer, and evidence/provenance and logging paths are the usual casualties because they read the same fields the UI does.
|
||||||
|
10. **Prioritize Hot Paths:** Focus deeply on request handling, database sessions, background workers, and external API calls.
|
||||||
|
11. **Enforce Read-Only Safety:** Do not modify code unless explicitly instructed.
|
||||||
|
12. **Escalate Provenance Audits:** For evidence/provenance-heavy changes, apply invariant checks from `.github/skills/evidence-provenance-auditor/skill.md` and include pass/fail outcomes in the report.
|
||||||
|
13. **Escalate Test-Suite Audits:** When findings touch test coverage, redundancy, or assertion strength, apply `.github/skills/test-effectiveness-auditor/skill.md` and include its outcomes alongside the provenance results.
|
||||||
|
|
||||||
|
### Worked example: why step 9 exists
|
||||||
|
|
||||||
|
The 2026-08-23 review recommended fixing a filesystem-path leak in
|
||||||
|
`classify_unexpected_error` by making `AppError.message` generic and logging the exception
|
||||||
|
detail instead. The analysis of the leak was correct, and the fix was implemented as written.
|
||||||
|
|
||||||
|
It was wrong. `AppError.message` had a second consumer the review never traced:
|
||||||
|
`format_error_detail`, which writes `ExecutionAttempt.error_detail` — a **provenance record**.
|
||||||
|
The recommended fix closed a privacy leak by silently stripping root-cause data from the
|
||||||
|
evidence history this system exists to preserve. It was caught only because an unrelated
|
||||||
|
integration test asserted on the persisted error text.
|
||||||
|
|
||||||
|
The correct fix separated the audiences — a user-safe `message` and an internal-only `detail`
|
||||||
|
that still reaches evidence and logs. One `grep` for consumers of `.message` during the review
|
||||||
|
would have found this. Treat any recommendation that changes a widely-read field as unverified
|
||||||
|
until its consumers are enumerated.
|
||||||
|
|
||||||
|
## Repo-Specific Deterministic Checks (Transcription)
|
||||||
|
When reviewing this repository, always include explicit pass/fail checks for the following.
|
||||||
|
Where **Enforced by** reads *unenforced*, recommending a deterministic test is itself a finding.
|
||||||
|
|
||||||
|
| # | Check | Enforced by |
|
||||||
|
| :-- | :--- | :--- |
|
||||||
|
| 1 | **Service boundary rule:** no service-to-service imports | `tests/test_service_boundaries.py` |
|
||||||
|
| 2 | **UI boundary rule:** pages/components do not perform persistence access | `tests/test_ui_boundaries.py` |
|
||||||
|
| 3 | **Status vocabulary conformance:** `JobStatus`/`JobSourceStatus`/`JobPurpose` usage matches current enums in `src/transcription/db/models.py`; no stringly-typed status literals | `tests/test_model_contract_guards.py` |
|
||||||
|
| 4 | **Evidence ownership conformance:** append-only attempt history is preserved and projection writes are not mistaken for history mutation (`src/transcription/services/sources.py`, `src/transcription/services/evidence.py`) | `tests/test_evidence_provenance.py::test_attempts_are_append_only_and_exported_with_integrity` |
|
||||||
|
| 5 | **Canonical authority:** findings must resolve against `docs/*` first | `tests/test_meta_contract_guards.py::test_canonical_authority_references_are_present` |
|
||||||
|
| 6 | **Schema contract fidelity:** when model/persistence behavior changes, `docs/schema.md` remains field-accurate with `src/transcription/db/models.py` | `tests/test_model_contract_guards.py` (field names, ordering, enum members, table coverage), `tests/test_meta_contract_guards.py` (presence and references) |
|
||||||
|
| 7 | **Media boundary conformance:** print/export media is record-validated and UI media URL generation uses controlled resolver paths | `tests/test_media_path_safety.py`, `tests/ui/test_media_urls.py` |
|
||||||
|
| 8 | **Eager-loading conformance:** service/UI read paths satisfy `lazy="raise"` expectations | `tests/test_model_contract_guards.py` (declaration-side; documented `noload` exceptions must match `docs/schema.md`) |
|
||||||
|
| 9 | **Cross-cutting error conformance:** service/API/UI translation and retry behavior align with `.github/instructions/error-handling.instructions.md` | `tests/test_errors.py`, `tests/api/test_error_responses.py`, `tests/ui/test_error_presenter.py` |
|
||||||
|
| 10 | **Orphaned/dead-code conformance:** include a deterministic orphan sweep and report confirmed orphans removed/retained with rationale | `tests/test_orphan_sweep.py` (`KNOWN_ORPHANS` records each retained orphan and its rationale) |
|
||||||
|
|
||||||
|
## Core Review Areas
|
||||||
|
|
||||||
|
### 1. Python Best Practices (3.12+)
|
||||||
|
- **Type Annotations:** Ensure completeness, modern syntax (`X | None`, builtin generics, `Self`, `type` statements), and avoid unparameterized containers or bare `Any`.
|
||||||
|
- **Error Handling:** Identify bare/broad `except`, swallowed exceptions, missing `raise ... from`, and exceptions used for control flow.
|
||||||
|
- **Resource Management:** Verify context managers for files, DB sessions, HTTP clients, and locks. Check for leaked tasks or connections.
|
||||||
|
- **Data Modeling:** Check proper use of dataclasses vs. Pydantic models vs. dictionaries. Eliminate mutable default arguments and stringly-typed payloads.
|
||||||
|
- **Idioms & Clean Code:** Verify `pathlib` usage over `os.path`, comprehensions vs manual loops, removal of dead code, and elimination of magic numbers.
|
||||||
|
|
||||||
|
### 2. FastAPI
|
||||||
|
- **Dependency Injection:** Verify `Depends` is used for shared resources (DB sessions, settings, clients) rather than global singletons.
|
||||||
|
- **Route Design:** Validate HTTP verbs, status codes, path/query/body typing, `response_model`, and domain-based router organization.
|
||||||
|
- **Lifecycle & Concurrency:** Ensure lifespan handlers are used instead of deprecated `@app.on_event`. Flag blocking synchronous calls in `async def` endpoints.
|
||||||
|
|
||||||
|
### 3. NiceGUI
|
||||||
|
- **Separation of Concerns:** Ensure UI components delegate business logic and persistence to service layers.
|
||||||
|
- **Client State Handling:** Verify correct use of client-scoped state vs global state to avoid state leaks across sessions.
|
||||||
|
- **Async Execution:** Check for blocking operations on the UI event loop and unbounded timers/pollers.
|
||||||
|
|
||||||
|
### 4. Persistence (SQLModel / SQLAlchemy)
|
||||||
|
- **Session Lifecycle:** Enforce one session per request/unit of work with explicit commit/rollback/close boundaries.
|
||||||
|
- **Query Optimization:** Detect N+1 patterns, missing eager loads (`selectinload`/`joinedload`), queries inside loops, and unindexed filters.
|
||||||
|
- **Cross-Dialect Portability:** Check compatibility for both SQLite (WAL mode, pragmas) and PostgreSQL (JSONB, locking, autoincrement).
|
||||||
|
|
||||||
|
### 5. Pydantic V2 & Settings
|
||||||
|
- **V2 Migration:** Flag legacy V1 patterns (`@validator`, `Config` class, `.dict()`, `parse_obj`) and use V2 equivalents (`@field_validator`, `model_config = ConfigDict(...)`, `model_dump()`).
|
||||||
|
- **Settings Management:** Ensure `BaseSettings` is the single source of truth without scattered `os.getenv` calls or committed secrets.
|
||||||
|
|
||||||
|
### 6. Concurrency & Asyncio Workers
|
||||||
|
- **Task Lifecycle:** Flag unreferenced `create_task` calls that risk garbage collection, missing cancellation handling, and lack of graceful shutdown.
|
||||||
|
- **Backpressure & Synchronization:** Check for appropriate use of `asyncio.Queue`, `TaskGroup`, `Lock`, and backoff retries.
|
||||||
|
|
||||||
|
### 7. Provider Adapters (OpenRouter / APIs)
|
||||||
|
- **Adapter Encapsulation:** Verify provider-specific details (headers, model names, payload formats) do not leak into UI or business logic.
|
||||||
|
- **Client Lifecycle:** Reuse shared `AsyncClient` instances with proper connection pooling and timeouts. Validate API responses using Pydantic schemas.
|
||||||
|
|
||||||
|
### 8. Testing & Quality Tooling
|
||||||
|
- **Test Isolation:** Verify tests do not rely on live external services, real clocks, or shared global state.
|
||||||
|
- **Async Test Setup:** Check `pytest-asyncio` configuration and fixture lifecycle.
|
||||||
|
- **Project Test Contract (`pyproject.toml`):** `--strict-markers` is enabled, so every marker must be declared; `asyncio_mode = "strict"` requires explicit `@pytest.mark.asyncio`; declared markers are `unit`, `integration`, and `external`, and `external` must be excluded from default verification runs. `filterwarnings` promotes `coroutine ... was never awaited` to an **error** — treat any unawaited coroutine as a hard failure and a Critical/High finding, never a warning.
|
||||||
|
- **Suite Signal Quality:** For low-value, redundant, or tautological tests, escalate to `.github/skills/test-effectiveness-auditor/skill.md` and fold its outcomes into the report.
|
||||||
|
|
||||||
|
### 9. Duplication & Consolidation
|
||||||
|
- Identify repeated code blocks, candidate helper abstractions, divergent patterns for identical operations, and duplicated domain constants.
|
||||||
|
|
||||||
|
### 10. Orphaned/Dead Code Audit
|
||||||
|
- Find candidate orphan modules/functions/classes with no inbound references.
|
||||||
|
- Validate each candidate against dynamic wiring exceptions (entrypoints, plugin registration, reflection/dynamic imports, CLI hooks, test utilities).
|
||||||
|
- Report outcomes as: removed orphan, retained-with-justification, or uncertain-follow-up.
|
||||||
|
|
||||||
|
### 11. Architecture & Governance
|
||||||
|
- **Architectural Drift:** Compare intended architecture rules against implementation behavior and cite concrete drift points.
|
||||||
|
- **Systemic Health:** Evaluate domain cohesion, dependency direction, lifecycle consistency, and operational reliability seams.
|
||||||
|
- **Invariant Routing:** Recommend the correct enforcement layer per rule (docs vs instructions vs skills vs tests).
|
||||||
|
- **Meta-Tooling Alignment:** Recommend updates for instruction files and skills when repository patterns or contracts evolve.
|
||||||
|
|
||||||
|
## Severity Rubric
|
||||||
|
|
||||||
|
Severity reflects concrete consequence, never style preference or effort to fix.
|
||||||
|
|
||||||
|
- **Critical:** Data or evidence loss/corruption; provenance or append-only history violated; secret leakage; silent wrong output presented as authoritative.
|
||||||
|
- **High:** Architectural boundary violated (service/UI/persistence/provider); runtime failure or unhandled exception on a hot path (request handling, DB sessions, worker loop, external API calls); documented invariant contradicted by implementation.
|
||||||
|
- **Medium:** Correctness risk under load or edge conditions (N+1, missing eager load, leaked task, missing timeout); drift between docs and code with no immediate runtime impact.
|
||||||
|
- **Low:** Maintainability, typing completeness, duplication, naming, or dead code with no behavioral risk.
|
||||||
|
|
||||||
|
### Reachability
|
||||||
|
|
||||||
|
Severity states how bad the consequence is; **Reachability** states whether it can happen today.
|
||||||
|
They are independent, and a finding is not complete without both. Record one of:
|
||||||
|
|
||||||
|
- **Live:** reachable in the current configuration and deployment.
|
||||||
|
- **Latent:** the defective code is present but unreachable because of a current setting, single-
|
||||||
|
instance deployment, or absent caller. **State the exact condition that unblocks it.**
|
||||||
|
- **Theoretical:** requires a combination the project has explicitly ruled out.
|
||||||
|
|
||||||
|
Latent findings carry a scheduling constraint that severity alone cannot express: a latent defect
|
||||||
|
must usually be fixed *before* the change that makes it live, not after. Say so explicitly in the
|
||||||
|
finding and reflect the ordering in the §9 action plan — for example, "fix the retry-category gate
|
||||||
|
before raising `worker_max_retries` above 0," or "handle this `IntegrityError` before deploying a
|
||||||
|
second worker replica." Do not downgrade severity merely because a finding is latent.
|
||||||
|
|
||||||
|
### Conflicting invariants
|
||||||
|
|
||||||
|
When a fix sits between two invariants that pull in opposite directions, say so in the
|
||||||
|
**Recommendation** and name both, along with the test that guards each. Flag explicitly what the
|
||||||
|
over-correction would be, because the simplest-looking fix usually satisfies one invariant by
|
||||||
|
silently destroying the other. A recommendation that resolves one side without naming the other is
|
||||||
|
incomplete and will be implemented incorrectly.
|
||||||
|
|
||||||
|
## Output Report Structure & Template
|
||||||
|
|
||||||
|
Generate Markdown reports at `./docs/reviews/<YYYY-MM-DD>-code-review.md` following this exact
|
||||||
|
template structure. Reports are dated, non-canonical artifacts: `docs/reviews/**` is explicitly
|
||||||
|
**not** part of the canonical authority set that the canonical-authority check resolves against.
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
# Architecture & Code Review Report
|
||||||
|
|
||||||
|
**Repository Target:** `project-root/`
|
||||||
|
**Target Stack:** Python 3.12+ | FastAPI | NiceGUI | SQLModel/SQLAlchemy | Pydantic V2 | asyncio | OpenRouter
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Executive Summary
|
||||||
|
- 5-10 bullets on overall health, top risks, and high-leverage refactors.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Executive Architecture Assessment
|
||||||
|
- High-level verdict on domain cohesion, boundary clarity, and architecture fitness.
|
||||||
|
- Top 3-5 systemic risks or bottlenecks.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Findings by Severity
|
||||||
|
|
||||||
|
### Critical Severity
|
||||||
|
#### [CRIT-01] Title
|
||||||
|
- **Location:** `path/to/file.py:lines`
|
||||||
|
- **Reachability:** Live / Latent (state the exact condition that unblocks it) / Theoretical
|
||||||
|
- **Problem & Consequence:** Concrete consequence, not a style opinion.
|
||||||
|
- **Blast Radius:** Every consumer of the symbols the recommendation changes, each confirmed
|
||||||
|
safe. Write `None — change is local` only after actually searching. If the fix touches a
|
||||||
|
shared field or helper, list the call sites (including tests and evidence/logging paths).
|
||||||
|
- **Recommendation:** Fix with before/after sketch. If two invariants conflict here, name both,
|
||||||
|
name the test guarding each, and state what the over-correction would be.
|
||||||
|
- **Effort:** S / M / L
|
||||||
|
|
||||||
|
### High Severity
|
||||||
|
#### [HIGH-01] Title
|
||||||
|
...
|
||||||
|
|
||||||
|
### Medium Severity
|
||||||
|
#### [MED-01] Title
|
||||||
|
...
|
||||||
|
|
||||||
|
### Low Severity
|
||||||
|
#### [LOW-01] Title
|
||||||
|
...
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Architectural Drift & Gap Analysis
|
||||||
|
|
||||||
|
`Direction` is `doc->code` (implementation must change to match documented intent) or
|
||||||
|
`code->doc` (an undocumented but repeatable convention that should be formalized).
|
||||||
|
|
||||||
|
| Area / Component | Direction | Documented / Intended Rule | Actual Implementation State | Severity | Recommended Resolution |
|
||||||
|
| :--- | :--- | :--- | :--- | :--- | :--- |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Invariant Inventory & Routing Recommendations
|
||||||
|
| Invariant / Constraint | Current Location | Recommended Target Layer | Rationale |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. Stack-Specific Analysis
|
||||||
|
- Python 3.12+ Best Practices
|
||||||
|
- FastAPI
|
||||||
|
- NiceGUI
|
||||||
|
- SQLModel & SQLAlchemy
|
||||||
|
- Pydantic V2 & Settings
|
||||||
|
- Asyncio Workers
|
||||||
|
- OpenRouter / Adapter Boundary
|
||||||
|
- Testing & Quality Tooling
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. Duplication & Consolidation Report
|
||||||
|
| Pattern / Duplication | Locations | Proposed Canonical Home | Estimated Lines Removed |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
|
||||||
|
### Proposed Canonical Abstractions
|
||||||
|
- Code signatures and implementation homes.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. Meta-Tooling & Instruction Update Recommendations
|
||||||
|
- Required updates to docs, instructions, skills, or tests to keep enforcement current.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. Prioritized Dependency-Ordered Action Plan
|
||||||
|
1. **Phase 1: Blocking fixes**
|
||||||
|
2. **Phase 2: Enforcement hardening**
|
||||||
|
3. **Phase 3: Reliability & concurrency**
|
||||||
|
4. **Phase 4: Consolidation & refactoring**
|
||||||
|
5. **Phase 5: Non-blocking governance/documentation depth**
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. Preserved Strengths
|
||||||
|
- Existing patterns worth maintaining.
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
---
|
||||||
|
name: test-effectiveness-auditor
|
||||||
|
description: Periodic reviewer for test-suite signal quality. Detects low-value or redundant tests, validates contract coverage, and recommends pruning or strengthening actions.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Test Effectiveness Auditor
|
||||||
|
|
||||||
|
Run a deterministic audit of test usefulness. Focus on whether tests catch real regressions, not whether they merely execute code.
|
||||||
|
|
||||||
|
## When to Use
|
||||||
|
|
||||||
|
- Monthly/quarterly test-health review.
|
||||||
|
- Pre-release hardening when test count grows quickly.
|
||||||
|
- After major AI-assisted test generation.
|
||||||
|
- When suite runtime is increasing without clear quality gains.
|
||||||
|
|
||||||
|
## Primary Objectives
|
||||||
|
|
||||||
|
1. Identify tests that are weak, redundant, or non-diagnostic.
|
||||||
|
2. Confirm critical contracts are guarded by meaningful assertions.
|
||||||
|
3. Produce a prune/strengthen backlog with explicit risk and effort.
|
||||||
|
|
||||||
|
## Normative References (Transcription Repo)
|
||||||
|
|
||||||
|
1. `docs/*`
|
||||||
|
2. `docs/invariant/*`
|
||||||
|
3. `.github/instructions/*.instructions.md`
|
||||||
|
4. `tests/test_meta_contract_guards.py`
|
||||||
|
5. Contract-specific guards (`tests/test_service_boundaries.py`, `tests/test_ui_boundaries.py`, worker/evidence/media/error suites)
|
||||||
|
|
||||||
|
## Deterministic Audit Checks
|
||||||
|
|
||||||
|
### A. Contract Traceability
|
||||||
|
- Each high-risk contract maps to at least one focused regression test file.
|
||||||
|
- Missing mapping is a gap.
|
||||||
|
|
||||||
|
### B. Assertion Strength
|
||||||
|
- Flag tests that only assert status code, non-null, or “no exception” without validating state transitions or persisted outcomes.
|
||||||
|
- Prefer assertions on domain effects: DB rows, status changes, error categories, evidence writes, or emitted payload shape.
|
||||||
|
|
||||||
|
### C. Failure-Path Coverage
|
||||||
|
- Critical paths must include negative-path tests (timeouts, provider errors, validation failures, cancellation paths, retries).
|
||||||
|
- Happy-path-only coverage on critical modules is a gap.
|
||||||
|
|
||||||
|
### D. Redundancy and Noise
|
||||||
|
- Detect near-duplicate tests asserting the same behavior at multiple layers with no extra signal.
|
||||||
|
- Recommend canonical location (unit/integration) and prune overlaps.
|
||||||
|
|
||||||
|
### E. Mutation/Change Sensitivity
|
||||||
|
- Prefer mutation testing for high-risk modules when practical.
|
||||||
|
- If not run, identify tests likely to survive meaningful code mutations (low sensitivity).
|
||||||
|
|
||||||
|
### F. Drift Guards
|
||||||
|
- Verify config/doc/instruction contracts have deterministic guards and are current.
|
||||||
|
- Ensure settings/docs synchronization checks remain active.
|
||||||
|
|
||||||
|
## Evidence Standards
|
||||||
|
|
||||||
|
- Every finding must include concrete file paths and line ranges.
|
||||||
|
- No speculative claims.
|
||||||
|
- Distinguish clearly between:
|
||||||
|
- **Confirmed ineffective tests**
|
||||||
|
- **Likely weak tests (needs mutation/probe confirmation)**
|
||||||
|
|
||||||
|
## Output Format
|
||||||
|
|
||||||
|
Produce a Markdown report at `docs/reviews/<YYYY-MM-DD>-test-effectiveness.md`, using today's date.
|
||||||
|
Like code review reports, it is a dated, non-canonical artifact: `docs/reviews/**` is not part of the
|
||||||
|
canonical authority set.
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
# Test Effectiveness Audit Report
|
||||||
|
|
||||||
|
## 1. Executive Verdict
|
||||||
|
- Effective / Effective with Conditions / Needs Remediation
|
||||||
|
- Top risks to confidence
|
||||||
|
|
||||||
|
## 2. Contract Coverage Matrix
|
||||||
|
| Contract | Guarding Tests | Signal Quality | Gap | Action |
|
||||||
|
| :--- | :--- | :--- | :--- | :--- |
|
||||||
|
|
||||||
|
## 3. Weak/Redundant Test Findings
|
||||||
|
| Finding ID | Location | Why Low-Signal | Risk | Recommendation |
|
||||||
|
| :--- | :--- | :--- | :--- | :--- |
|
||||||
|
|
||||||
|
## 4. Prune/Strengthen Backlog
|
||||||
|
| Task ID | Goal | Files | Acceptance Criteria | Validation |
|
||||||
|
| :--- | :--- | :--- | :--- | :--- |
|
||||||
|
|
||||||
|
## 5. Confidence Recommendation
|
||||||
|
- Go / Go with Conditions / No-Go for release confidence
|
||||||
|
```
|
||||||
|
|
||||||
|
## Decision Rules
|
||||||
|
|
||||||
|
- Do not recommend deleting a test unless equivalent or stronger coverage is identified.
|
||||||
|
- Prefer strengthening assertions before adding more tests.
|
||||||
|
- Prioritize deterministic contract guards over broad snapshot-style tests.
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
name: Quality Gate
|
||||||
|
|
||||||
|
# Repository quality gate. Before this workflow existed, ruff, ty, and pytest were
|
||||||
|
# enforced only by .pre-commit-config.yaml for developers who had run
|
||||||
|
# `pre-commit install`.
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
gate:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Check out the commit under test
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install uv
|
||||||
|
run: |
|
||||||
|
curl -LsSf https://astral.sh/uv/install.sh | sh
|
||||||
|
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
|
||||||
|
|
||||||
|
- name: Install dependencies from the lockfile
|
||||||
|
# --locked fails if uv.lock has drifted from pyproject.toml, so a stale
|
||||||
|
# lockfile is caught here rather than producing an untested dependency set.
|
||||||
|
run: uv sync --locked
|
||||||
|
|
||||||
|
- name: Write placeholder configuration
|
||||||
|
# Settings requires openrouter_api_key and 115 tests cannot construct
|
||||||
|
# Settings without it. This is written to .env.production rather than exported
|
||||||
|
# as an environment variable on purpose: the external tests guard on
|
||||||
|
# os.getenv("OPENROUTER_API_KEY"), which reads the process environment and
|
||||||
|
# not the file, so writing the file reproduces the local result exactly -
|
||||||
|
# the 4 external tests skip instead of running against a fake key and
|
||||||
|
# failing. Exporting it instead produces 3 failures.
|
||||||
|
run: echo "OPENROUTER_API_KEY=ci-placeholder-not-a-real-key" > .env.production
|
||||||
|
|
||||||
|
- name: Lint and type check
|
||||||
|
# Runs the hooks defined in .pre-commit-config.yaml instead of repeating
|
||||||
|
# "ruff check" and "ty check" here. The commands then have one definition,
|
||||||
|
# so the local and CI gates cannot drift apart.
|
||||||
|
run: uv run pre-commit run --all-files --show-diff-on-failure
|
||||||
|
|
||||||
|
- name: Tests
|
||||||
|
# Deliberately unfiltered, unlike the "-m 'not external'" form the guidance files
|
||||||
|
# use for local runs. Tests marked "external" skip themselves when live-service
|
||||||
|
# credentials are absent, so CI gets the same effective set plus a real run of any
|
||||||
|
# external test whose credentials are configured. Not drift -- do not "fix" this to
|
||||||
|
# match the local command without also giving those tests a way to run.
|
||||||
|
run: uv run pytest
|
||||||
+16
-4
@@ -11,13 +11,25 @@ wheels/
|
|||||||
|
|
||||||
# Environment secrets
|
# Environment secrets
|
||||||
.env
|
.env
|
||||||
|
.env.production
|
||||||
|
|
||||||
# SQLite database
|
# SQLite database
|
||||||
*.db
|
*.db
|
||||||
|
|
||||||
# Document images
|
# All data including db, backups, document images, photos, and logs:
|
||||||
uploads/*
|
|
||||||
data/*
|
data/*
|
||||||
|
data/backups/*
|
||||||
|
data/documents/*
|
||||||
|
data/logs/*
|
||||||
|
data/photos/*
|
||||||
|
data-local/*
|
||||||
|
|
||||||
# Local destructive-test backups
|
|
||||||
.test-backups/
|
# Migration tests
|
||||||
|
data-migration-test/*
|
||||||
|
.migration-bundle/*
|
||||||
|
|
||||||
|
# Cloudflare tunnel local runtime files
|
||||||
|
deploy/cloudflared/config.yml
|
||||||
|
deploy/cloudflared/config.yaml
|
||||||
|
deploy/cloudflared/credentials.json
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
# Quality gate: `ruff check`, `ruff format --check`, and `ty check`
|
||||||
|
# are blocking once known `ty` false positives are suppressed inline with rationale.
|
||||||
|
#
|
||||||
|
# Both tools are uv-managed dev dependencies and are not on PATH, so each entry must
|
||||||
|
# go through `uv run`.
|
||||||
|
repos:
|
||||||
|
- repo: local
|
||||||
|
hooks:
|
||||||
|
- id: ruff
|
||||||
|
name: ruff check
|
||||||
|
entry: uv run ruff check
|
||||||
|
language: system
|
||||||
|
types_or: [python, pyi]
|
||||||
|
require_serial: true
|
||||||
|
- id: ruff-format
|
||||||
|
name: ruff format check
|
||||||
|
entry: uv run ruff format --check .
|
||||||
|
language: system
|
||||||
|
types_or: [python, pyi]
|
||||||
|
pass_filenames: false
|
||||||
|
require_serial: true
|
||||||
|
- id: ty
|
||||||
|
name: ty check
|
||||||
|
entry: uv run ty check
|
||||||
|
language: system
|
||||||
|
types_or: [python, pyi]
|
||||||
|
pass_filenames: false
|
||||||
|
require_serial: true
|
||||||
|
verbose: true
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
# AGENTS.md
|
||||||
|
|
||||||
|
Orientation for AI agents working in this repository. This file is a **router**, not a spec:
|
||||||
|
it points at canonical authority and flags the traps that are expensive to discover by trial.
|
||||||
|
Where this file and `docs/*` disagree, `docs/*` wins.
|
||||||
|
|
||||||
|
## What This Is
|
||||||
|
|
||||||
|
A document transcription system that preserves durable archival records (Documents, Sources,
|
||||||
|
People) and executes page transcription asynchronously through vision/LLM providers. Its
|
||||||
|
defining constraint is **evidence**: every machine attempt is recorded append-only with
|
||||||
|
request/response provenance. Features that would lose, mutate, or obscure that history are
|
||||||
|
wrong regardless of how convenient they are.
|
||||||
|
|
||||||
|
Stack: Python 3.12+ · FastAPI + NiceGUI · SQLModel/SQLAlchemy (SQLite-first, PostgreSQL-
|
||||||
|
compatible) · Pydantic V2 · asyncio worker · OpenRouter adapter.
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
This is a `uv` project. **Nothing is on `PATH`** — `ruff`, `ty`, and `pytest` all require
|
||||||
|
`uv run`. Bare invocations fail with command-not-found.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
uv run ruff check . # lint (blocking in pre-commit)
|
||||||
|
uv run ruff format --check . # format (blocking in pre-commit)
|
||||||
|
uv run ty check # types (blocking in pre-commit)
|
||||||
|
uv run pytest -q -m "not external" # default verification run
|
||||||
|
```
|
||||||
|
|
||||||
|
`external` marks tests that hit live services; always exclude it unless explicitly asked.
|
||||||
|
All four commands are expected to pass clean — there is no tolerated baseline of failures.
|
||||||
|
If `ty` reports something, fix it or suppress it inline *with a rationale comment*; a bare
|
||||||
|
`ignore` will not survive review.
|
||||||
|
|
||||||
|
## Authority Order
|
||||||
|
|
||||||
|
Resolve every question in this order, and stop at the first that answers it:
|
||||||
|
|
||||||
|
1. **`docs/*`** — canonical. Start at [`docs/index.md`](docs/index.md), which defines the
|
||||||
|
reading order. `docs/invariant/*` holds cross-version rules that outlive any release.
|
||||||
|
2. **`.github/instructions/*.md`** — active steering, auto-attached when you edit matching
|
||||||
|
paths. Covers services, UI, providers, tests, error handling, and documentation sync.
|
||||||
|
3. **`.github/skills/*`** — periodic audit procedures (code review, provenance, test
|
||||||
|
effectiveness).
|
||||||
|
4. **`tests/`** — deterministic enforcement. A guard test is the ground truth for whatever
|
||||||
|
rule it encodes.
|
||||||
|
|
||||||
|
`.github/agents/` and `.github/prompts/` hold named workflows that are loaded only when
|
||||||
|
invoked explicitly, so they never override the order above. They are how a review or audit
|
||||||
|
is *started*, not a source of rules.
|
||||||
|
|
||||||
|
`docs/reviews/**` is **not** canonical. Those are dated, opinionated snapshots that were
|
||||||
|
accurate when written and may since have been fixed, superseded, or found wrong.
|
||||||
|
|
||||||
|
## Layout
|
||||||
|
|
||||||
|
| Path | Role |
|
||||||
|
| :--- | :--- |
|
||||||
|
| `src/transcription/ui/**`, `api/**` | Interface. No direct persistence access. |
|
||||||
|
| `src/transcription/services/**` | Domain logic and transaction ownership. |
|
||||||
|
| `src/transcription/db/**` | Models and persistence. |
|
||||||
|
| `src/transcription/providers/**` | Provider adapters; provider details stop here. |
|
||||||
|
| `src/transcription/worker.py` | Asyncio worker loop. |
|
||||||
|
| `tests/` | Includes boundary/contract guards, not just behavior tests. |
|
||||||
|
|
||||||
|
## Enforced Boundaries
|
||||||
|
|
||||||
|
These are not conventions — a test fails if you break them:
|
||||||
|
|
||||||
|
- **No service-to-service imports** (`test_service_boundaries.py`). Compose in the caller.
|
||||||
|
- **No persistence access from pages/components** (`test_ui_boundaries.py`, allowlist-based).
|
||||||
|
- **No hand-rolled error notifications in UI** — use the shared error presenter.
|
||||||
|
- **No stringly-typed status literals** — use the enums (`test_model_contract_guards.py`).
|
||||||
|
- **Attempt history is append-only** (`test_evidence_provenance.py`).
|
||||||
|
- **`docs/schema.md` stays field-accurate** with `db/models.py`.
|
||||||
|
- **Orphans are tracked, not tolerated** — `test_orphan_sweep.py` records each retained
|
||||||
|
orphan with rationale in `KNOWN_ORPHANS`.
|
||||||
|
|
||||||
|
## Traps
|
||||||
|
|
||||||
|
Non-obvious things that have already caused real bugs here:
|
||||||
|
|
||||||
|
- **`AppError.message` vs `AppError.detail`.** `message` is user/API-facing and must stay
|
||||||
|
generic — never put exception text or filesystem paths in it. `detail` is internal-only and
|
||||||
|
is what reaches logs and `ExecutionAttempt.error_detail`. Putting root-cause data in
|
||||||
|
`message` leaks; removing it from `detail` silently degrades provenance. See
|
||||||
|
`docs/error_handling.md`.
|
||||||
|
- **Two competing atomicity invariants in `services/workflows.py`.** Intermediate pages must
|
||||||
|
commit individually (durability across a long multi-page job); the *final* page must commit
|
||||||
|
atomically with the terminal job status. Collapsing the batch into one transaction satisfies
|
||||||
|
the second and destroys the first. Both are guarded — `test_workflows_reliability.py` and
|
||||||
|
`tests/integration/test_pipeline_atomicity.py`.
|
||||||
|
- **Shared symbols have more consumers than the obvious one.** Before changing a model field,
|
||||||
|
exception attribute, or helper return value, grep for every consumer including tests.
|
||||||
|
Evidence and logging paths frequently read the same fields the UI does.
|
||||||
|
- **`Tag` is owned by two services, on purpose.** Every other model has exactly one owning
|
||||||
|
service, so the ownership rule reads as absolute — it isn't. `Tag` is a single table reached
|
||||||
|
through two `RegistryService[Tag]` facades, `TagRegistry` (documents) and `PersonTagRegistry`
|
||||||
|
(people), which count usage through `DocumentTag` and `PersonTag` respectively. Changing tag
|
||||||
|
semantics through one facade silently changes the other. Consolidating them under one service
|
||||||
|
is not a cleanup; it makes the other side a cross-aggregate writer.
|
||||||
|
- **Import style:** ruff `isort` runs with `force-single-line = true`. One import per line.
|
||||||
|
- **Latent defects have ordering constraints.** Some code is unreachable only because of a
|
||||||
|
current setting or single-instance deployment. Fix it *before* the change that unblocks it,
|
||||||
|
not after.
|
||||||
|
|
||||||
|
## Change Protocol
|
||||||
|
|
||||||
|
- **Write the failing test first** for behavioral fixes, and confirm it actually fails for the
|
||||||
|
reason you think. Several bugs here were subtle enough that a test written afterward would
|
||||||
|
have passed against the broken code.
|
||||||
|
- **Update docs in the same change** when you alter a contract, behavior, or scope — see
|
||||||
|
`.github/instructions/documentation-sync.instructions.md`.
|
||||||
|
- **Do not commit unless asked.** Making a requested change is not consent to commit it.
|
||||||
|
- **Do not push or open PRs on your own initiative.**
|
||||||
|
- **Scope discipline:** fix what was asked plus what your change genuinely breaks. Pre-existing
|
||||||
|
unrelated issues are a separate conversation.
|
||||||
@@ -13,6 +13,8 @@ RUN uv sync --frozen --no-dev --no-install-project
|
|||||||
|
|
||||||
COPY src ./src
|
COPY src ./src
|
||||||
COPY prompts ./prompts
|
COPY prompts ./prompts
|
||||||
|
COPY tools ./tools
|
||||||
|
COPY deploy ./deploy
|
||||||
RUN uv sync --frozen --no-dev
|
RUN uv sync --frozen --no-dev
|
||||||
|
|
||||||
|
|
||||||
@@ -27,12 +29,18 @@ ENV PYTHONDONTWRITEBYTECODE=1 \
|
|||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN apt-get update \
|
||||||
|
&& apt-get install -y --no-install-recommends postgresql-client \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
RUN groupadd --system --gid 1001 appgroup \
|
RUN groupadd --system --gid 1001 appgroup \
|
||||||
&& useradd --system --uid 1001 --gid appgroup --create-home appuser
|
&& useradd --system --uid 1001 --gid appgroup --create-home appuser
|
||||||
|
|
||||||
COPY --from=builder /app/.venv /app/.venv
|
COPY --from=builder /app/.venv /app/.venv
|
||||||
COPY --from=builder /app/src /app/src
|
COPY --from=builder /app/src /app/src
|
||||||
COPY --from=builder /app/prompts /app/prompts
|
COPY --from=builder /app/prompts /app/prompts
|
||||||
|
COPY --from=builder /app/tools /app/tools
|
||||||
|
COPY --from=builder /app/deploy /app/deploy
|
||||||
|
|
||||||
RUN mkdir -p /app/uploads /app/data \
|
RUN mkdir -p /app/uploads /app/data \
|
||||||
&& chown -R appuser:appgroup /app
|
&& chown -R appuser:appgroup /app
|
||||||
|
|||||||
@@ -22,28 +22,28 @@ uv sync
|
|||||||
|
|
||||||
### 2) Configure environment
|
### 2) Configure environment
|
||||||
|
|
||||||
Create a `.env` file in the project root with the required OpenRouter API key:
|
Create a `.env.production` file in the project root with the required OpenRouter API key:
|
||||||
|
|
||||||
```env
|
```env
|
||||||
OPENROUTER_API_KEY=your_openrouter_api_key
|
OPENROUTER_API_KEY=your_openrouter_api_key
|
||||||
```
|
```
|
||||||
|
|
||||||
Settings are read from CLI arguments first, then environment variables, then `.env`, then the defaults below.
|
Settings are read from CLI arguments first, then environment variables, then `.env.production`, then the defaults below.
|
||||||
|
|
||||||
### Configuration Source Precedence
|
### Configuration Source Precedence
|
||||||
|
|
||||||
When the same setting is provided in multiple places, the value is chosen in this order (highest priority first):
|
When the same setting is provided in multiple places, the value is chosen in this order (highest priority first):
|
||||||
|
|
||||||
1. CLI arguments (for example `--port 9999`)
|
1. CLI arguments (for example `--port 8000`)
|
||||||
2. Settings constructor arguments (used mainly in tests)
|
2. Settings constructor arguments (used mainly in tests)
|
||||||
3. Environment variables
|
3. Environment variables
|
||||||
4. `.env` file values
|
4. `.env.production` file values
|
||||||
5. Model defaults in code
|
5. Model defaults in code
|
||||||
|
|
||||||
Practical examples:
|
Practical examples:
|
||||||
|
|
||||||
- `--port 9999` overrides both `PORT=8000` in the shell and `PORT=7000` in `.env`.
|
- `--port 8000` overrides both `PORT=8000` in the shell and `PORT=7000` in `.env.production`.
|
||||||
- `DATABASE__PATH=prod.db` in the shell overrides `DATABASE__PATH=dev.db` in `.env`.
|
- `DATABASE__PATH=prod.db` in the shell overrides `DATABASE__PATH=dev.db` in `.env.production`.
|
||||||
|
|
||||||
#### Server and runtime
|
#### Server and runtime
|
||||||
|
|
||||||
@@ -54,6 +54,7 @@ Practical examples:
|
|||||||
| `LOG_LEVEL` | `info` | Uvicorn and application log level. |
|
| `LOG_LEVEL` | `info` | Uvicorn and application log level. |
|
||||||
| `RELOAD` | `false` | Restart the development server when source files change. |
|
| `RELOAD` | `false` | Restart the development server when source files change. |
|
||||||
| `ENVIRONMENT` | `development` | Runtime environment: `development`, `test`, or `production`. |
|
| `ENVIRONMENT` | `development` | Runtime environment: `development`, `test`, or `production`. |
|
||||||
|
| `RUN_EMBEDDED_WORKER` | `true` | Run worker loop inside web app process. Set `false` when using a dedicated worker service. |
|
||||||
|
|
||||||
#### Provider
|
#### Provider
|
||||||
|
|
||||||
@@ -92,7 +93,7 @@ DATABASE__USER=postgres
|
|||||||
DATABASE__PASSWORD=change-me
|
DATABASE__PASSWORD=change-me
|
||||||
```
|
```
|
||||||
|
|
||||||
This uses Pydantic nested settings (`env_nested_delimiter='__'`) and avoids JSON blobs in `.env`. A top-level `DATABASE={...}` JSON value is still supported as a fallback, and nested keys such as `DATABASE__PATH` take precedence over conflicting JSON keys.
|
This uses Pydantic nested settings (`env_nested_delimiter='__'`) and avoids JSON blobs in env files. A top-level `DATABASE={...}` JSON value is still supported as a fallback, and nested keys such as `DATABASE__PATH` take precedence over conflicting JSON keys.
|
||||||
|
|
||||||
`BOOTSTRAP_SCHEMA_ON_STARTUP` creates missing tables when the app starts. When unset, it is enabled in `development` and `test`, and disabled in `production`; set it explicitly to override that policy. `SQLITE_CHECK_SAME_THREAD` defaults to `false`.
|
`BOOTSTRAP_SCHEMA_ON_STARTUP` creates missing tables when the app starts. When unset, it is enabled in `development` and `test`, and disabled in `production`; set it explicitly to override that policy. `SQLITE_CHECK_SAME_THREAD` defaults to `false`.
|
||||||
|
|
||||||
@@ -110,18 +111,45 @@ WORKER_FAIL_ON_FINISH_REASON_LENGTH=false
|
|||||||
### 3) Run the app
|
### 3) Run the app
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
uv run python -m transcription --port 9999 --reload --database.driver sqlite --bootstrap-schema-on-startup
|
uv run python -m transcription --port 8000 --reload --database.driver sqlite --bootstrap-schema-on-startup
|
||||||
```
|
```
|
||||||
|
|
||||||
This starts the development server with SQLite, creates missing tables, and enables automatic reload. Run `uv run python -m transcription --help` for all CLI options; CLI names use kebab case and nested database options use dot notation, such as `--database.path ./data/transcription.db`.
|
This starts the development server with SQLite, creates missing tables, and enables automatic reload. Run `uv run python -m transcription --help` for all CLI options; CLI names use kebab case and nested database options use dot notation, such as `--database.path ./data/transcription.db`.
|
||||||
|
|
||||||
### 4) Open in browser
|
### 4) Open in browser
|
||||||
|
|
||||||
- GUI: [http://localhost:9999/ui](http://localhost:9999/ui)
|
- GUI: [http://localhost:8000/ui](http://localhost:8000/ui)
|
||||||
- Health check: [http://localhost:9999/healthz](http://localhost:9999/healthz)
|
- Health check: [http://localhost:8000/healthz](http://localhost:8000/healthz)
|
||||||
|
|
||||||
Replace `localhost` with the server's hostname or IP address when connecting from another machine.
|
Replace `localhost` with the server's hostname or IP address when connecting from another machine.
|
||||||
|
|
||||||
|
## Production stack (Phase 1)
|
||||||
|
|
||||||
|
Use the production compose profile for split app/worker deployment with PostgreSQL and Cloudflare Tunnel:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
copy .env.production.example .env.production
|
||||||
|
docker compose -f docker-compose.production.yml up -d --build
|
||||||
|
```
|
||||||
|
|
||||||
|
Services:
|
||||||
|
|
||||||
|
- `app`: FastAPI + NiceGUI runtime (`RUN_EMBEDDED_WORKER=false`)
|
||||||
|
- `worker`: standalone queue processor (`python -m transcription.worker_service`)
|
||||||
|
- `postgres`: primary datastore
|
||||||
|
- `cloudflared`: tunnel client using mounted ingress config + `CLOUDFLARE_TUNNEL_TOKEN`
|
||||||
|
|
||||||
|
Operational defaults in the production compose file:
|
||||||
|
|
||||||
|
- worker healthcheck is disabled (the worker process has no HTTP `/healthz` endpoint)
|
||||||
|
- cloudflared is pinned to HTTP/2 with explicit DNS resolvers (`1.1.1.1`, `1.0.0.1`) for restricted LXC/container DNS environments
|
||||||
|
|
||||||
|
Cloudflare setup files:
|
||||||
|
|
||||||
|
1. `copy deploy\cloudflared\config.yml.example deploy\cloudflared\config.yml`
|
||||||
|
2. set `CLOUDFLARE_TUNNEL_TOKEN` in `.env.production`
|
||||||
|
3. update ingress hostnames in `deploy\cloudflared\config.yml`
|
||||||
|
|
||||||
## How to navigate the GUI
|
## How to navigate the GUI
|
||||||
|
|
||||||
- **Upload page** (`/ui`)
|
- **Upload page** (`/ui`)
|
||||||
@@ -147,6 +175,15 @@ not a path. Each job snapshots the validated prompt text, SHA-256 hash, and samp
|
|||||||
The canonical MVP prompt is:
|
The canonical MVP prompt is:
|
||||||
- `prompts/transcribe_document.md`
|
- `prompts/transcribe_document.md`
|
||||||
|
|
||||||
|
## Database migration workflow
|
||||||
|
|
||||||
|
Schema upgrades use an explicit export/import rebuild flow (no runtime legacy write compatibility).
|
||||||
|
See `docs/data_migration.md` for commands and cutover steps.
|
||||||
|
|
||||||
|
## Backup and restore workflow
|
||||||
|
|
||||||
|
Production backup/restore (PostgreSQL + uploads + deployment config) steps are documented in `docs/backup_restore.md`.
|
||||||
|
|
||||||
## Destructive test procedure (with data backup)
|
## Destructive test procedure (with data backup)
|
||||||
|
|
||||||
AI execution policy: before the first unit-test run in a test/fix cycle, create one backup of `./data`. Reuse that same backup for every subsequent test run in the cycle. After tests succeed, always pause and ask whether to restore now.
|
AI execution policy: before the first unit-test run in a test/fix cycle, create one backup of `./data`. Reuse that same backup for every subsequent test run in the cycle. After tests succeed, always pause and ask whether to restore now.
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
BACKUP_DIR="${BACKUP_DIR:-./data/backups}"
|
||||||
|
RETENTION_DAYS="${BACKUP_RETENTION_DAYS:-14}"
|
||||||
|
UPLOAD_DIR="${UPLOAD_DIR:-/app/uploads}"
|
||||||
|
PROMPT_DIR="${PROMPT_DIR:-/app/prompts}"
|
||||||
|
DATABASE_DRIVER="${DATABASE__DRIVER:-postgres}"
|
||||||
|
DATABASE_HOST="${DATABASE__HOST:-postgres}"
|
||||||
|
DATABASE_PORT="${DATABASE__PORT:-5432}"
|
||||||
|
DATABASE_NAME="${DATABASE__DATABASE:-}"
|
||||||
|
DATABASE_USER="${DATABASE__USER:-}"
|
||||||
|
DATABASE_PASSWORD="${DATABASE__PASSWORD:-}"
|
||||||
|
|
||||||
|
timestamp="$(date -u +%Y%m%d-%H%M%S)"
|
||||||
|
postgres_file="postgres-${timestamp}.dump"
|
||||||
|
manifest_file="backup-${timestamp}.manifest"
|
||||||
|
|
||||||
|
mkdir -p "${BACKUP_DIR}"
|
||||||
|
if [ "${DATABASE_DRIVER}" != "postgres" ]; then
|
||||||
|
echo "create_postgres_backup.sh requires DATABASE__DRIVER=postgres." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [ -z "${DATABASE_NAME}" ] || [ -z "${DATABASE_USER}" ] || [ -z "${DATABASE_PASSWORD}" ]; then
|
||||||
|
echo "DATABASE__DATABASE, DATABASE__USER, and DATABASE__PASSWORD must be set." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if ! command -v pg_dump >/dev/null 2>&1; then
|
||||||
|
echo "pg_dump is not installed in this environment." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
PGPASSWORD="${DATABASE_PASSWORD}" pg_dump \
|
||||||
|
-h "${DATABASE_HOST}" \
|
||||||
|
-p "${DATABASE_PORT}" \
|
||||||
|
-U "${DATABASE_USER}" \
|
||||||
|
-d "${DATABASE_NAME}" \
|
||||||
|
-Fc \
|
||||||
|
> "${BACKUP_DIR}/${postgres_file}"
|
||||||
|
|
||||||
|
cat > "${BACKUP_DIR}/${manifest_file}" <<EOF
|
||||||
|
created_at_utc=${timestamp}
|
||||||
|
postgres_dump=${postgres_file}
|
||||||
|
backup_dir=${BACKUP_DIR}
|
||||||
|
uploads_backup_dir=${BACKUP_DIR}/uploads
|
||||||
|
prompts_backup_dir=${BACKUP_DIR}/prompts
|
||||||
|
EOF
|
||||||
|
|
||||||
|
find "${BACKUP_DIR}" -type f \( \
|
||||||
|
-name 'postgres-*.dump' -o \
|
||||||
|
-name 'backup-*.manifest' \
|
||||||
|
\) -mtime +"${RETENTION_DAYS}" -delete
|
||||||
|
|
||||||
|
uploads_backup_dir="${BACKUP_DIR}/uploads"
|
||||||
|
mkdir -p "${uploads_backup_dir}"
|
||||||
|
if [ -d "${UPLOAD_DIR}" ]; then
|
||||||
|
cp -an "${UPLOAD_DIR}/." "${uploads_backup_dir}/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
prompts_backup_dir="${BACKUP_DIR}/prompts"
|
||||||
|
mkdir -p "${prompts_backup_dir}"
|
||||||
|
if [ -d "${PROMPT_DIR}" ]; then
|
||||||
|
cp -a "${PROMPT_DIR}/." "${prompts_backup_dir}/"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Created backup set:"
|
||||||
|
echo " ${BACKUP_DIR}/${postgres_file}"
|
||||||
|
echo " ${BACKUP_DIR}/${manifest_file}"
|
||||||
|
echo " ${uploads_backup_dir}/ (incremental uploads mirror)"
|
||||||
|
echo " ${prompts_backup_dir}/ (prompts mirror)"
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
# Example only. Copy to a local script and replace placeholder values.
|
||||||
|
# Do NOT commit secrets.
|
||||||
|
|
||||||
|
SHARE="//nas-host-or-ip/share-name"
|
||||||
|
MOUNT_POINT="/mnt/nas-backups"
|
||||||
|
CREDENTIALS_FILE="/etc/samba/credentials/nas-share-credentials"
|
||||||
|
USERNAME="replace-with-nas-user"
|
||||||
|
PASSWORD="replace-with-nas-password"
|
||||||
|
|
||||||
|
if [ "${USERNAME}" = "replace-with-nas-user" ] || [ "${PASSWORD}" = "replace-with-nas-password" ]; then
|
||||||
|
echo "Edit USERNAME and PASSWORD placeholders before running this script."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p "${MOUNT_POINT}"
|
||||||
|
mkdir -p "$(dirname "${CREDENTIALS_FILE}")"
|
||||||
|
|
||||||
|
cat > "${CREDENTIALS_FILE}" <<'EOF'
|
||||||
|
username=__USERNAME__
|
||||||
|
password=__PASSWORD__
|
||||||
|
EOF
|
||||||
|
sed -i "s|__USERNAME__|${USERNAME}|g" "${CREDENTIALS_FILE}"
|
||||||
|
sed -i "s|__PASSWORD__|${PASSWORD}|g" "${CREDENTIALS_FILE}"
|
||||||
|
chmod 600 "${CREDENTIALS_FILE}"
|
||||||
|
|
||||||
|
mount -t cifs "${SHARE}" "${MOUNT_POINT}" \
|
||||||
|
-o "credentials=${CREDENTIALS_FILE},vers=3.0,iocharset=utf8,uid=0,gid=0,file_mode=0600,dir_mode=0700"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Mounted ${SHARE} at ${MOUNT_POINT}"
|
||||||
|
echo ""
|
||||||
|
echo "To persist across reboot, add this line to /etc/fstab:"
|
||||||
|
echo "${SHARE} ${MOUNT_POINT} cifs credentials=${CREDENTIALS_FILE},vers=3.0,iocharset=utf8,uid=0,gid=0,file_mode=0600,dir_mode=0700,_netdev,nofail,x-systemd.automount 0 0"
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
if [ "$#" -lt 1 ]; then
|
||||||
|
echo "Usage: $0 <path-to-postgres-dump>"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
dump_file="$1"
|
||||||
|
COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.production.yml}"
|
||||||
|
ENV_FILE="${ENV_FILE:-.env.production}"
|
||||||
|
SYNOLOGY_BACKUP_DIR="${SYNOLOGY_BACKUP_DIR:-}"
|
||||||
|
|
||||||
|
if [ ! -f "${dump_file}" ]; then
|
||||||
|
echo "Backup file not found: ${dump_file}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
backup_dir="$(dirname "${dump_file}")"
|
||||||
|
backup_name="$(basename "${dump_file}")"
|
||||||
|
timestamp="$(printf '%s' "${backup_name}" | sed -n 's/^postgres-\([0-9]\{8\}-[0-9]\{6\}\)\.dump$/\1/p')"
|
||||||
|
uploads_file=""
|
||||||
|
config_file=""
|
||||||
|
|
||||||
|
# If SYNOLOGY_BACKUP_DIR wasn't exported in the shell, read it from ENV_FILE.
|
||||||
|
if [ -z "${SYNOLOGY_BACKUP_DIR}" ] && [ -f "${ENV_FILE}" ]; then
|
||||||
|
SYNOLOGY_BACKUP_DIR="$(
|
||||||
|
sed -n 's/^SYNOLOGY_BACKUP_DIR=//p' "${ENV_FILE}" | tail -n 1
|
||||||
|
)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "${timestamp}" ]; then
|
||||||
|
# Legacy local full-archive naming.
|
||||||
|
candidate_uploads="${backup_dir}/uploads-${timestamp}.tar.gz"
|
||||||
|
candidate_config="${backup_dir}/config-${timestamp}.tar.gz"
|
||||||
|
if [ -f "${candidate_uploads}" ]; then
|
||||||
|
uploads_file="${candidate_uploads}"
|
||||||
|
fi
|
||||||
|
if [ -f "${candidate_config}" ]; then
|
||||||
|
config_file="${candidate_config}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
docker compose --env-file "${ENV_FILE}" -f "${COMPOSE_FILE}" exec -T postgres sh -lc \
|
||||||
|
"PGPASSWORD=\"\$POSTGRES_PASSWORD\" psql -U \"\$POSTGRES_USER\" -d postgres -c \"DROP DATABASE IF EXISTS \\\"\$POSTGRES_DB\\\";\""
|
||||||
|
|
||||||
|
docker compose --env-file "${ENV_FILE}" -f "${COMPOSE_FILE}" exec -T postgres sh -lc \
|
||||||
|
"PGPASSWORD=\"\$POSTGRES_PASSWORD\" psql -U \"\$POSTGRES_USER\" -d postgres -c \"CREATE DATABASE \\\"\$POSTGRES_DB\\\";\""
|
||||||
|
|
||||||
|
cat "${dump_file}" | docker compose --env-file "${ENV_FILE}" -f "${COMPOSE_FILE}" exec -T postgres sh -lc \
|
||||||
|
"PGPASSWORD=\"\$POSTGRES_PASSWORD\" pg_restore -U \"\$POSTGRES_USER\" -d \"\$POSTGRES_DB\" --clean --if-exists --no-owner --no-privileges"
|
||||||
|
|
||||||
|
if [ -n "${SYNOLOGY_BACKUP_DIR}" ] && [ -d "${SYNOLOGY_BACKUP_DIR}/uploads" ]; then
|
||||||
|
docker compose --env-file "${ENV_FILE}" -f "${COMPOSE_FILE}" run --rm --no-deps \
|
||||||
|
-v "${SYNOLOGY_BACKUP_DIR}:/backup" \
|
||||||
|
--entrypoint sh app -lc \
|
||||||
|
"mkdir -p /app/uploads/documents /app/uploads/photos && \
|
||||||
|
find /app/uploads/documents -mindepth 1 -delete && \
|
||||||
|
find /app/uploads/photos -mindepth 1 -delete && \
|
||||||
|
if [ -d /backup/uploads/documents ]; then cp -a /backup/uploads/documents/. /app/uploads/documents/; fi && \
|
||||||
|
if [ -d /backup/uploads/photos ]; then cp -a /backup/uploads/photos/. /app/uploads/photos/; fi && \
|
||||||
|
if [ -f /backup/uploads/homepage.md ]; then cp /backup/uploads/homepage.md /app/uploads/homepage.md; else rm -f /app/uploads/homepage.md; fi"
|
||||||
|
elif [ -n "${uploads_file}" ]; then
|
||||||
|
# Legacy local full-archive restore.
|
||||||
|
cat "${uploads_file}" | docker compose --env-file "${ENV_FILE}" -f "${COMPOSE_FILE}" run --rm --no-deps --entrypoint sh app -lc \
|
||||||
|
"mkdir -p /app/uploads && find /app/uploads -mindepth 1 -delete && tar -xzf - -C /app/uploads"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "${SYNOLOGY_BACKUP_DIR}" ] && [ -n "${timestamp}" ] && [ -f "${SYNOLOGY_BACKUP_DIR}/config-${timestamp}.tar.gz" ]; then
|
||||||
|
tar -xzf "${SYNOLOGY_BACKUP_DIR}/config-${timestamp}.tar.gz" -C .
|
||||||
|
elif [ -n "${config_file}" ]; then
|
||||||
|
# Legacy local full-archive restore.
|
||||||
|
tar -xzf "${config_file}" -C .
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Restore complete from: ${dump_file}"
|
||||||
|
if [ -n "${SYNOLOGY_BACKUP_DIR}" ] && [ -d "${SYNOLOGY_BACKUP_DIR}/uploads" ]; then
|
||||||
|
echo "Restored uploads mirror from: ${SYNOLOGY_BACKUP_DIR}/uploads"
|
||||||
|
elif [ -n "${uploads_file}" ]; then
|
||||||
|
echo "Restored uploads archive: ${uploads_file}"
|
||||||
|
fi
|
||||||
|
if [ -n "${SYNOLOGY_BACKUP_DIR}" ] && [ -n "${timestamp}" ] && [ -f "${SYNOLOGY_BACKUP_DIR}/config-${timestamp}.tar.gz" ]; then
|
||||||
|
echo "Restored config archive: ${SYNOLOGY_BACKUP_DIR}/config-${timestamp}.tar.gz"
|
||||||
|
elif [ -n "${config_file}" ]; then
|
||||||
|
echo "Restored config archive: ${config_file}"
|
||||||
|
fi
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
ingress:
|
||||||
|
# Primary transcription app endpoint.
|
||||||
|
- hostname: transcription.example.com
|
||||||
|
service: http://app:8000
|
||||||
|
|
||||||
|
# Optional: generic remote access endpoints for other internal services.
|
||||||
|
# Replace hostnames and targets for your LAN.
|
||||||
|
- hostname: homeassistant.example.com
|
||||||
|
service: http://192.168.1.50:8123
|
||||||
|
- hostname: pihole.example.com
|
||||||
|
service: http://192.168.1.60:80
|
||||||
|
|
||||||
|
# Required catch-all.
|
||||||
|
- service: http_status:404
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
services:
|
||||||
|
app:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
image: transcription:prod
|
||||||
|
env_file:
|
||||||
|
- .env.production
|
||||||
|
environment:
|
||||||
|
RUN_EMBEDDED_WORKER: "false"
|
||||||
|
RUNTIME_SETTINGS_ENV_FILE: "/app/.env.production"
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
ports:
|
||||||
|
- "8000:8000"
|
||||||
|
volumes:
|
||||||
|
- app_uploads:/app/uploads
|
||||||
|
- app_data:/app/data
|
||||||
|
- ./backup:/backup
|
||||||
|
- ./.env.production:/app/.env.production
|
||||||
|
- ./prompts:/app/prompts:ro
|
||||||
|
restart: unless-stopped
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/healthz')"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
start_period: 20s
|
||||||
|
|
||||||
|
worker:
|
||||||
|
image: transcription:prod
|
||||||
|
env_file:
|
||||||
|
- .env.production
|
||||||
|
command: ["python", "-m", "transcription.worker_service"]
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
volumes:
|
||||||
|
- app_uploads:/app/uploads
|
||||||
|
- app_data:/app/data
|
||||||
|
- ./backup:/backup
|
||||||
|
- ./.env.production:/app/.env.production:ro
|
||||||
|
- ./prompts:/app/prompts:ro
|
||||||
|
healthcheck:
|
||||||
|
disable: true
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
postgres:
|
||||||
|
image: postgres:16-alpine
|
||||||
|
env_file:
|
||||||
|
- .env.production
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: ${DATABASE__DATABASE}
|
||||||
|
POSTGRES_USER: ${DATABASE__USER}
|
||||||
|
POSTGRES_PASSWORD: ${DATABASE__PASSWORD}
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
restart: unless-stopped
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 10
|
||||||
|
start_period: 10s
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres_data:
|
||||||
|
app_uploads:
|
||||||
|
app_data:
|
||||||
+5
-2
@@ -5,9 +5,12 @@ services:
|
|||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
container_name: transcription-app
|
container_name: transcription-app
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env.production
|
||||||
environment:
|
environment:
|
||||||
DATABASE_URL: sqlite:////app/data/transcription.db
|
# Database configuration uses nested settings names (env_nested_delimiter="__").
|
||||||
|
# DATABASE_URL is NOT read by the application and must not be used here.
|
||||||
|
DATABASE__DRIVER: sqlite
|
||||||
|
DATABASE__PATH: /app/data/transcription.db
|
||||||
UPLOAD_DIR: /app/uploads
|
UPLOAD_DIR: /app/uploads
|
||||||
PROMPT_DIR: /app/prompts
|
PROMPT_DIR: /app/prompts
|
||||||
ports:
|
ports:
|
||||||
|
|||||||
@@ -0,0 +1,230 @@
|
|||||||
|
# System Architecture (Current Baseline: V6.1)
|
||||||
|
|
||||||
|
This document defines the current V6.1 architecture baseline.
|
||||||
|
|
||||||
|
## Architecture Objectives
|
||||||
|
|
||||||
|
- Preserve durable archival records for Documents, Sources, People, and processing runs.
|
||||||
|
- Execute page transcription asynchronously with bounded worker behavior.
|
||||||
|
- Preserve append-only machine-attempt evidence with request/response provenance.
|
||||||
|
- Keep UI, API, service, persistence, and provider boundaries explicit and testable.
|
||||||
|
|
||||||
|
## Technical Stack
|
||||||
|
|
||||||
|
- **Runtime:** Python 3.12+
|
||||||
|
- **Web application:** FastAPI + NiceGUI
|
||||||
|
- **Persistence:** SQLModel / SQLAlchemy — PostgreSQL in production, SQLite for local development and tests
|
||||||
|
- **Validation and settings:** Pydantic V2 + pydantic-settings
|
||||||
|
- **Concurrency:** asyncio worker loop
|
||||||
|
- **Provider integration:** OpenRouter adapter behind provider interface
|
||||||
|
- **Deployment:** Docker Compose (app, worker, PostgreSQL, Cloudflare Tunnel)
|
||||||
|
- **Quality and tests:** Ruff, ty, pytest, pytest-asyncio
|
||||||
|
|
||||||
|
## Runtime Topology
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
flowchart LR
|
||||||
|
U[Browser User] --> A[FastAPI + NiceGUI App]
|
||||||
|
A --> W[Asyncio Worker]
|
||||||
|
A --> DB[(PostgreSQL / SQLite)]
|
||||||
|
W --> P[Provider Adapter]
|
||||||
|
W --> DB
|
||||||
|
```
|
||||||
|
|
||||||
|
The worker loop drains two queues in the same pass: queued transcription Jobs and queued
|
||||||
|
`MaintenanceRun` records. When neither has work, it idles.
|
||||||
|
|
||||||
|
### Production deployment
|
||||||
|
|
||||||
|
Production runs as a Docker Compose stack with the app and worker as separate services, so the app
|
||||||
|
process runs with `RUN_EMBEDDED_WORKER=false` and the worker process owns queue draining. Local
|
||||||
|
development runs a single process with the worker embedded.
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
flowchart LR
|
||||||
|
I[Internet] --> CF[cloudflared tunnel + Access]
|
||||||
|
CF --> APP[app service]
|
||||||
|
APP --> PG[(postgres service)]
|
||||||
|
WK[worker service] --> PG
|
||||||
|
WK --> PROV[OpenRouter]
|
||||||
|
```
|
||||||
|
|
||||||
|
Deployment, rollback, and recovery procedures are in [Production Runbook](production-runbook.md);
|
||||||
|
backup configuration and restore are in [Backup and Restore](backup_restore.md).
|
||||||
|
|
||||||
|
## Layered Boundaries
|
||||||
|
|
||||||
|
### Interface Layer
|
||||||
|
|
||||||
|
- `src/transcription/ui/**`
|
||||||
|
- `src/transcription/api/**`
|
||||||
|
|
||||||
|
Responsibilities:
|
||||||
|
|
||||||
|
- Route registration, page orchestration, presentation adapters.
|
||||||
|
- Structured user messaging through shared error presenter.
|
||||||
|
- No direct persistence access from pages/components.
|
||||||
|
|
||||||
|
### Service and Orchestration Layer
|
||||||
|
|
||||||
|
Aggregate services:
|
||||||
|
|
||||||
|
- `src/transcription/services/documents.py`
|
||||||
|
- `src/transcription/services/people.py`
|
||||||
|
- `src/transcription/services/jobs.py`
|
||||||
|
- `src/transcription/services/sources.py`
|
||||||
|
- `src/transcription/services/photos.py`
|
||||||
|
- `src/transcription/services/maintenance.py`
|
||||||
|
- `src/transcription/services/evidence.py` (read/projection only)
|
||||||
|
|
||||||
|
Orchestration modules:
|
||||||
|
|
||||||
|
- `src/transcription/services/store.py`
|
||||||
|
- `src/transcription/services/workflows.py`
|
||||||
|
|
||||||
|
Responsibilities:
|
||||||
|
|
||||||
|
- Aggregate ownership and invariants.
|
||||||
|
- Transaction-aware write helpers.
|
||||||
|
- Cross-service workflows in orchestration modules (`store.py`, `workflows.py`).
|
||||||
|
- Lookup-table CRUD through the generic `RegistryService` base (`registry.py`), which is not an
|
||||||
|
aggregate owner itself.
|
||||||
|
|
||||||
|
Module classification and per-model ownership are defined in
|
||||||
|
[services instructions](../.github/instructions/services.instructions.md).
|
||||||
|
|
||||||
|
### Persistence Layer
|
||||||
|
|
||||||
|
- `src/transcription/db/**`
|
||||||
|
|
||||||
|
Responsibilities:
|
||||||
|
|
||||||
|
- SQLModel definitions, async session/engine runtime, registry bootstrap.
|
||||||
|
- Loader helpers that enforce explicit eager loading with `lazy="raise"` relationships.
|
||||||
|
|
||||||
|
### Provider Layer
|
||||||
|
|
||||||
|
- `src/transcription/providers/**`
|
||||||
|
|
||||||
|
Responsibilities:
|
||||||
|
|
||||||
|
- Provider API encapsulation.
|
||||||
|
- Request manifest and transport evidence capture.
|
||||||
|
- Normalized transcription result contract.
|
||||||
|
|
||||||
|
## Core Domain Model
|
||||||
|
|
||||||
|
- `Document` owns archival metadata and links to `Source`, `Job`, and `DocumentPerson`.
|
||||||
|
- `Source` is a document page/file record with selected machine projection and human revision.
|
||||||
|
- `Job` is an aggregate processing run with status and frozen prompt/runtime settings.
|
||||||
|
- `JobSource` is queue/membership state for one `(job, source)` pair.
|
||||||
|
- `ExecutionAttempt` is append-only evidence for each provider call.
|
||||||
|
- `Photo` is person imagery owned by `PhotosService`.
|
||||||
|
- `MaintenanceRun` is one queued or executed operational maintenance run.
|
||||||
|
- `GenealogyPerson`, `GenealogyFamily`, `GenealogyFamilyChild`, and `GenealogyCitation` store
|
||||||
|
imported GEDCOM genealogy data and citation provenance.
|
||||||
|
- `DocumentType` and `PersonRole` are UUID-backed registries with optional protected `semantic_key`.
|
||||||
|
- `Tag` is a shared registry reached through both document and person tagging, linked by
|
||||||
|
`DocumentTag` and `PersonTag`.
|
||||||
|
|
||||||
|
## Processing and Evidence Workflow
|
||||||
|
|
||||||
|
1. User creates/updates Document metadata and linked People atomically through workflow orchestration.
|
||||||
|
2. User creates a Job by uploading one or more Source files or by retranscribing an existing Source.
|
||||||
|
3. Source files are validated and stored; orientation normalization may be applied at ingest, and stored bytes become the canonical processing bytes.
|
||||||
|
4. Worker claims queued Job, transitions to `processing`, and processes pending pages in deterministic order.
|
||||||
|
5. Each provider call writes one immutable `ExecutionAttempt` with:
|
||||||
|
- request manifest + hash
|
||||||
|
- transport evidence (when response exists)
|
||||||
|
- SDK snapshot and normalized metadata
|
||||||
|
- outcome, timing, and error details when applicable
|
||||||
|
6. `JobSource` status is updated as queue/projection state; `Source.raw_transcription` is set on first successful attempt and can be explicitly re-pointed by candidate promotion.
|
||||||
|
7. Job terminal status resolves to `transcribed`, `partial_success`, or `failed`.
|
||||||
|
|
||||||
|
## Status Semantics
|
||||||
|
|
||||||
|
- **Job statuses:** `queued`, `processing`, `transcribed`, `partial_success`, `failed`
|
||||||
|
- Operational success path resolves to `transcribed`.
|
||||||
|
- **JobSource statuses:** `pending`, `transcribed`, `failed`, `cancelled`
|
||||||
|
- **MaintenanceRun statuses:** `queued`, `processing`, `succeeded`, `failed`
|
||||||
|
- Maintenance uses `succeeded` rather than `transcribed`; the transcription vocabulary does not
|
||||||
|
apply to operational runs.
|
||||||
|
- **Maintenance job types:** `backup`, `storage_reconciliation`, `gedcom_import`
|
||||||
|
|
||||||
|
## Maintenance Execution
|
||||||
|
|
||||||
|
Operational maintenance is queue-backed rather than run inline from the UI, so it survives request
|
||||||
|
lifetime and is recorded:
|
||||||
|
|
||||||
|
1. Settings enqueues a `MaintenanceRun` with `status=queued` and a `triggered_by` marker.
|
||||||
|
2. The worker claims the oldest queued run with a conditional update, moving it to `processing`.
|
||||||
|
3. `backup` runs the deploy backup script; `storage_reconciliation` compares stored media against
|
||||||
|
`Document`/`Source` records; `gedcom_import` parses the latest uploaded `.ged` file and upserts
|
||||||
|
genealogy records.
|
||||||
|
4. The run finalizes to `succeeded` or `failed` with summary, timing, log path, and `error_detail`.
|
||||||
|
|
||||||
|
`MaintenanceRun` records operational history and is not evidence in the `ExecutionAttempt` sense;
|
||||||
|
append-only guarantees apply to transcription attempts.
|
||||||
|
|
||||||
|
## Security and Path Handling Boundaries
|
||||||
|
|
||||||
|
- Print media delivery uses record-validated API route:
|
||||||
|
- `src/transcription/api/print_api.py`
|
||||||
|
- General UI media links resolve through:
|
||||||
|
- `src/transcription/ui/components/media_urls.py`
|
||||||
|
- Local filesystem paths must never be accepted from user input as trusted media routes.
|
||||||
|
|
||||||
|
## Concurrency and Reliability Principles
|
||||||
|
|
||||||
|
- Worker loop reuses service bundle/provider resources for pooled calls.
|
||||||
|
- Provider-call timeout is explicit and bounded.
|
||||||
|
- Non-retriable worker-loop faults are surfaced and stop loop spin.
|
||||||
|
- Per-page outcomes are durably persisted before processing next page.
|
||||||
|
|
||||||
|
## Design Decisions and Rationale
|
||||||
|
|
||||||
|
### Why `transcribed` is the success terminal state
|
||||||
|
|
||||||
|
- The worker and job orchestration resolve successful completion to `JobStatus.TRANSCRIBED`, with mixed and failure outcomes represented by `partial_success` and `failed`.
|
||||||
|
- This keeps terminal status vocabulary aligned with what the pipeline actually produces: transcribed page content and evidence, not a generic completion marker.
|
||||||
|
|
||||||
|
### Why evidence history is append-only while page text is a projection
|
||||||
|
|
||||||
|
- `ExecutionAttempt` stores immutable per-call evidence and preserves full attempt history across retries.
|
||||||
|
- `Source.raw_transcription` is intentionally a mutable projection so UI and exports can show a selected current machine text without mutating historical evidence.
|
||||||
|
- This split keeps auditability and UX both first-class: history is durable, presentation is editable.
|
||||||
|
|
||||||
|
### Why orchestration modules own cross-service workflows
|
||||||
|
|
||||||
|
- Service modules do not import each other; aggregate ownership remains local to each service.
|
||||||
|
- Multi-aggregate writes are coordinated in orchestration modules (`store.py`, `workflows.py`) so transaction boundaries are explicit and testable.
|
||||||
|
- This avoids circular dependencies and keeps cross-cutting workflow logic centralized.
|
||||||
|
|
||||||
|
### Why explicit eager loading is required
|
||||||
|
|
||||||
|
- ORM relationships are configured with `lazy="raise"` in key paths, so code must request needed relationships up front.
|
||||||
|
- This prevents hidden query behavior in UI/service code and makes read shape deterministic and reviewable.
|
||||||
|
|
||||||
|
### Why canonical source bytes may be ingest-normalized
|
||||||
|
|
||||||
|
- Ingest normalization can correct orientation before persistence so provider calls, evidence hashes, and rendered processing source are consistent.
|
||||||
|
- The canonical stored bytes, digest, and size become the durable processing identity for that source.
|
||||||
|
|
||||||
|
### Why media access uses controlled routes/helpers
|
||||||
|
|
||||||
|
- Print/export media uses record-validated API endpoints to avoid direct filesystem path exposure.
|
||||||
|
- General UI media URLs are generated through shared resolver helpers to keep path handling consistent and centralized.
|
||||||
|
|
||||||
|
## Scope Boundary
|
||||||
|
|
||||||
|
Current architecture rules live in `docs/*`.
|
||||||
|
|
||||||
|
## Related References
|
||||||
|
|
||||||
|
- [System Requirements](requirements.md)
|
||||||
|
- [Data Model](schema.md)
|
||||||
|
- [Error Handling Policy](error_handling.md)
|
||||||
|
- [Production Runbook](production-runbook.md)
|
||||||
|
- [Backup and Restore](backup_restore.md)
|
||||||
|
- [Error Handling invariant](./invariant/error_handling.md)
|
||||||
|
- [AI evidence invariant](./invariant/ai_evidence_and_provenance.md)
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
# Backup and Restore (V6.1)
|
||||||
|
|
||||||
|
This guide defines operational backup/restore for clean-slate recovery of the Docker runtime using a host-visible backup folder.
|
||||||
|
|
||||||
|
## 1. Backup artifacts
|
||||||
|
|
||||||
|
- Backup target root: `BACKUP_DIR` (recommended production value: `/backup`)
|
||||||
|
- Database artifact per run:
|
||||||
|
- `postgres-YYYYMMDD-HHMMSS.dump` (PostgreSQL custom dump via `pg_dump -Fc`)
|
||||||
|
- `backup-YYYYMMDD-HHMMSS.manifest` (run manifest)
|
||||||
|
- Media/config mirrors under `BACKUP_DIR`:
|
||||||
|
- `uploads/**` (incremental copy: new files only)
|
||||||
|
- `prompts/**` (prompt directory mirror)
|
||||||
|
|
||||||
|
Retention:
|
||||||
|
|
||||||
|
- `BACKUP_RETENTION_DAYS` applies to `postgres-*.dump` and `backup-*.manifest` files.
|
||||||
|
|
||||||
|
## 2. Creating backups
|
||||||
|
|
||||||
|
Run from repository root:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sh deploy/backup/create_postgres_backup.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
Environment variables used by the backup script:
|
||||||
|
|
||||||
|
- `BACKUP_DIR` (default `./data/backups`)
|
||||||
|
- `BACKUP_RETENTION_DAYS` (default `14`)
|
||||||
|
- `UPLOAD_DIR` (default `/app/uploads`)
|
||||||
|
- `PROMPT_DIR` (default `/app/prompts`)
|
||||||
|
- `DATABASE__DRIVER` (must be `postgres`)
|
||||||
|
- `DATABASE__HOST` (default `postgres`)
|
||||||
|
- `DATABASE__PORT` (default `5432`)
|
||||||
|
- `DATABASE__DATABASE` (required)
|
||||||
|
- `DATABASE__USER` (required)
|
||||||
|
- `DATABASE__PASSWORD` (required)
|
||||||
|
|
||||||
|
Recommended production setup:
|
||||||
|
|
||||||
|
- Mount a host-visible folder into `/backup` for both `app` and `worker`.
|
||||||
|
- Set `BACKUP_DIR=/backup` in `.env.production`.
|
||||||
|
- Use host-level tooling (for example Synology Drive Client on the host) to replicate that folder externally.
|
||||||
|
|
||||||
|
## 3. Restoring from backup
|
||||||
|
|
||||||
|
Restore requires downtime for app + worker writes.
|
||||||
|
|
||||||
|
1. Stop app and worker:
|
||||||
|
- `docker compose --env-file .env.production -f docker-compose.production.yml stop app worker`
|
||||||
|
2. Restore database:
|
||||||
|
- `sh deploy/backup/restore_postgres_backup.sh /backup/postgres-YYYYMMDD-HHMMSS.dump`
|
||||||
|
3. Start app and worker:
|
||||||
|
- `docker compose --env-file .env.production -f docker-compose.production.yml start app worker`
|
||||||
|
4. Validate `/healthz` and run one smoke workflow.
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
- `restore_postgres_backup.sh` still supports legacy archive restore paths for older backup sets.
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
# Cloudflare Tunnel and Access Setup
|
||||||
|
|
||||||
|
This guide defines the repository-supported setup for exposing app and selected LAN services through Cloudflare Tunnel with Cloudflare Access protection.
|
||||||
|
|
||||||
|
## 1. Files used by this deployment
|
||||||
|
|
||||||
|
1. `deploy/cloudflared/config.yml` (local copy from `config.yml.example`)
|
||||||
|
2. `.env.production` (`CLOUDFLARE_TUNNEL_TOKEN`)
|
||||||
|
3. `docker-compose.production.yml` (`cloudflared` service reads token + mounts config)
|
||||||
|
|
||||||
|
Do not commit `config.yml` or `.env.production`.
|
||||||
|
|
||||||
|
## 2. Configure cloudflared
|
||||||
|
|
||||||
|
1. Copy `deploy/cloudflared/config.yml.example` to `deploy/cloudflared/config.yml`.
|
||||||
|
2. Update hostname -> service mappings in `ingress`.
|
||||||
|
3. Keep the final catch-all ingress `http_status:404`.
|
||||||
|
4. Set `CLOUDFLARE_TUNNEL_TOKEN` in `.env.production`.
|
||||||
|
|
||||||
|
Example app route:
|
||||||
|
|
||||||
|
- `transcription.example.com` -> `http://app:8000`
|
||||||
|
|
||||||
|
Optional generic remote-access routes:
|
||||||
|
|
||||||
|
- `homeassistant.example.com` -> `http://<home-assistant-lan-ip>:8123`
|
||||||
|
- `pihole.example.com` -> `http://<pihole-lan-ip>:80`
|
||||||
|
|
||||||
|
## 3. Cloudflare Access policy baseline
|
||||||
|
|
||||||
|
Create one Access app policy per exposed hostname:
|
||||||
|
|
||||||
|
1. Include: your allowed identities/groups only.
|
||||||
|
2. Exclude: none by default.
|
||||||
|
3. Require: identity provider login (and MFA if available).
|
||||||
|
|
||||||
|
Recommended baseline:
|
||||||
|
|
||||||
|
- App endpoint (`transcription.*`): your admin identity set.
|
||||||
|
- Other internal endpoints (`homeassistant.*`, `pihole.*`, etc.): explicit least-privilege groups.
|
||||||
|
|
||||||
|
## 4. Startup
|
||||||
|
|
||||||
|
Start production stack:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose --env-file .env.production -f docker-compose.production.yml up -d --build
|
||||||
|
```
|
||||||
|
|
||||||
|
Validate tunnel container:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose --env-file .env.production -f docker-compose.production.yml logs cloudflared
|
||||||
|
```
|
||||||
|
|
||||||
|
LXC/proxied-network note:
|
||||||
|
|
||||||
|
- The `cloudflared` service is pinned to `--protocol http2` with explicit DNS resolvers (`1.1.1.1`, `1.0.0.1`) in `docker-compose.production.yml`.
|
||||||
|
- This avoids environments where Docker's embedded resolver (`127.0.0.11`) cannot resolve `region*.v2.argotunnel.com`, which causes connector precheck failure and tunnel shutdown.
|
||||||
|
- If tunnel status is still down, verify host/container egress for DNS and TCP 443 to `api.cloudflare.com` and `*.argotunnel.com`.
|
||||||
|
|
||||||
|
## 5. Security notes
|
||||||
|
|
||||||
|
- Keep `postgres` and other internal-only services off public hostnames unless required.
|
||||||
|
- Use distinct hostnames per service; avoid path-based multiplexing for unrelated admin surfaces.
|
||||||
|
- Rotate `CLOUDFLARE_TUNNEL_TOKEN` and Access policy memberships on a regular schedule.
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
# Database Rebuild Migration Workflow
|
||||||
|
|
||||||
|
This project uses an explicit **export/import rebuild workflow** for schema migration.
|
||||||
|
|
||||||
|
Policy:
|
||||||
|
- Do not add runtime legacy-compatibility write paths.
|
||||||
|
- Rebuild a fresh target database from current models.
|
||||||
|
- Export current data/media, then import into the fresh target.
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
### 1) Export current DB + uploads into a bundle
|
||||||
|
|
||||||
|
```bash
|
||||||
|
uv run python tools/export_import_migration.py export --bundle-dir .migration-bundle
|
||||||
|
```
|
||||||
|
|
||||||
|
Optional source overrides:
|
||||||
|
- `--source-db <path-or-sqlalchemy-url>`
|
||||||
|
- `--source-upload-dir <path>`
|
||||||
|
|
||||||
|
### 2) Import bundle into a fresh target (SQLite or PostgreSQL)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
uv run python tools/export_import_migration.py import --bundle-dir .migration-bundle --target-db .\data\transcription-new.db --target-upload-dir .\data-new
|
||||||
|
```
|
||||||
|
|
||||||
|
PostgreSQL target example:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
uv run python tools/export_import_migration.py import --bundle-dir .migration-bundle --target-db postgresql://transcription:change-me@localhost:5432/transcription --target-upload-dir .\data-new
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3) Verify migration parity and integrity
|
||||||
|
|
||||||
|
```bash
|
||||||
|
uv run python tools/export_import_migration.py verify --source-db .\data\transcription.db --target-db postgresql://transcription:change-me@localhost:5432/transcription
|
||||||
|
```
|
||||||
|
|
||||||
|
The verify command checks:
|
||||||
|
|
||||||
|
- row-count parity across migration tables
|
||||||
|
- orphan-reference checks for `source`, `job`, `job_source`, and `execution_attempt`
|
||||||
|
- duplicate `(job_id, source_id, attempt_number)` in `execution_attempt`
|
||||||
|
|
||||||
|
Exit code:
|
||||||
|
|
||||||
|
- `0` when counts and integrity checks pass
|
||||||
|
- `1` when mismatches or integrity violations are detected
|
||||||
|
|
||||||
|
### 4) One-shot export+import
|
||||||
|
|
||||||
|
```bash
|
||||||
|
uv run python tools/export_import_migration.py migrate --bundle-dir .migration-bundle --target-db .\data\transcription-new.db --target-upload-dir .\data-new
|
||||||
|
```
|
||||||
|
|
||||||
|
## What gets migrated
|
||||||
|
|
||||||
|
- Tables (in dependency order): `document_type`, `person_role`, `tag`, `document`, `person`, `photo`, `document_person`, `document_tag`, `person_tag`, `job`, `source`, `job_source`, `execution_attempt`.
|
||||||
|
- Media tree under `UPLOAD_DIR`.
|
||||||
|
|
||||||
|
The bundle contains:
|
||||||
|
- `database.json` (row export)
|
||||||
|
- `uploads/` (copied media files)
|
||||||
|
|
||||||
|
Path normalization during export/import:
|
||||||
|
- `source.file_path` is normalized to `documents/...` (upload-root-relative POSIX).
|
||||||
|
- `photo.path` is normalized to `photos/...` (upload-root-relative POSIX).
|
||||||
|
|
||||||
|
Legacy V4.x portrait/homepage backfill in the export step:
|
||||||
|
- If the source DB has no `photo` table, the exporter synthesizes `photo` rows from legacy `person.portrait_path` values and from legacy homepage image files under `UPLOAD_DIR/homepage`.
|
||||||
|
- Legacy portrait and homepage image files are copied into the unified `UPLOAD_DIR/photos/{photo_id}{suffix}` layout in the migration bundle.
|
||||||
|
- Legacy homepage markdown is relocated from `UPLOAD_DIR/homepage/homepage.md` to `UPLOAD_DIR/homepage.md`.
|
||||||
|
- Legacy `person.full_name` values are split into `given_names` + `last_name` for V5.1 schema compatibility.
|
||||||
|
|
||||||
|
## Cutover (SQLite -> PostgreSQL)
|
||||||
|
|
||||||
|
After importing to a fresh target:
|
||||||
|
1. Stop app and worker services to freeze writes.
|
||||||
|
2. Export a migration bundle from the last SQLite state.
|
||||||
|
3. Import bundle to PostgreSQL target.
|
||||||
|
4. Run `verify` against source and target before switching runtime.
|
||||||
|
5. Switch runtime config to PostgreSQL (`DATABASE__DRIVER=postgres` and related `DATABASE__*` values).
|
||||||
|
6. Start app and worker services.
|
||||||
|
7. Run smoke checks (`/healthz`, create/upload/process one job).
|
||||||
|
|
||||||
|
## Rollback
|
||||||
|
|
||||||
|
If verify or smoke checks fail:
|
||||||
|
|
||||||
|
1. Stop app and worker services.
|
||||||
|
2. Revert runtime config to SQLite.
|
||||||
|
3. Start app and worker against pre-cutover SQLite database.
|
||||||
|
4. Preserve failed migration bundle and logs for analysis.
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
# Error Handling Policy (Current Baseline: V6.1)
|
||||||
|
|
||||||
|
This policy defines the active V6.1 error taxonomy, translation boundaries, and retry semantics.
|
||||||
|
|
||||||
|
## Error Categories
|
||||||
|
|
||||||
|
| Category | Meaning | Typical Origin | User Treatment |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `validation` | Input payload/selection is invalid | UI form parsing, service validators | Inline correction guidance |
|
||||||
|
| `not_found` | Target record is missing | ID lookup in service layer | Non-blocking warning or redirect |
|
||||||
|
| `conflict` | State prevents requested action | lifecycle transitions, duplicate semantic keys | Explain required precondition |
|
||||||
|
| `external` | Provider/network dependency failure | OpenRouter/provider adapter | Retry path and evidence retained |
|
||||||
|
| `timeout` | Provider call exceeded configured bound | worker/provider client timeout | Retry path and bounded messaging |
|
||||||
|
| `internal` | Unexpected local failure | unhandled service/runtime faults | Safe generic message + diagnostics capture |
|
||||||
|
|
||||||
|
## Runtime Taxonomy and Canonical Mapping
|
||||||
|
|
||||||
|
Runtime code uses a richer internal taxonomy for diagnostics and persisted evidence, then maps that
|
||||||
|
taxonomy to the six canonical categories at the API/UI envelope boundary.
|
||||||
|
|
||||||
|
### Internal runtime categories
|
||||||
|
|
||||||
|
- `validation_error`
|
||||||
|
- `user_input_error`
|
||||||
|
- `not_found_error`
|
||||||
|
- `conflict_error`
|
||||||
|
- `external_provider_error`
|
||||||
|
- `external_timeout_error`
|
||||||
|
- `processing_error`
|
||||||
|
- `infrastructure_transient_error`
|
||||||
|
- `infrastructure_persistent_error`
|
||||||
|
- `internal_unexpected_error`
|
||||||
|
|
||||||
|
### Internal -> Canonical mapping
|
||||||
|
|
||||||
|
| Internal category | Canonical envelope category |
|
||||||
|
| :--- | :--- |
|
||||||
|
| `validation_error` | `validation` |
|
||||||
|
| `user_input_error` | `validation` |
|
||||||
|
| `not_found_error` | `not_found` |
|
||||||
|
| `conflict_error` | `conflict` |
|
||||||
|
| `external_provider_error` | `external` |
|
||||||
|
| `external_timeout_error` | `timeout` |
|
||||||
|
| `infrastructure_transient_error` | `timeout` |
|
||||||
|
| `processing_error` | `internal` |
|
||||||
|
| `infrastructure_persistent_error` | `internal` |
|
||||||
|
| `internal_unexpected_error` | `internal` |
|
||||||
|
|
||||||
|
`ExecutionAttempt.error_category` stores the internal category value so diagnostics remain specific.
|
||||||
|
|
||||||
|
## Translation Boundaries
|
||||||
|
|
||||||
|
- **Provider layer:** raise provider-scoped exceptions with provider context; do not emit UI text.
|
||||||
|
- **Service layer:** map raw exceptions into internal categories and preserve causal chain.
|
||||||
|
- **UI/API layer:** convert internal categories to canonical categories using the centralized mapping.
|
||||||
|
|
||||||
|
## Decision Context
|
||||||
|
|
||||||
|
### Why taxonomy is category-based (not exception-class-based)
|
||||||
|
|
||||||
|
- Categories encode operator-facing recovery semantics (fix input, retry later, investigate internal failure) independent of low-level exception type.
|
||||||
|
- This keeps retry and messaging behavior consistent even when provider/client libraries change.
|
||||||
|
|
||||||
|
### Why page-level failure is isolated
|
||||||
|
|
||||||
|
- Multi-page archival documents often contain a mix of readable and degraded pages.
|
||||||
|
- Isolating failures to page scope preserves successful results and avoids all-or-nothing loss when one page fails.
|
||||||
|
- Aggregate job status then communicates overall outcome (`transcribed`, `partial_success`, `failed`) without hiding page detail.
|
||||||
|
|
||||||
|
### Why retries append evidence instead of mutating rows
|
||||||
|
|
||||||
|
- Retry operations are new observations, not corrections of history.
|
||||||
|
- Appending attempts preserves forensic traceability, timing history, and provider variability analysis.
|
||||||
|
- Projection updates remain explicit user/workflow decisions, separate from immutable evidence.
|
||||||
|
|
||||||
|
## Job and Page Failure Semantics
|
||||||
|
|
||||||
|
### Page-Level (`JobSource`)
|
||||||
|
|
||||||
|
- `pending` -> `transcribed` when attempt succeeds.
|
||||||
|
- `pending` -> `failed` when attempt fails terminally.
|
||||||
|
- `pending` -> `cancelled` on job cancellation before processing.
|
||||||
|
|
||||||
|
### Job-Level (`Job`)
|
||||||
|
|
||||||
|
- `transcribed` when all pages transcribe successfully.
|
||||||
|
- `partial_success` when mixed success/failure outcomes exist.
|
||||||
|
- `failed` when no page transcribes successfully.
|
||||||
|
|
||||||
|
## Retry and Retranscription Rules
|
||||||
|
|
||||||
|
1. Failed/cancelled pages may be re-queued through retranscription workflows.
|
||||||
|
2. Retry attempts must append new `ExecutionAttempt` rows; prior evidence remains immutable.
|
||||||
|
3. Selecting a better candidate must update projection pointers, not mutate historical attempt rows.
|
||||||
|
|
||||||
|
## Logging and Diagnostics Rules
|
||||||
|
|
||||||
|
1. Persist sufficient attempt error metadata (`error_category`, `error_message`, transport evidence) for post-hoc analysis.
|
||||||
|
2. Avoid leaking stack traces or local paths into user-facing message envelopes.
|
||||||
|
3. Preserve causal exception chains for internal diagnostics.
|
||||||
|
|
||||||
|
### Message vs detail split
|
||||||
|
|
||||||
|
Rules 1 and 2 pull in opposite directions: evidence records need the root cause, and
|
||||||
|
user-facing envelopes must not carry it. `AppError` therefore separates the two audiences:
|
||||||
|
|
||||||
|
| Field | Audience | Carries root cause | Surfaces |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `message` | User-facing and API-facing | No | `show_error`, `build_error_envelope` |
|
||||||
|
| `detail` | Internal only | Yes | `format_error_detail` (evidence), logs, sanitized UI projection only |
|
||||||
|
|
||||||
|
`classify_unexpected_error` builds a generic `message` and puts the exception type and
|
||||||
|
text on `detail`. Anything rendered to a user or serialized into an API envelope must
|
||||||
|
read `message`; anything persisted as provenance or logged may read `detail`. When a UI
|
||||||
|
surface needs to show persisted `error_detail`, it must route through a sanitizing
|
||||||
|
projection that preserves the category, suggestion, and error reference while reducing
|
||||||
|
machine-local absolute paths to basenames only.
|
||||||
|
Enforced by `tests/test_errors.py::test_unexpected_error_does_not_leak_filesystem_paths`
|
||||||
|
and `tests/test_error_message_safety.py`.
|
||||||
|
|
||||||
|
## Operator Recovery Guidance
|
||||||
|
|
||||||
|
- **validation/conflict:** correct input or state and retry manually.
|
||||||
|
- **external/timeout:** allow bounded retries and keep prior attempt evidence visible.
|
||||||
|
- **internal:** stop automatic retries, surface a safe message, and inspect diagnostics with correlation context.
|
||||||
|
|
||||||
|
## UI Messaging Contract
|
||||||
|
|
||||||
|
- User-visible errors must be actionable, bounded, and category-consistent.
|
||||||
|
- Multi-page jobs must show partial outcomes instead of collapsing into a single opaque failure.
|
||||||
|
- Recovery actions (`retry`, `retranscribe`, `edit input`) must be offered where available.
|
||||||
|
|
||||||
|
## Cross-Reference
|
||||||
|
|
||||||
|
- [Error Handling invariant](./invariant/error_handling.md)
|
||||||
|
- [System Requirements](requirements.md)
|
||||||
|
- [Data Model](schema.md)
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
# Document Transcription System Overview (Current Baseline: V6.1)
|
||||||
|
|
||||||
|
This directory is the single source of truth for current V6.1 behavior and architecture.
|
||||||
|
|
||||||
|
## Canonical Reading Order
|
||||||
|
|
||||||
|
1. [System Architecture](architecture.md) for runtime topology, boundaries, and lifecycle ownership.
|
||||||
|
2. [System Requirements](requirements.md) for verifiable current-state requirements.
|
||||||
|
3. [Data Model](schema.md) for entities, constraints, and evidence persistence rules.
|
||||||
|
4. [Error Handling Policy](error_handling.md) for category, translation, and retry behavior.
|
||||||
|
|
||||||
|
## Cross-Version Invariants
|
||||||
|
|
||||||
|
- [Historical Document Transcription Design Intent](./invariant/intent.md)
|
||||||
|
- [Transcription Methodology](./invariant/transcription_methodology.md)
|
||||||
|
- [Error Handling](./invariant/error_handling.md)
|
||||||
|
- [Digital Evidence and AI Processing Provenance](./invariant/ai_evidence_and_provenance.md)
|
||||||
|
- [UI Style Guide](./invariant/ui_style_guide.md)
|
||||||
|
|
||||||
|
## Deployment and Operations
|
||||||
|
|
||||||
|
- [Production Runbook](production-runbook.md) for deploy, rollback, and recovery.
|
||||||
|
- [Backup and Restore](backup_restore.md) for backup configuration and restore procedure.
|
||||||
|
- [Data Migration](data_migration.md) for the SQLite to PostgreSQL migration path.
|
||||||
|
- [Cloudflare Tunnel and Access](cloudflare_tunnel_access.md) for remote exposure and access control.
|
||||||
|
|
||||||
|
## Baseline Statement
|
||||||
|
|
||||||
|
The current V6.1 baseline includes the architectural cleanup, person-schema redesign,
|
||||||
|
containerized PostgreSQL deployment, and the navigation, Document Detail, and worker-backed
|
||||||
|
maintenance refinements reflected across this canonical document set.
|
||||||
|
Use this `docs/*` canonical set for active design and implementation decisions.
|
||||||
|
|
||||||
|
Every canonical document above states this same baseline; `tests/test_meta_contract_guards.py`
|
||||||
|
fails if one of them falls behind. Forward-looking work is tracked in
|
||||||
|
[`roadmap_plan.md`](roadmap_plan.md) and is not part of the baseline.
|
||||||
@@ -10,7 +10,7 @@ The application exists to preserve historical source material and produce useful
|
|||||||
|
|
||||||
The application distinguishes five kinds of information:
|
The application distinguishes five kinds of information:
|
||||||
|
|
||||||
1. **Source evidence**: the original uploaded media and the facts needed to identify and verify it.
|
1. **Source evidence**: the canonical stored media used for processing and the facts needed to identify and verify it.
|
||||||
2. **Execution specification**: the frozen instructions, parameters, source identity, and software context for one processing attempt.
|
2. **Execution specification**: the frozen instructions, parameters, source identity, and software context for one processing attempt.
|
||||||
3. **Transport evidence**: the response received at the application/provider boundary, including safe protocol metadata.
|
3. **Transport evidence**: the response received at the application/provider boundary, including safe protocol metadata.
|
||||||
4. **Normalized data**: selected fields extracted for search, display, accounting, and workflow behavior.
|
4. **Normalized data**: selected fields extracted for search, display, accounting, and workflow behavior.
|
||||||
@@ -20,12 +20,12 @@ Normalized data and derived artifacts never replace source or transport evidence
|
|||||||
|
|
||||||
## 3. Core Invariants
|
## 3. Core Invariants
|
||||||
|
|
||||||
### 3.1 Original Source Preservation
|
### 3.1 Canonical Source Preservation
|
||||||
|
|
||||||
1. The original uploaded bytes are the primary evidence and must be preserved without transformation.
|
1. Each source must have one canonical stored byte stream used for processing and provenance.
|
||||||
2. Each source must have a cryptographic content digest, byte size, and stable identity.
|
2. Canonical storage may apply deterministic ingest normalization before persistence.
|
||||||
3. Processing may use transformed derivatives, but those derivatives must not overwrite the original.
|
3. Canonical stored bytes must have a cryptographic content digest, byte size, and stable identity.
|
||||||
4. A derivative used for processing must record its relationship to the original, its transformation, and its own digest.
|
4. Post-ingest processing derivatives must not overwrite canonical stored bytes.
|
||||||
5. Moving or renaming a stored file must not change its evidence identity.
|
5. Moving or renaming a stored file must not change its evidence identity.
|
||||||
|
|
||||||
### 3.2 Append-Only Processing History
|
### 3.2 Append-Only Processing History
|
||||||
@@ -45,7 +45,7 @@ Each execution must preserve enough information to understand what the applicati
|
|||||||
3. Prompt asset name and content digest when a prompt asset is used.
|
3. Prompt asset name and content digest when a prompt asset is used.
|
||||||
4. Every explicitly supplied generation or processing parameter.
|
4. Every explicitly supplied generation or processing parameter.
|
||||||
5. Whether an optional parameter was explicitly set or omitted.
|
5. Whether an optional parameter was explicitly set or omitted.
|
||||||
6. Source and derivative digests, media type, dimensions or page geometry when known, and page identity.
|
6. Canonical source digest (and derivative digests when used), media type, dimensions or page geometry when known, and page identity.
|
||||||
7. A secret-safe representation of the request structure.
|
7. A secret-safe representation of the request structure.
|
||||||
8. Application, provider-adapter, and client-library versions sufficient to interpret the execution.
|
8. Application, provider-adapter, and client-library versions sufficient to interpret the execution.
|
||||||
|
|
||||||
@@ -108,7 +108,7 @@ Provenance supports explanation, comparison, and best-effort reproduction; it do
|
|||||||
|
|
||||||
Identical requests may produce different results because of model updates, provider routing, nondeterministic computation, undocumented defaults, safety systems, or retired endpoints. The application must preserve whether a parameter was omitted rather than pretending to know the provider default used at that time.
|
Identical requests may produce different results because of model updates, provider routing, nondeterministic computation, undocumented defaults, safety systems, or retired endpoints. The application must preserve whether a parameter was omitted rather than pretending to know the provider default used at that time.
|
||||||
|
|
||||||
Likewise, preserving a general vision-model response does not create OCR coordinates that were never returned. Future coordinate extraction remains possible because the original source evidence is preserved and can be processed again by a suitable system.
|
Likewise, preserving a general vision-model response does not create OCR coordinates that were never returned. Future coordinate extraction remains possible because canonical source evidence is preserved and can be processed again by a suitable system.
|
||||||
|
|
||||||
## 5. Model Evaluation Policy
|
## 5. Model Evaluation Policy
|
||||||
|
|
||||||
@@ -123,11 +123,15 @@ Evaluation should:
|
|||||||
5. Preserve the exact model, endpoint or route, parameters, prompt, source digest, and scoring method for every comparison.
|
5. Preserve the exact model, endpoint or route, parameters, prompt, source digest, and scoring method for every comparison.
|
||||||
6. Treat model rankings as corpus- and version-specific, not permanent declarations of a universal “best” model.
|
6. Treat model rankings as corpus- and version-specific, not permanent declarations of a universal “best” model.
|
||||||
|
|
||||||
|
The deterministic scorer for these comparisons lives in `src/transcription/benchmarking.py`; it is
|
||||||
|
retained as evaluation-policy infrastructure even though application runtime paths do not call it
|
||||||
|
directly.
|
||||||
|
|
||||||
Benchmark material containing family records remains private application data unless explicitly approved for publication.
|
Benchmark material containing family records remains private application data unless explicitly approved for publication.
|
||||||
|
|
||||||
## 6. Ownership and Change Policy
|
## 6. Ownership and Change Policy
|
||||||
|
|
||||||
1. Versioned architecture, schema, scope, and implementation documents define how a release satisfies this invariant.
|
1. Canonical V6.1 architecture, schema, requirements, and error-policy documents define how current behavior satisfies this invariant.
|
||||||
2. Provider adapters own the capture of provider-boundary evidence.
|
2. Provider adapters own the capture of provider-boundary evidence.
|
||||||
3. Services own validation, persistence, retention, and export behavior.
|
3. Services own validation, persistence, retention, and export behavior.
|
||||||
4. UI pages may inspect evidence through service contracts but do not define evidence semantics.
|
4. UI pages may inspect evidence through service contracts but do not define evidence semantics.
|
||||||
|
|||||||
@@ -45,6 +45,24 @@ The following rules map directly to editorial conventions for handling common ma
|
|||||||
| **Non-Textual Artifacts** | Record non-textual elements (seals, stamps, sketches, physical damage) using brief descriptive text inside square brackets. | [description] | [wax notary seal attached here] or [sketch of a fort layout] |
|
| **Non-Textual Artifacts** | Record non-textual elements (seals, stamps, sketches, physical damage) using brief descriptive text inside square brackets. | [description] | [wax notary seal attached here] or [sketch of a fort layout] |
|
||||||
| **Marginalia & Addenda** | Explicitly indicate spatial transitions before transcribing content located in margins or non-standard orientations. | [location:] | [written in left margin:] Do not share this with anyone. |
|
| **Marginalia & Addenda** | Explicitly indicate spatial transitions before transcribing content located in margins or non-standard orientations. | [location:] | [written in left margin:] Do not share this with anyone. |
|
||||||
|
|
||||||
|
### 3.4 Document-Body Medium
|
||||||
|
|
||||||
|
Every transcript must identify the predominant document-body medium exactly once at the beginning:
|
||||||
|
|
||||||
|
| Medium | Use | Standard Markup |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| **Handwritten** | The main body was written by hand. | `[document body handwritten]` |
|
||||||
|
| **Typewritten** | The main body was produced with a typewriter. Uneven impressions, monospaced characters, and mechanical defects remain typewritten rather than handwritten. | `[document body typewritten]` |
|
||||||
|
| **Typeset** | The main body was composed for printing or produced as printed text rather than with a typewriter. | `[document body typeset]` |
|
||||||
|
| **Mixed** | No single medium predominates, or handwritten and printed/typewritten content are structurally interleaved. | `[document body mixed]` |
|
||||||
|
|
||||||
|
- Use exactly one document-body marker.
|
||||||
|
- Do not wrap each line in `[handwritten: ...]` after declaring the body handwritten.
|
||||||
|
- In typewritten or typeset documents, use localized handwriting markers only for genuinely handwritten annotations, insertions, or signatures.
|
||||||
|
- In mixed documents, identify handwritten portions locally while preserving their reading context.
|
||||||
|
- Preserve tables of contents, tables, forms, columns, captions, marginalia, page numbers, dotted leaders, and associated references in their logical reading order.
|
||||||
|
- Produce plain text characters rather than HTML entities for ordinary transcription content.
|
||||||
|
|
||||||
## 4. Prompt Asset Integration
|
## 4. Prompt Asset Integration
|
||||||
|
|
||||||
When executing programmatic transcriptions via LLM APIs or local models, processing instructions must be packaged into single-purpose system prompts aligned with these rules.
|
When executing programmatic transcriptions via LLM APIs or local models, processing instructions must be packaged into single-purpose system prompts aligned with these rules.
|
||||||
|
|||||||
@@ -4,14 +4,14 @@
|
|||||||
This guide defines non-negotiable UI styling rules for the transcription application.
|
This guide defines non-negotiable UI styling rules for the transcription application.
|
||||||
|
|
||||||
The design system is token-first and class-driven:
|
The design system is token-first and class-driven:
|
||||||
1. Theme tokens are defined in [src/transcription/ui/static/theme.css](src/transcription/ui/static/theme.css).
|
1. Theme tokens are defined in [src/transcription/ui/static/theme.css](../../src/transcription/ui/static/theme.css).
|
||||||
2. Python UI code composes semantic classes instead of inline color values.
|
2. Python UI code composes semantic classes instead of inline color values.
|
||||||
3. Pages and components should share a single visual language across Documents, Jobs, People, and Sources flows.
|
3. Pages and components should share a single visual language across Documents, Jobs, People, and Sources flows.
|
||||||
|
|
||||||
## 2. Source of Truth
|
## 2. Source of Truth
|
||||||
Use these files as the style authority:
|
Use these files as the style authority:
|
||||||
1. [src/transcription/ui/static/theme.css](src/transcription/ui/static/theme.css) for color tokens, semantic utility classes, table styles, and viewer surfaces.
|
1. [src/transcription/ui/static/theme.css](../../src/transcription/ui/static/theme.css) for color tokens, semantic utility classes, table styles, and viewer surfaces.
|
||||||
2. [src/transcription/ui/theme.py](src/transcription/ui/theme.py) for runtime NiceGUI theme bridge and shared UI helpers.
|
2. [src/transcription/ui/theme.py](../../src/transcription/ui/theme.py) for runtime NiceGUI theme bridge and shared UI helpers.
|
||||||
|
|
||||||
If this document conflicts with implementation, update this document to match the code immediately after intentional style changes.
|
If this document conflicts with implementation, update this document to match the code immediately after intentional style changes.
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ If this document conflicts with implementation, update this document to match th
|
|||||||
## 4. Token System
|
## 4. Token System
|
||||||
|
|
||||||
### 4.1 Palette Tokens
|
### 4.1 Palette Tokens
|
||||||
Base palette variables live under :root in [src/transcription/ui/static/theme.css](src/transcription/ui/static/theme.css):
|
Base palette variables live under :root in [src/transcription/ui/static/theme.css](../../src/transcription/ui/static/theme.css):
|
||||||
1. --palette-carbon-black: #1c2321
|
1. --palette-carbon-black: #1c2321
|
||||||
2. --palette-cool-steel: #7d98a1
|
2. --palette-cool-steel: #7d98a1
|
||||||
3. --palette-blue-slate: #5e6572
|
3. --palette-blue-slate: #5e6572
|
||||||
@@ -81,7 +81,7 @@ Semantic tokens currently include:
|
|||||||
2. ui-table-header
|
2. ui-table-header
|
||||||
3. ui-table-body
|
3. ui-table-body
|
||||||
|
|
||||||
Use existing class combinations from [src/transcription/ui/components](src/transcription/ui/components) and [src/transcription/ui/pages](src/transcription/ui/pages) as reference implementations.
|
Use existing class combinations from [src/transcription/ui/components](../../src/transcription/ui/components) and [src/transcription/ui/pages](../../src/transcription/ui/pages) as reference implementations.
|
||||||
|
|
||||||
## 6. Legacy Class Policy
|
## 6. Legacy Class Policy
|
||||||
Legacy `vibe-` presentation classes are prohibited. Use `ui-` semantic classes from `theme.css`.
|
Legacy `vibe-` presentation classes are prohibited. Use `ui-` semantic classes from `theme.css`.
|
||||||
@@ -96,7 +96,7 @@ Legacy `vibe-` presentation classes are prohibited. Use `ui-` semantic classes f
|
|||||||
|
|
||||||
## 8. Implementation Rules For Contributors
|
## 8. Implementation Rules For Contributors
|
||||||
1. Prefer composing existing semantic classes before creating new ones.
|
1. Prefer composing existing semantic classes before creating new ones.
|
||||||
2. If a new class is required, add it to [src/transcription/ui/static/theme.css](src/transcription/ui/static/theme.css) with a semantic name, then reuse it.
|
2. If a new class is required, add it to [src/transcription/ui/static/theme.css](../../src/transcription/ui/static/theme.css) with a semantic name, then reuse it.
|
||||||
3. Keep behavior ownership in Python and appearance ownership in CSS.
|
3. Keep behavior ownership in Python and appearance ownership in CSS.
|
||||||
4. Update UI tests that assert exact text or labels when intentional copy changes are made.
|
4. Update UI tests that assert exact text or labels when intentional copy changes are made.
|
||||||
5. Avoid introducing class churn unrelated to the feature being changed.
|
5. Avoid introducing class churn unrelated to the feature being changed.
|
||||||
@@ -104,7 +104,7 @@ Legacy `vibe-` presentation classes are prohibited. Use `ui-` semantic classes f
|
|||||||
## 9. Verification Checklist
|
## 9. Verification Checklist
|
||||||
Before merging UI changes, verify:
|
Before merging UI changes, verify:
|
||||||
1. No new inline hex colors were introduced in UI pages/components.
|
1. No new inline hex colors were introduced in UI pages/components.
|
||||||
2. New styles are token-backed and added to [src/transcription/ui/static/theme.css](src/transcription/ui/static/theme.css).
|
2. New styles are token-backed and added to [src/transcription/ui/static/theme.css](../../src/transcription/ui/static/theme.css).
|
||||||
3. Primary buttons, links, cards, and tables still render with consistent semantics.
|
3. Primary buttons, links, cards, and tables still render with consistent semantics.
|
||||||
4. Keyboard focus ring visibility is preserved.
|
4. Keyboard focus ring visibility is preserved.
|
||||||
5. Relevant UI and integration tests pass.
|
5. Relevant UI and integration tests pass.
|
||||||
@@ -0,0 +1,184 @@
|
|||||||
|
# Production Runbook
|
||||||
|
|
||||||
|
This runbook is the operational checklist for releasing and monitoring the transcription system.
|
||||||
|
|
||||||
|
## 1. Pre-release gate checklist
|
||||||
|
|
||||||
|
1. Run the full suite: `uv run pytest`
|
||||||
|
2. Confirm contract guardrails are green:
|
||||||
|
- `uv run pytest tests/test_meta_contract_guards.py`
|
||||||
|
3. Confirm health endpoint includes worker liveness payload (`/healthz` returns `worker.state`).
|
||||||
|
4. Confirm required runtime settings are present in deployment environment:
|
||||||
|
- `OPENROUTER_API_KEY`
|
||||||
|
- `DATABASE__*`
|
||||||
|
- filesystem paths for data/logs/backups.
|
||||||
|
- `CLOUDFLARE_TUNNEL_TOKEN`
|
||||||
|
5. Confirm schema contract alignment is current:
|
||||||
|
- `src/transcription/db/models.py`
|
||||||
|
- `docs/schema.md`
|
||||||
|
|
||||||
|
## 2. Release execution steps
|
||||||
|
|
||||||
|
1. Deploy artifact/config to target environment.
|
||||||
|
- Production stack: `docker compose -f docker-compose.production.yml up -d --build`
|
||||||
|
- `Settings` loads from explicit `_env_file`, then `ENV_FILE`, then the repository-root `.env.production`; it does not resolve relative to the process working directory.
|
||||||
|
- For Runtime Settings writes in production, mount `.env.production` into the app container and set `RUNTIME_SETTINGS_ENV_FILE=/app/.env.production`.
|
||||||
|
- If deployment uses a non-default env-file location, set both `ENV_FILE` and `RUNTIME_SETTINGS_ENV_FILE` to that absolute path so startup reads and Settings-page writes stay aligned.
|
||||||
|
- For SQLite -> PostgreSQL cutover, run `uv run python tools/export_import_migration.py verify --source-db <sqlite-path-or-url> --target-db <postgres-url>` before switching runtime.
|
||||||
|
2. Validate service startup:
|
||||||
|
- `/healthz` responds `200`
|
||||||
|
- if `RUN_EMBEDDED_WORKER=true`, `worker.state` is `running`
|
||||||
|
- if `RUN_EMBEDDED_WORKER=false`, validate `worker` container is running in Compose
|
||||||
|
(worker healthcheck is intentionally disabled because it does not expose `/healthz`)
|
||||||
|
- validate `cloudflared` logs show active tunnel routes and no ingress errors
|
||||||
|
3. Execute one smoke workflow:
|
||||||
|
- create a document/job with at least one source
|
||||||
|
- verify terminal job outcome updates
|
||||||
|
- verify execution evidence row appended
|
||||||
|
4. Verify log flow:
|
||||||
|
- stdout aggregation receives events
|
||||||
|
- file logs are written under `./data/logs`
|
||||||
|
5. Create a fresh PostgreSQL backup after successful deployment:
|
||||||
|
- `sh deploy/backup/create_postgres_backup.sh` (creates DB dump plus uploads/prompts backups under `BACKUP_DIR`)
|
||||||
|
|
||||||
|
## 3. Rollback triggers and actions
|
||||||
|
|
||||||
|
### Trigger conditions
|
||||||
|
|
||||||
|
1. `/healthz` reports `worker.state=failed`
|
||||||
|
2. Repeated provider timeout/error spikes beyond normal baseline
|
||||||
|
3. Evidence write failures or DB persistence failures
|
||||||
|
|
||||||
|
### Actions
|
||||||
|
|
||||||
|
1. Roll back app artifact and config to previous release.
|
||||||
|
2. Restart service and re-check `/healthz`.
|
||||||
|
3. Re-run smoke workflow and confirm worker returns to `running`.
|
||||||
|
4. Preserve incident evidence:
|
||||||
|
- `./data/logs`
|
||||||
|
- relevant DB rows (`job`, `job_source`, `execution_attempt`)
|
||||||
|
5. If persistence regression is confirmed, restore the latest valid DB dump:
|
||||||
|
- `sh deploy/backup/restore_postgres_backup.sh <dump-file>`
|
||||||
|
- Synology media mirror and paired config snapshot (same timestamp) are restored automatically when present.
|
||||||
|
|
||||||
|
## 4. Post-release monitoring checklist
|
||||||
|
|
||||||
|
## First 24 hours
|
||||||
|
|
||||||
|
1. Monitor `/healthz` periodically for `worker.state`.
|
||||||
|
2. Track job terminal distribution (`transcribed`, `partial_success`, `failed`).
|
||||||
|
3. Sample timeout/error categories for abnormal increase.
|
||||||
|
4. Spot-check new `execution_attempt` records for append-only growth and timing metadata.
|
||||||
|
|
||||||
|
## First 72 hours
|
||||||
|
|
||||||
|
1. Re-check error/timeout trend versus 24h baseline.
|
||||||
|
2. Verify no recurring worker-failed states.
|
||||||
|
3. Verify storage growth and rotation behavior under `./data/logs`.
|
||||||
|
4. Confirm incident response notes are captured for any production anomalies.
|
||||||
|
|
||||||
|
## 5. Operator playbook for common incidents
|
||||||
|
|
||||||
|
### Worker failed
|
||||||
|
|
||||||
|
1. Check `/healthz` payload (`error_id`, `error_category`).
|
||||||
|
2. Locate matching error in logs.
|
||||||
|
3. If non-transient defect persists, roll back.
|
||||||
|
|
||||||
|
### Provider timeout spike
|
||||||
|
|
||||||
|
1. Confirm provider reachability and rate limits.
|
||||||
|
2. Review timeout frequency and impacted job volume.
|
||||||
|
3. If sustained, execute rollback criteria and notify stakeholders.
|
||||||
|
|
||||||
|
### Partial-success increase
|
||||||
|
|
||||||
|
1. Inspect affected `job_source` and `execution_attempt` records.
|
||||||
|
2. Confirm failures are category-aligned (`external`/`timeout`/`internal`).
|
||||||
|
3. Triage whether issue is source quality, provider, or runtime regression.
|
||||||
|
|
||||||
|
### Cloudflare ingress/access failure
|
||||||
|
|
||||||
|
1. Check `cloudflared` container logs for ingress parse, DNS, or auth failures.
|
||||||
|
2. Confirm `deploy/cloudflared/config.yml` hostname mappings are correct.
|
||||||
|
3. Confirm `CLOUDFLARE_TUNNEL_TOKEN` in `.env.production` matches the tunnel configured in Cloudflare.
|
||||||
|
4. Confirm Cloudflare Access app policy includes the intended identity/group for that hostname.
|
||||||
|
5. If logs show SRV/DNS failures via `127.0.0.11`, use the compose-defined resolver override
|
||||||
|
(`dns: 1.1.1.1, 1.0.0.1`) and ensure outbound TCP 443 is allowed.
|
||||||
|
|
||||||
|
### Backup or restore failure
|
||||||
|
|
||||||
|
1. Verify `postgres` container is healthy and accepting connections.
|
||||||
|
2. Confirm dump file exists and is non-zero size.
|
||||||
|
3. Re-run backup/restore scripts with explicit `ENV_FILE` and `COMPOSE_FILE` if using non-default paths.
|
||||||
|
4. If direct Synology copy fails, keep local backup and resolve mount/network before next backup cycle.
|
||||||
|
- For LXC setups, use `deploy/backup/mount_synology_cifs.example.sh` as the persistent mount template.
|
||||||
|
|
||||||
|
## 6. Dependency upgrade policy
|
||||||
|
|
||||||
|
Dependencies are declared in `pyproject.toml` and resolved through the committed
|
||||||
|
`uv.lock`. The lockfile guarantees reproducible installs; the version specifiers
|
||||||
|
control what a deliberate `uv lock --upgrade` is allowed to move.
|
||||||
|
|
||||||
|
### NiceGUI is pinned exactly (`nicegui==3.13.0`)
|
||||||
|
|
||||||
|
1. **Rationale.** NiceGUI bundles Quasar and Vue. Minor releases change component
|
||||||
|
props, slots, and styling, which surfaces as visual and interaction regressions
|
||||||
|
rather than import or type errors. The UI suite under `tests/ui/` asserts
|
||||||
|
structure and behavior, not rendered appearance, so a NiceGUI bump can pass the
|
||||||
|
full test suite and still degrade the interface.
|
||||||
|
2. **Scope of risk.** All NiceGUI usage is confined to `src/transcription/ui/` and
|
||||||
|
uses only the public `nicegui.ui` and `nicegui.events` surfaces. The coupling is
|
||||||
|
shallow, so the pin is about release stability, not about unpicking deep
|
||||||
|
framework entanglement.
|
||||||
|
3. **Current stance.** Hold the exact pin through release stabilization. Do not
|
||||||
|
widen it as incidental cleanup, and do not let automated dependency updates move
|
||||||
|
it. This includes forgoing patch releases, which is the accepted cost.
|
||||||
|
4. **Revisiting.** Treat a NiceGUI upgrade as scheduled work with its own change
|
||||||
|
window: bump the pin deliberately, run `uv run pytest -m "not external"`, then
|
||||||
|
manually verify each page contract in `docs/ui/pages/` before accepting.
|
||||||
|
|
||||||
|
### All other dependencies
|
||||||
|
|
||||||
|
Declared with `>=` floors and moved by explicit `uv lock --upgrade`. Verify with
|
||||||
|
`uv run ruff check .`, `uv run ty check`, and `uv run pytest -q -m "not external"`
|
||||||
|
before committing a changed lockfile.
|
||||||
|
|
||||||
|
## 7. Type-check suppression policy
|
||||||
|
|
||||||
|
`uv run ty check` is a blocking pre-commit gate. Suppressions are allowed only for
|
||||||
|
proven SQLAlchemy descriptor false positives where runtime behavior is correct and
|
||||||
|
the checker cannot represent the descriptor protocol at that call site.
|
||||||
|
|
||||||
|
Every suppression must be:
|
||||||
|
|
||||||
|
1. **Targeted** to a single rule (for example `# ty: ignore[unresolved-attribute]`).
|
||||||
|
2. **Inline** on the expression it suppresses (not file-wide).
|
||||||
|
3. Followed by a **one-line rationale** stating it is a SQLAlchemy descriptor false positive.
|
||||||
|
|
||||||
|
Do not use broad or rationale-free suppressions. If a diagnostic is not a known
|
||||||
|
false positive, fix the code instead of suppressing it.
|
||||||
|
|
||||||
|
## 8. Worker shutdown budget
|
||||||
|
|
||||||
|
Worker shutdown waits for at most:
|
||||||
|
|
||||||
|
`WORKER_PROVIDER_TIMEOUT_SECONDS + WORKER_SHUTDOWN_GRACE_SECONDS`
|
||||||
|
|
||||||
|
`WORKER_PROVIDER_TIMEOUT_SECONDS` covers an in-flight provider call, and
|
||||||
|
`WORKER_SHUTDOWN_GRACE_SECONDS` is extra time for the loop to persist outcomes
|
||||||
|
and exit cleanly after the call returns.
|
||||||
|
|
||||||
|
Set the container or service termination grace period **above this total**
|
||||||
|
budget. If termination grace is shorter, the process may be killed before
|
||||||
|
terminal status and evidence writes are finalized.
|
||||||
|
|
||||||
|
## 9. Horizontal scaling precondition
|
||||||
|
|
||||||
|
Multiple worker replicas can race on execution-attempt numbering for the same
|
||||||
|
`(job_id, source_id)` pair. The runtime now retries boundedly on unique-key
|
||||||
|
conflicts (`uq_execution_attempt_number`) and surfaces a conflict-domain error
|
||||||
|
if retries are exhausted.
|
||||||
|
|
||||||
|
Do not deploy additional worker replicas unless this conflict-retry path and its
|
||||||
|
tests are present and green in the target build.
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
# System Requirements (Current Baseline: V6.1)
|
||||||
|
|
||||||
|
These requirements define the active V6.1 contract and align to current implementation.
|
||||||
|
|
||||||
|
Requirement IDs encode the baseline that introduced them (`REQ-4-*` from V4, `REQ-6-*` from V6) and
|
||||||
|
are stable. Never renumber an existing ID; retire it explicitly instead.
|
||||||
|
|
||||||
|
## Functional Requirements
|
||||||
|
|
||||||
|
### Domain and Record Management
|
||||||
|
|
||||||
|
- **REQ-4-001 Document Registry:** The system must create and update `Document` records with title, type, date metadata, optional location, optional archive identifier, and optional notes.
|
||||||
|
- **REQ-4-002 Source Registry:** The system must create and update `Source` records linked to exactly one `Document`.
|
||||||
|
- **REQ-4-003 People Registry:** The system must create and update `Person` records, support many-to-many links to `Document` with role, and support many-to-many Person tagging via the shared Tag registry.
|
||||||
|
- **REQ-4-004 Registry Semantics:** Document types and person roles must support optional immutable semantic keys and hard-delete only when unreferenced.
|
||||||
|
|
||||||
|
### Job and Workflow Behavior
|
||||||
|
|
||||||
|
- **REQ-4-010 Job Creation:** The system must create `Job` records from uploaded sources and from retranscription of existing sources.
|
||||||
|
- **REQ-4-011 Prompt Snapshotting:** Job creation must persist effective prompt and runtime settings as immutable per-job snapshots.
|
||||||
|
- **REQ-4-012 Queue Membership:** Each `(job, source)` pair must be represented by one `JobSource` row.
|
||||||
|
- **REQ-4-013 Job Status Lifecycle:** `Job.status` must use one of `queued`, `processing`, `transcribed`, `partial_success`, `failed`.
|
||||||
|
- **REQ-4-014 JobSource Status Lifecycle:** `JobSource.status` must use one of `pending`, `transcribed`, `failed`, `cancelled`.
|
||||||
|
- **REQ-4-015 Terminal Job Resolution:** Job terminal status must derive from page outcomes as `transcribed`, `partial_success`, or `failed`.
|
||||||
|
- **REQ-4-016 Cancellation Semantics:** Job cancellation must set remaining `pending` page entries to `cancelled`.
|
||||||
|
|
||||||
|
### Transcription and Evidence
|
||||||
|
|
||||||
|
- **REQ-4-020 Attempt Evidence:** Each provider call must emit one append-only `ExecutionAttempt` record.
|
||||||
|
- **REQ-4-021 Attempt Payload:** `ExecutionAttempt` must retain request manifest/hash, outcome, timing, model/provider fields, and error details when present.
|
||||||
|
- **REQ-4-022 Transport Evidence:** Provider response evidence must be attached to the attempt when a response is available.
|
||||||
|
- **REQ-4-023 Source Projection Rule:** `Source.raw_transcription` is a projection chosen from attempt outcomes and can be repointed by explicit promotion.
|
||||||
|
- **REQ-4-024 Candidate Visibility:** UI must expose candidate attempts with metadata needed for comparative review and selection.
|
||||||
|
|
||||||
|
### Media and Access
|
||||||
|
|
||||||
|
- **REQ-4-030 Ingest Canonicalization:** Stored source bytes may be normalized at ingest (for example orientation correction); stored bytes are the canonical processing source.
|
||||||
|
- **REQ-4-031 Path Safety:** Client-facing media URLs must be generated from controlled application paths only.
|
||||||
|
- **REQ-4-032 Print Media Validation:** Print/export source media must be served through record-validated API routes.
|
||||||
|
|
||||||
|
### Error and UX Contracts
|
||||||
|
|
||||||
|
- **REQ-4-040 Error Envelope:** Service/API errors must map to structured, user-safe error categories and messages.
|
||||||
|
- **REQ-4-041 Partial Failure Visibility:** Mixed page outcomes must be visible at job and page level.
|
||||||
|
- **REQ-4-042 Retry Support:** Failed and cancelled pages must support targeted retranscription without requiring full document recreation.
|
||||||
|
|
||||||
|
### Person Imagery
|
||||||
|
|
||||||
|
- **REQ-6-001 Photo Records:** The system must store reusable `Photo` records that are either owned by a `Person` or unowned for homepage gallery use.
|
||||||
|
- **REQ-6-002 Primary Photo:** At most one photo per owning `Person` may be marked `is_primary`; setting a new primary must clear the previous one.
|
||||||
|
- **REQ-6-003 Primary Reassignment:** Deleting an owner's primary photo must promote a remaining photo of that owner rather than leaving the owner without a primary.
|
||||||
|
|
||||||
|
### Operational Maintenance
|
||||||
|
|
||||||
|
- **REQ-6-010 Queued Maintenance Runs:** Settings-initiated maintenance must persist a `MaintenanceRun` and execute in the worker, not inline in the request that started it.
|
||||||
|
- **REQ-6-011 Maintenance Run Types:** `MaintenanceRun.job_type` must use one of `backup`, `storage_reconciliation`, `gedcom_import`.
|
||||||
|
- **REQ-6-012 Maintenance Status Lifecycle:** `MaintenanceRun.status` must use one of `queued`, `processing`, `succeeded`, `failed`.
|
||||||
|
- **REQ-6-013 Single Claim:** A queued run must be claimed by at most one worker, using a conditional status update rather than read-then-write.
|
||||||
|
- **REQ-6-014 Run History:** Completed runs must retain status, timing, summary, log reference, and error detail, and expose the log for viewing and download.
|
||||||
|
- **REQ-6-015 GEDCOM Upload Import:** Settings must support manual `.ged` upload and queue-backed import into genealogy tables.
|
||||||
|
- **REQ-6-016 GEDCOM Idempotent Upsert:** GEDCOM import must upsert `GenealogyPerson` and `GenealogyFamily` by FamilySearch IDs and avoid duplicate imported citations on re-run.
|
||||||
|
|
||||||
|
### Deployment and Runtime Configuration
|
||||||
|
|
||||||
|
- **REQ-6-020 Production Persistence:** Production must run against PostgreSQL; SQLite remains supported for local development and tests.
|
||||||
|
- **REQ-6-021 Split Worker Deployment:** Production must support running the worker as its own process with the app started at `RUN_EMBEDDED_WORKER=false`.
|
||||||
|
- **REQ-6-022 Runtime Settings Persistence:** Runtime settings edits must persist to the mounted production environment file and survive container restart.
|
||||||
|
- **REQ-6-023 Configuration Contract Sync:** `.env.production.example` must stay synchronized with `Settings` keys and production-safe defaults.
|
||||||
|
- **REQ-6-024 Health Reporting:** The deployed stack must report app and worker health through `/healthz`.
|
||||||
|
- **REQ-6-025 Backup and Restore:** Database and media/config backups must be produced on a host-visible path with a tested restore procedure.
|
||||||
|
|
||||||
|
## Non-Functional Requirements
|
||||||
|
|
||||||
|
- **REQ-4-100 Boundary Integrity:** UI pages/components must not access persistence directly and must call service APIs.
|
||||||
|
- **REQ-4-101 Service Ownership:** Aggregate writes must occur in owning service/workflow modules, not in UI handlers.
|
||||||
|
- **REQ-4-102 Deterministic Loading:** ORM relationship reads in service/UI code must use explicit eager loading compatible with `lazy="raise"`.
|
||||||
|
- **REQ-4-103 Async Safety:** Long-running provider calls must not block UI event handlers directly.
|
||||||
|
- **REQ-4-104 Evidence Durability:** Attempt evidence must survive process restart once the transaction commits.
|
||||||
|
- **REQ-4-105 Test Guardrails:** Architecture boundary tests must remain in place for services and UI boundaries.
|
||||||
|
|
||||||
|
## Requirement Interpretation Notes
|
||||||
|
|
||||||
|
### Status and lifecycle semantics
|
||||||
|
|
||||||
|
- `REQ-4-013` and `REQ-4-015` intentionally bind success to `transcribed`, not a generic `completed`, so docs, tests, and runtime transitions stay consistent.
|
||||||
|
- `REQ-4-016` and `REQ-4-042` distinguish cancellation from failure at page level (`cancelled` vs `failed`) while still allowing targeted retranscription.
|
||||||
|
|
||||||
|
### Evidence semantics
|
||||||
|
|
||||||
|
- `REQ-4-020` through `REQ-4-024` separate authoritative history (`ExecutionAttempt`) from operational projection (`Source.raw_transcription`).
|
||||||
|
- This supports immutable provenance while allowing explicit candidate promotion for operator workflows.
|
||||||
|
|
||||||
|
### Boundary and loading semantics
|
||||||
|
|
||||||
|
- `REQ-4-100` and `REQ-4-101` codify aggregate/service ownership and keep UI out of persistence concerns.
|
||||||
|
- `REQ-4-102` exists to enforce deterministic query shape under `lazy="raise"` and avoid hidden data access in rendering callbacks.
|
||||||
|
|
||||||
|
## Verification Anchors
|
||||||
|
|
||||||
|
- Service boundary enforcement: `tests/test_service_boundaries.py`
|
||||||
|
- UI boundary enforcement: `tests/test_ui_boundaries.py`
|
||||||
|
- Job lifecycle reliability and terminal status behavior: `tests/services/test_workflows_reliability.py`
|
||||||
|
- Evidence append-only and projection behavior: `tests/services/test_store.py`, `tests/services/test_transcription_service.py`
|
||||||
|
- Person photo ownership and primary selection: `tests/services/test_photo_service.py`
|
||||||
|
- Maintenance run lifecycle and worker execution: `tests/services/test_maintenance_service.py`
|
||||||
|
- Runtime settings persistence: `tests/ui/test_runtime_settings_store.py`, `tests/services/test_settings_services.py`
|
||||||
|
- Configuration contract synchronization: `tests/test_meta_contract_guards.py`
|
||||||
|
- Deployment health reporting: `tests/api/test_health.py`
|
||||||
|
|
||||||
|
## Traceability Notes
|
||||||
|
|
||||||
|
- Source of truth for status enums:
|
||||||
|
- `src/transcription/db/models.py`
|
||||||
|
- Source of truth for workflow transitions:
|
||||||
|
- `src/transcription/services/workflows.py`
|
||||||
|
- `src/transcription/services/jobs.py`
|
||||||
|
- Source of truth for attempt evidence writes:
|
||||||
|
- `src/transcription/services/sources.py`
|
||||||
@@ -0,0 +1,488 @@
|
|||||||
|
# Architecture & Code Review Report
|
||||||
|
|
||||||
|
**Repository Target:** `transcription/`
|
||||||
|
**Target Stack:** Python 3.12+ | FastAPI | NiceGUI | SQLModel/SQLAlchemy | Pydantic V2 | asyncio | OpenRouter
|
||||||
|
|
||||||
|
**Review date:** 2026-08-23
|
||||||
|
**Governing procedure:** `.github/skills/python-code-reviewer/skill.md`
|
||||||
|
**Escalations applied:** `.github/skills/evidence-provenance-auditor/skill.md`, `.github/skills/test-effectiveness-auditor/skill.md`
|
||||||
|
**Scope:** 77 Python modules / ~13k LOC under `src/transcription`, 57 test files (377 collected non-external tests), 23 documents under `docs/`, 9 active rule files.
|
||||||
|
|
||||||
|
> **Status: closed.** Every finding below was remediated in the phases following this
|
||||||
|
> review. This document is retained as a record of the reasoning, **not** as a list of
|
||||||
|
> open work, and it is not canonical authority.
|
||||||
|
>
|
||||||
|
> Two recommendations were wrong on contact and were corrected during implementation:
|
||||||
|
> the HIGH-03 fix as written would have stripped root-cause data from `ExecutionAttempt`
|
||||||
|
> provenance, and the HIGH-01 fix needed to preserve per-page durability that the report
|
||||||
|
> did not mention. Where this text and the current code or guard tests disagree, the code
|
||||||
|
> and tests are correct.
|
||||||
|
|
||||||
|
### Verification commands and outcomes
|
||||||
|
|
||||||
|
| Command | Outcome |
|
||||||
|
| :--- | :--- |
|
||||||
|
| `uv run ruff check .` | **Pass** — `All checks passed!` |
|
||||||
|
| `uv run pytest -q -m "not external"` | **Pass** — 377 passed |
|
||||||
|
| `uv run ty check` | **10 diagnostics** — all SQLModel/SQLAlchemy column-descriptor false positives (`services/photos.py` ×8, `tests/test_storage_reconciliation.py` ×2). Advisory only; no suppression strategy exists. |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Executive Summary
|
||||||
|
|
||||||
|
- **Overall health is good.** The codebase has genuine architectural discipline: layered `ui → services → db`, a single Pydantic-V2 settings source, an atomic compare-and-swap job claim, append-only evidence history, and eleven deterministic guard tests that enforce structural rules rather than describing them.
|
||||||
|
- **No Critical findings.** The highest-risk category for this domain — secret leakage into stored provenance — was explicitly audited and **passes**: request headers are never persisted, response headers use an allowlist, and the API key is `SecretStr` end-to-end.
|
||||||
|
- **The top risk is a transaction-atomicity violation on the worker hot path.** Page evidence and terminal job status commit in two separate transactions (`workflows.py:549-598`), directly contradicting `services.instructions.md`. A crash between them leaves a transcript persisted against a job stuck in `PROCESSING`.
|
||||||
|
- **That violation is invisible to the test suite.** The test-effectiveness audit confirms no test can fail on a split commit — the pipeline tests assert the happy-path end state, which passes either way. The invariant is documented and steered but *not enforced*.
|
||||||
|
- **Stale-job recovery is startup-only** (`app.py:79`), with a 30-second staleness threshold. A job orphaned shortly before a fast restart is not recovered and remains `PROCESSING` indefinitely, because the worker only claims `QUEUED` rows.
|
||||||
|
- **The mandated error-presentation boundary is bypassed at 8 sites.** `home_page.py` and `people_page.py` hand-roll `ui.notify(str(exc), ...)`, discarding the `error_id`, category, and suggestion that `error_presenter.show_error` provides. `people_page.py` imports the correct helpers and still bypasses them.
|
||||||
|
- **User-facing output can leak filesystem paths.** `classify_unexpected_error` (`errors.py:94`) interpolates the raw exception into a message rendered in the UI; a SQLAlchemy `OperationalError` embeds the database file path. This contradicts an explicit rule in `error-handling.instructions.md`.
|
||||||
|
- **The retry gate ignores error category** (`workflows.py:185`), so non-retriable faults would be requeued. Currently latent because `worker_max_retries` defaults to `0`.
|
||||||
|
- **Highest-leverage work is enforcement, not refactoring.** Two atomicity tests, a `ty` suppression strategy that lets the pre-commit hook become blocking, and `ruff format --check` in the gate would convert three documented-but-unenforced invariants into deterministic ones.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Executive Architecture Assessment
|
||||||
|
|
||||||
|
**Verdict: architecturally sound with a concentrated reliability gap in the worker's commit boundary.**
|
||||||
|
|
||||||
|
Domain cohesion is strong. The `services/` layer owns transactions and business rules, `ui/` owns presentation, `db/` owns schema, and `providers/` isolates the OpenRouter adapter behind a `TranscriptionProvider` protocol. Dependency direction is correct and — unusually — *mechanically enforced*: `test_service_boundaries.py` AST-scans for service-to-service imports and `test_ui_boundaries.py` scans pages/components for persistence access. Provider details do not leak upward; `workflows.py` imports only the abstract `providers` types, never `openrouter`.
|
||||||
|
|
||||||
|
The evidence/provenance model is the strongest part of the system. `ExecutionAttempt` is genuinely append-only, retries append rather than rewrite, projection writes onto `JobSource` are clearly distinguished from history mutation, and all 14 provenance-auditor invariant checks pass.
|
||||||
|
|
||||||
|
**Top systemic risks:**
|
||||||
|
|
||||||
|
1. **Split commit boundary on the worker path (High).** Evidence durability and job terminal status are two transactions. This is the one place where the architecture's own written contract is contradicted by the implementation, on the hottest path in the system.
|
||||||
|
2. **Recovery is a startup-only, time-thresholded sweep (Medium).** There is no runtime reconciliation, so the self-healing property depends on restart cadence rather than on a bounded interval.
|
||||||
|
3. **Enforcement coverage has known holes (Medium).** Atomicity, error-presenter usage, and formatting are all documented rules with no deterministic test. The repo's own strength — routing invariants into tests — has not been applied to these three.
|
||||||
|
4. **Leaky transaction ownership (Medium).** `workflows.py` reaches into `services.jobs._session_scope()` and `services.sources._session_scope()` — private members of two different services — to open transactions. Session ownership is ambiguous exactly where it most needs to be explicit.
|
||||||
|
5. **A 10-diagnostic type-checker baseline with no suppression policy (Low).** The signal is currently ignorable, which means a real regression would blend into the noise.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Findings by Severity
|
||||||
|
|
||||||
|
### Critical Severity
|
||||||
|
|
||||||
|
**None identified.**
|
||||||
|
|
||||||
|
The secret-leakage check — the only plausible Critical for this system — passes explicitly. `OpenRouterProvider` stores an allowlisted subset of *response* headers only (`providers/evidence.py:130-134`, `SAFE_RESPONSE_HEADERS`); request headers containing `Authorization` are never captured into `TransportEvidence`; and the key is held as `SecretStr` from `config.py` through to the client. Append-only evidence history is likewise intact and test-enforced.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### High Severity
|
||||||
|
|
||||||
|
#### [HIGH-01] Page evidence and terminal job status commit in separate transactions
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/services/workflows.py:549-565` (`_finalize_batch_outcome`), `src/transcription/services/workflows.py:584-598` (`_persist_page_outcome`)
|
||||||
|
- **Problem & Consequence:** `.github/instructions/services.instructions.md` states: *"Never commit transcript updates separately from the paired terminal/retry job status change."* The implementation does exactly that. `_persist_page_outcome` opens its own scope and commits page evidence (line 592-594); `_finalize_batch_outcome` later opens a *second* scope and commits the terminal `JobStatus` (line 558-560). For a single-page job these are two transactions with a window between them. A process crash, container eviction, or unhandled error in that window persists the transcript while the job remains `PROCESSING`. Because the worker only claims `QUEUED` rows, that job is not reprocessed; it is recoverable only by the startup sweep, and only if it has aged past the staleness threshold (see MED-01). The user sees a job that never completes despite the transcription having succeeded and been billed.
|
||||||
|
|
||||||
|
This is a deliberate design tension, not an oversight: `_persist_page_outcome_durably` (line 568-581) wraps the page write in `asyncio.shield` precisely so per-page evidence survives cancellation mid-batch. That goal is correct for *multi*-page jobs. The defect is that the single-page and final-page cases inherit the split unnecessarily.
|
||||||
|
|
||||||
|
- **Recommendation:** Keep per-page durability for intermediate pages, but commit the final page outcome and the terminal status in one transaction.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Before — two scopes, two commits
|
||||||
|
await _persist_page_outcome_durably(job=job, services=services, page=page, session=None)
|
||||||
|
...
|
||||||
|
await _finalize_batch_outcome(job=job, services=services, status=status, session=None)
|
||||||
|
|
||||||
|
# After — final page and terminal status share one transaction
|
||||||
|
async with services.jobs.session_scope() as tx:
|
||||||
|
for page in intermediate_pages:
|
||||||
|
await _persist_page_outcome_durably(job=job, services=services, page=page, session=None)
|
||||||
|
await _write_page_outcome(job=job, services=services, page=final_page, session=tx)
|
||||||
|
await services.jobs.mark_job_status(job.id, status, session=tx)
|
||||||
|
await tx.commit()
|
||||||
|
```
|
||||||
|
|
||||||
|
Pair this with the atomicity test in HIGH-04 so the boundary cannot silently regress.
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [HIGH-02] Mandated error-presentation boundary bypassed at 8 sites
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/ui/pages/home_page.py:212,220,228,255`; `src/transcription/ui/pages/people_page.py:265,321,330,339`
|
||||||
|
- **Problem & Consequence:** `.github/instructions/ui.instructions.md:42` requires all user-facing error display to route through `components/error_presenter.py`. Seven of nine pages comply. These two hand-roll `ui.notify(str(exc), type="negative")`. The consequence is not cosmetic: `show_error` (`error_presenter.py:52-67`) surfaces the correlation `error_id`, the canonical error category, and the actionable `suggestion` field. Bypassing it means a user hitting a failure on the home or people page gets a bare exception string with **no error reference to report**, making these two pages unsupportable in production — precisely the pages most likely to be a user's entry point.
|
||||||
|
|
||||||
|
`people_page.py` already imports `run_ui_action` and `show_error` at lines 28-29 and uses them elsewhere in the same module, so the bypass is inconsistency rather than missing infrastructure.
|
||||||
|
- **Recommendation:** Replace each site with the canonical helper. The unused `summarize_error` helper in `error_presenter.py` (currently a retained orphan — see LOW-07) is the natural fit where a compact string is genuinely needed.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Before
|
||||||
|
except AppError as exc:
|
||||||
|
ui.notify(str(exc), type="negative")
|
||||||
|
|
||||||
|
# After
|
||||||
|
except AppError as exc:
|
||||||
|
show_error(exc)
|
||||||
|
```
|
||||||
|
|
||||||
|
Then close the hole permanently by extending `tests/test_ui_boundaries.py` with an AST check that no module under `PAGES_DIR` calls `ui.notify(...)` with `type="negative"`.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [HIGH-03] Unexpected-error path leaks filesystem paths into user-facing output
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/errors.py:91-98` (line 94), rendered via `src/transcription/ui/components/error_presenter.py:52-67`
|
||||||
|
- **Problem & Consequence:** `classify_unexpected_error` builds `f"Unexpected error during {operation}: {exc}"` and stores it as `AppError.message`. `show_error` renders `error.message` directly to the user. Any exception whose `str()` contains infrastructure detail is therefore displayed verbatim — a SQLAlchemy `OperationalError` embeds the absolute SQLite database path, and an `OSError` from the media layer embeds the storage root. `.github/instructions/error-handling.instructions.md:74` states: *"Never leak … local filesystem paths in user-facing output."* This is the generic catch-all path, so it applies to every unanticipated failure across the application.
|
||||||
|
- **Recommendation:** Split the diagnostic detail from the user-facing message. Log the full exception with the `error_id` as the correlation key; show the user a stable message plus that id.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Before
|
||||||
|
return AppError(
|
||||||
|
f"Unexpected error during {operation}: {exc}",
|
||||||
|
category=ErrorCategory.INTERNAL_UNEXPECTED,
|
||||||
|
...
|
||||||
|
)
|
||||||
|
|
||||||
|
# After
|
||||||
|
error = AppError(
|
||||||
|
f"Unexpected error during {operation}.",
|
||||||
|
category=ErrorCategory.INTERNAL_UNEXPECTED,
|
||||||
|
suggestion="Retry once. If it persists, report the error reference id.",
|
||||||
|
retriable=False,
|
||||||
|
)
|
||||||
|
logger.exception("error_id=%s operation=%s", error.error_id, operation)
|
||||||
|
return error
|
||||||
|
```
|
||||||
|
|
||||||
|
Add a case to `tests/ui/test_error_presenter.py` asserting that a raised `OperationalError` carrying a path does not surface that path in the rendered message.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [HIGH-04] Transaction-atomicity invariants have no enforcing test
|
||||||
|
|
||||||
|
- **Location:** Contract at `.github/instructions/services.instructions.md` §"Workflow Transaction Boundaries"; gap confirmed across `tests/integration/test_pipeline_flow.py:66-160` and `tests/services/test_job_service.py:41-59`
|
||||||
|
- **Problem & Consequence:** The test-effectiveness audit establishes that **neither** Transaction B (transcript + `TRANSCRIBED`) nor Transaction C (retry: `error_detail` + `retry_count` + `QUEUED`) is enforced. The existing pipeline test asserts the final state after a successful run — which passes identically whether the writes shared one commit or used two. To fail on a split-commit regression a test must inject a fault *between* the writes; no such test exists.
|
||||||
|
|
||||||
|
The consequence is that HIGH-01 shipped undetected and any future refactor of `advance_job` can reintroduce it just as silently. This is a *governance* failure rather than a code defect: the repo's stated model is that hard rules belong in deterministic tests, and this rule is the most consequential one that never made the transition.
|
||||||
|
- **Recommendation:** Add `tests/integration/test_pipeline_atomicity.py` with two tests that patch the session to raise after `flush()` but before `commit()`, then assert that *neither* side of the pair is visible in a fresh session. These tests should **fail against the current implementation** and pass once HIGH-01 is fixed — write them first.
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Medium Severity
|
||||||
|
|
||||||
|
#### [MED-01] Stale-job recovery runs only at startup, behind a 30-second threshold
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/app.py:71-81` (`_recover_stale_processing_jobs`), sole caller at `app.py:79` inside `_lifespan`
|
||||||
|
- **Problem & Consequence:** `requeue_stale_processing_jobs` has exactly one call site, in the lifespan startup handler. There is no runtime re-check. The staleness predicate is `updated_at < now - worker_provider_timeout_seconds` (default **30.0s**, `config.py:116`). A job orphaned less than 30 seconds before a fast container restart therefore fails the predicate at the only moment recovery is attempted, and stays `PROCESSING` forever — the worker claims only `QUEUED` rows. It self-heals only on some *later, unrelated* restart. In a frequently-redeployed environment, restarts are exactly when orphans are created, so the recovery window is systematically misaligned with the failure it exists to handle.
|
||||||
|
- **Recommendation:** Move the sweep onto a periodic task in the worker loop (e.g. every `max(30, provider_timeout * 2)` seconds) in addition to the startup call, and derive the threshold from a dedicated `worker_stale_job_seconds` setting rather than reusing the provider timeout, so the two can be tuned independently.
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [MED-02] Retry gate ignores `error_category`, so non-retriable failures would be requeued
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/services/workflows.py:184-194`
|
||||||
|
- **Problem & Consequence:** The `JobStatus.FAILED` branch gates solely on `job.retry_count < settings.worker_max_retries`. It does not consult `error_category` or the `AppError.retriable` flag. `.github/instructions/error-handling.instructions.md` classifies `validation`, `not_found`, and `conflict` as non-retriable; under this gate a malformed source or a missing record would be retried to exhaustion, consuming provider quota on calls that cannot succeed and delaying the terminal failure the user needs to see. There is also no backoff — retries requeue immediately.
|
||||||
|
|
||||||
|
Currently **latent**: `worker_max_retries` defaults to `0` (`config.py:113`) and is commented out in `.env`, so the branch always falls through to the max-retries log. It becomes live the moment anyone enables retries.
|
||||||
|
- **Recommendation:** Gate on retriability *and* count, and add exponential backoff before requeue.
|
||||||
|
|
||||||
|
```python
|
||||||
|
case JobStatus.FAILED:
|
||||||
|
if job.error_category in NON_RETRIABLE_CATEGORIES:
|
||||||
|
logger.error("Job %s failed non-retriably (%s).", job.id, job.error_category)
|
||||||
|
return
|
||||||
|
if job.retry_count < settings.worker_max_retries:
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
Cover with a test that a `validation`-category failure is not requeued even when `worker_max_retries > 0`.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [MED-03] `IntegrityError` on the attempt-number flush is uncaught, risking evidence loss
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/services/sources.py:540-546` (attempt-number computation), `sources.py:587` (unguarded `flush()`)
|
||||||
|
- **Problem & Consequence:** `attempt_number` is derived read-then-write as `MAX(attempt_number) + 1`, and `uq_execution_attempt_number` enforces uniqueness (`db/models.py:507`, documented at `docs/schema.md:273`). The sibling `JobSource` insert *does* catch `IntegrityError` (`sources.py:531-534`), but the `ExecutionAttempt` flush at line 587 does not. Two concurrent attempt writes for the same job source would raise an unhandled `IntegrityError` and lose an evidence row — the one class of data this system exists to preserve. Not currently reachable: the worker is single-instance and processes sources sequentially. It becomes reachable the moment a second worker replica is deployed.
|
||||||
|
- **Recommendation:** Mirror the `JobSource` handling — catch `IntegrityError`, recompute `MAX(attempt_number) + 1`, and retry the insert a bounded number of times, raising a domain error on exhaustion. Note this constraint as a horizontal-scaling precondition in `docs/production-runbook.md`.
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [MED-04] Shutdown timeout is shorter than the provider timeout
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/worker.py:146` (`asyncio.wait_for(worker_task, timeout=2.0)`); provider timeout at `config.py:116` (default 30.0s)
|
||||||
|
- **Problem & Consequence:** Graceful shutdown waits 2 seconds for the worker task, but the stop event is only checked *between* jobs and an in-flight provider call may run for up to 30 seconds. Any shutdown during a provider call therefore cancels mid-flight. Combined with HIGH-01's split commit, a cancellation that lands between the evidence commit and the status commit produces exactly the stuck-`PROCESSING` state described there — so this finding materially raises HIGH-01's probability rather than being independent of it.
|
||||||
|
- **Recommendation:** Derive the shutdown budget from the provider timeout (`worker_provider_timeout_seconds + small_grace`) instead of hardcoding `2.0`, and ensure the container's termination grace period exceeds it. Document both in `docs/production-runbook.md`.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [MED-05] `workflows.py` reaches into two services' private `_session_scope`
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/services/workflows.py:558` (`services.jobs._session_scope()`), `workflows.py:592` (`services.sources._session_scope()`)
|
||||||
|
- **Problem & Consequence:** The orchestration module opens transactions by calling a private member on two different service objects. This is the concrete mechanism behind HIGH-01: because transaction ownership is expressed through a private back-door rather than a declared boundary, nothing in the design makes it obvious that two scopes are being opened for one logical unit of work. It also couples `workflows.py` to a service implementation detail that `test_service_boundaries.py` cannot see (it checks imports, not attribute access).
|
||||||
|
- **Recommendation:** Promote a single explicit transaction entry point — a `session_scope()` on `ServiceBundle`, or a module-level `unit_of_work(services)` helper — and make `workflows.py` use only that. Extend `test_service_boundaries.py` with an AST check forbidding `_session_scope` attribute access outside the owning service module.
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Low Severity
|
||||||
|
|
||||||
|
#### [LOW-01] `hashlib.sha256` over full file bytes runs on the event loop
|
||||||
|
- **Location:** `src/transcription/services/store.py:401`
|
||||||
|
- **Problem & Consequence:** Digest computation is CPU-bound and synchronous inside an `async def`. For large uploads this blocks the loop, stalling both the NiceGUI UI and the worker. Every sibling I/O path in the codebase correctly uses `asyncio.to_thread` (`media_storage.py:43`, `normalization.py:117`, `photos.py:176`, `sources.py:740,753`), so this is an isolated deviation.
|
||||||
|
- **Recommendation:** `digest = await asyncio.to_thread(lambda: hashlib.sha256(file_bytes).hexdigest())`.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
#### [LOW-02] `homepage_store.py` performs synchronous file I/O from async callers
|
||||||
|
- **Location:** `src/transcription/ui/homepage_store.py:25,32`; called from `src/transcription/ui/pages/home_page.py:170`
|
||||||
|
- **Problem & Consequence:** Same class as LOW-01 — reads/writes the homepage JSON directly rather than via `asyncio.to_thread`. Impact is small (a tiny file), but it is a second deviation from an otherwise universal convention.
|
||||||
|
- **Recommendation:** Wrap both calls in `asyncio.to_thread`.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
#### [LOW-03] Worker poll interval is hardcoded outside `Settings`
|
||||||
|
- **Location:** `src/transcription/app.py:62` (`poll_interval_seconds=1.0`)
|
||||||
|
- **Problem & Consequence:** The single operational knob controlling worker latency-vs-load cannot be tuned without a code change, contradicting the otherwise-clean rule that all configuration lives in `config.py` (zero `os.getenv` calls exist outside it).
|
||||||
|
- **Recommendation:** Add `worker_poll_interval_seconds: float = 1.0` to `Settings` and read it at the call site.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
#### [LOW-04] `_build_request_manifest` returns `None` silently, producing incomplete evidence
|
||||||
|
- **Location:** `src/transcription/providers/openrouter.py:347`
|
||||||
|
- **Problem & Consequence:** When `source_reference is None` the manifest is skipped with no log line. The attempt is still recorded but its provenance is quietly incomplete, and there is no signal that it happened — the failure mode is undetectable after the fact.
|
||||||
|
- **Recommendation:** Log at `warning` with the job/source identifiers before returning `None`, so incomplete provenance is at least attributable.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
#### [LOW-05] Ten `ty` diagnostics with no suppression strategy
|
||||||
|
- **Location:** `src/transcription/services/photos.py` (8), `tests/test_storage_reconciliation.py` (2)
|
||||||
|
- **Problem & Consequence:** All ten are SQLModel/SQLAlchemy false positives — column descriptors are typed as their Python value type (`UUID`, `datetime`, `bool`), so `.is_()`, `.asc()`, `func.count()`, and `group_by()` appear invalid. Because there is no suppression policy, the pre-commit hook must run `ty` in advisory mode, which means a *genuine* new type error would print alongside the known ten and block nothing.
|
||||||
|
- **Recommendation:** Add targeted `# ty: ignore[...]` comments with a one-line rationale at each of the ten sites, then flip the pre-commit hook to blocking. This converts a permanently-ignored signal into a real gate.
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
#### [LOW-06] `ruff format` is not enforced; 35 files have drifted
|
||||||
|
- **Location:** `.pre-commit-config.yaml`, `ruff.toml`
|
||||||
|
- **Problem & Consequence:** `ruff check` is blocking but `ruff format --check` is absent from the gate, so formatting drift accumulates silently and inflates unrelated diffs whenever anyone does run the formatter.
|
||||||
|
- **Recommendation:** Run `uv run ruff format .` once as a single isolated commit, then add `ruff format --check` to the pre-commit gate.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
#### [LOW-07] Four retained orphans, all recorded as "uncertain — follow-up"
|
||||||
|
- **Location:** `tests/test_orphan_sweep.py:33-52` (`KNOWN_ORPHANS`): `BenchmarkManifest`, `dispose_all_engines`, `refresh_engine`, `summarize_error`
|
||||||
|
- **Problem & Consequence:** Every entry carries the weakest possible justification. `summarize_error` is the notable one: it is an unused helper in `error_presenter.py` *while two pages hand-roll error display* (HIGH-02) — the orphan and the boundary violation are the same problem viewed from two directions. `dispose_all_engines` / `refresh_engine` are plausibly test-support utilities and should be classified as such rather than left uncertain.
|
||||||
|
- **Recommendation:** Resolve each to a definite outcome — `summarize_error` becomes used by the HIGH-02 fix; classify the engine helpers as test-support or delete them; decide on `BenchmarkManifest`.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
#### [LOW-08] Orphan sweep only scans module-level public definitions
|
||||||
|
- **Location:** `tests/test_orphan_sweep.py`
|
||||||
|
- **Problem & Consequence:** Methods and private functions are out of scope, so dead code inside classes — the most common kind in a service-oriented codebase — is structurally invisible to the sweep.
|
||||||
|
- **Recommendation:** Extend the AST walk to public methods on service classes, seeding `KNOWN_ORPHANS` with the current result set to keep the change non-breaking.
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
#### [LOW-09] f-string interpolation in logging calls
|
||||||
|
- **Location:** `src/transcription/services/workflows.py:193` and similar sites
|
||||||
|
- **Problem & Consequence:** `logger.error(f"Job {job.id} has failed...")` formats eagerly regardless of level and prevents structured-logging backends from grouping by template. Ruff's `flake8-logging-format` (`G`) rules are not enabled, so this is unenforced.
|
||||||
|
- **Recommendation:** Use `logger.error("Job %s has failed and reached max retries.", job.id)` and enable ruff rule set `G`.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
#### [LOW-10] Low-signal and always-true assertions in the test suite
|
||||||
|
- **Location:** `tests/test_traceability.py:54-57`; `tests/integration/test_pipeline_flow.py:135-140,446-452`; `tests/test_orphan_sweep.py:119`; `tests/services/test_workflows_reliability.py:105,178,241,317,375`
|
||||||
|
- **Problem & Consequence:** Per the test-effectiveness audit: `test_traceability.py:54-57` asserts properties of dict literals defined in the same file (can only fail if the test itself is edited); `assert processed is True` in the pipeline tests is unfalsifiable because `read_job` raises rather than returning `None`; the `>= 200` orphan threshold is a historical snapshot that tolerates ±40 drift; and the `assert result is not None` guards are shadowed by the attribute assertions that follow. Together these overstate effective coverage.
|
||||||
|
- **Recommendation:** Apply the prune/strengthen backlog in §6 (Testing).
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
#### [LOW-11] Wall-clock timing dependencies risk CI flakiness
|
||||||
|
- **Location:** `tests/services/test_workflows_reliability.py:157-196` (real `time.sleep(0.40)`, upper bound `< 540ms` with only 10% slack); `test_workflows_reliability.py:341` (`asyncio.wait_for(..., timeout=2)`)
|
||||||
|
- **Problem & Consequence:** On a loaded CI runner, a 200ms asyncio task plus 400ms blocking setup can exceed the 540ms bound, producing false failures that erode trust in the suite.
|
||||||
|
- **Recommendation:** Widen the slack factor to `0.8` or replace the blocking sleep with a controlled clock mock.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Architectural Drift & Gap Analysis
|
||||||
|
|
||||||
|
`Direction` is `doc->code` (implementation must change to match documented intent) or `code->doc` (an undocumented but repeatable convention that should be formalized).
|
||||||
|
|
||||||
|
| Area / Component | Direction | Documented / Intended Rule | Actual Implementation State | Severity | Recommended Resolution |
|
||||||
|
| :--- | :--- | :--- | :--- | :--- | :--- |
|
||||||
|
| Worker commit boundary | `doc->code` | `services.instructions.md`: never commit transcript updates separately from the paired terminal status change | `workflows.py:549-598` commits page evidence and terminal status in two separate sessions | High | Fix per HIGH-01; enforce per HIGH-04 |
|
||||||
|
| UI error presentation | `doc->code` | `ui.instructions.md:42`: all user-facing error display routes through `error_presenter.py` | 8 hand-rolled `ui.notify` sites in `home_page.py` and `people_page.py` | High | Fix per HIGH-02; add AST guard to `test_ui_boundaries.py` |
|
||||||
|
| Unexpected-error messaging | `doc->code` | `error-handling.instructions.md:74`: never leak local filesystem paths in user-facing output | `errors.py:94` interpolates raw `exc` into the rendered message | High | Fix per HIGH-03 |
|
||||||
|
| Retry policy | `doc->code` | `error-handling.instructions.md`: validation / not_found / conflict are non-retriable | `workflows.py:185` gates on retry count only | Medium | Fix per MED-02 |
|
||||||
|
| Stale-job recovery | `code->doc` | Not documented as startup-only or time-thresholded | Single startup call site; 30s threshold reuses the provider timeout | Medium | Fix per MED-01, then document the recovery contract in `docs/production-runbook.md` |
|
||||||
|
| Transaction ownership | `code->doc` | `services.instructions.md` assigns transaction ownership to services | `workflows.py` opens scopes via two services' private `_session_scope` | Medium | Fix per MED-05; document the single unit-of-work entry point |
|
||||||
|
| Blocking-I/O convention | `code->doc` | Not stated as a rule; followed at 5 of 7 sites | `store.py:401` and `homepage_store.py:25,32` deviate | Low | Fix per LOW-01/LOW-02, then state the `asyncio.to_thread` rule in `services.instructions.md` |
|
||||||
|
| Configuration centralization | `code->doc` | Zero `os.getenv` outside `config.py` — a real, held convention | Held everywhere except the hardcoded `poll_interval_seconds` at `app.py:62` | Low | Fix per LOW-03, then formalize the rule and add a deterministic guard |
|
||||||
|
| Type-check baseline | `code->doc` | No documented policy for `ty` diagnostics | 10 tolerated false positives; hook is advisory-only | Low | Adopt the suppression strategy in LOW-05 and document it |
|
||||||
|
| Formatting | `code->doc` | `ruff.toml` configures the formatter | `ruff format --check` absent from the gate; 35 files drifted | Low | Fix per LOW-06 |
|
||||||
|
| Dependency pin | — | `docs/production-runbook.md` "Dependency upgrade policy" records the exact `nicegui==3.13.0` pin as a deliberate stability decision | Matches | — | **No action** — correctly documented, not a defect |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Invariant Inventory & Routing Recommendations
|
||||||
|
|
||||||
|
| Invariant / Constraint | Current Location | Recommended Target Layer | Rationale |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| Transcript + terminal status commit atomically | Instructions only | **Deterministic test** (`tests/integration/test_pipeline_atomicity.py`) | Highest-consequence rule in the system with zero enforcement; steering alone already failed to prevent HIGH-01 |
|
||||||
|
| Retry writes commit atomically | Instructions only | **Deterministic test** (same file) | Same class; a partial retry commit corrupts `retry_count` accounting |
|
||||||
|
| All UI errors route through `error_presenter` | Instructions (`ui.instructions.md:42`) | **Deterministic test** (extend `test_ui_boundaries.py`) | Mechanically checkable via AST; 8 live violations prove instructions are insufficient here |
|
||||||
|
| No filesystem paths in user-facing output | Instructions (`error-handling.instructions.md:74`) | **Deterministic test** (extend `tests/ui/test_error_presenter.py`) | Checkable by asserting a path-bearing exception does not surface its path |
|
||||||
|
| Non-retriable categories are never requeued | Instructions | **Deterministic test** (`tests/services/test_workflows_reliability.py`) | Latent today; a test freezes the correct behavior before retries are enabled |
|
||||||
|
| Blocking I/O runs via `asyncio.to_thread` | Convention only (5/7 sites) | **Instructions** (`services.instructions.md`) | Judgment-dependent (thresholds vary by payload size); steering fits better than a hard test |
|
||||||
|
| Transaction opened through one owned entry point | Convention, violated | **Instructions + test** | Document the entry point; AST-guard against `_session_scope` access outside its owning module |
|
||||||
|
| Append-only `ExecutionAttempt` history | Docs + 3 tests | **Keep as-is** | Correctly routed and genuinely mutation-sensitive; the model to imitate |
|
||||||
|
| Service/UI boundary rules | Instructions + 2 AST tests | **Keep as-is** | Working exactly as intended |
|
||||||
|
| Status vocabulary conformance | `docs/schema.md` + contract guards | **Keep as-is** | Enum drift would fail the suite |
|
||||||
|
| No secrets in stored evidence | Docs + provenance skill + allowlist in code | **Keep as-is** | Allowlist is the right mechanism — fails closed by construction |
|
||||||
|
| `ty` diagnostic suppression policy | Nonexistent | **Docs + blocking hook** | Needs a written rationale per suppression before the gate can be trusted |
|
||||||
|
| NiceGUI exact pin | `docs/production-runbook.md` | **Keep as-is** | Deliberate, documented, correctly excluded from review findings |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. Stack-Specific Analysis
|
||||||
|
|
||||||
|
### Python 3.12+ Best Practices
|
||||||
|
Modern syntax is used consistently: `X | None` unions throughout, builtin generics, no `typing.List`/`Optional` legacy forms, `pathlib` over `os.path`. Type-annotation coverage is high, with no bare `Any` on public service signatures. Broad `except Exception` appears where it belongs — the per-page handler at `workflows.py:352` deliberately isolates one page's failure from the batch, which is correct. `# noqa: PLR0915` / `PLR1702` are used sparingly and consistently. Minor gaps: f-strings in logging (LOW-09), and two blocking-I/O deviations (LOW-01/LOW-02).
|
||||||
|
|
||||||
|
### FastAPI
|
||||||
|
Lifespan is handled correctly via an `asynccontextmanager` `_lifespan` (`app.py:36-68`) rather than deprecated `@app.on_event`. Routers are domain-organized with typed path/query parameters and `response_model` declarations. Error handling is centralized through `register_error_handlers`, and the full internal→canonical category mapping is round-trip tested at the HTTP layer (`tests/api/test_error_responses.py:59-95`). `print_api.py:42-49` performs correct `relative_to`-based path containment for media serving. No blocking calls found in `async def` route handlers.
|
||||||
|
|
||||||
|
### NiceGUI
|
||||||
|
Separation of concerns is good — pages delegate to services and `test_ui_boundaries.py` mechanically prevents persistence access from pages and components. Client state is client-scoped; no cross-session global-state leaks found. API usage is correct for the pinned 3.13.0 release. The two defects are the error-presenter bypass (HIGH-02) and synchronous file I/O in `homepage_store.py` (LOW-02).
|
||||||
|
|
||||||
|
### SQLModel & SQLAlchemy
|
||||||
|
The strongest layer. `lazy="raise"` is declared on relationships and correctly paired with `expire_on_commit=False`, which together make N+1 access a loud failure rather than a silent performance cost — no N+1 patterns found. The job claim is a genuine atomic compare-and-swap (`jobs.py:212-222`: conditional `UPDATE ... WHERE status = QUEUED ... RETURNING`), which is the correct primitive and correctly implemented. Hot-path indexes are declared and test-verified (`test_db.py:131`). Cross-dialect portability is handled for SQLite and PostgreSQL. Weaknesses are transaction *ownership* (MED-05, HIGH-01) rather than query construction, plus the uncaught `IntegrityError` at MED-03.
|
||||||
|
|
||||||
|
### Pydantic V2 & Settings
|
||||||
|
Fully migrated — no `@validator`, no `Config` class, no `.dict()` or `parse_obj` anywhere. `model_config = ConfigDict(...)` and `@field_validator` are used correctly. `config.py` is a clean single source of truth: **zero** `os.getenv` calls exist outside it, `.env` is untracked and gitignored, and the API key is `SecretStr` end-to-end. The only deviation is the hardcoded poll interval (LOW-03).
|
||||||
|
|
||||||
|
### Asyncio Workers
|
||||||
|
Task lifecycle is handled properly: task references are retained (no GC risk), `CancelledError` is re-raised rather than swallowed, the provider call happens outside any DB transaction, timeouts resolve to terminal states, and there is no tight polling spin. `_persist_page_outcome_durably`'s use of `asyncio.shield` (`workflows.py:568-581`) is a thoughtful durability mechanism. The defects are the split commit boundary (HIGH-01), the shutdown-vs-provider timeout mismatch (MED-04), and startup-only recovery (MED-01).
|
||||||
|
|
||||||
|
### OpenRouter / Adapter Boundary
|
||||||
|
Encapsulation is clean — `workflows.py` imports only abstract types from `providers`, never `openrouter` directly, so provider specifics do not leak into business logic. The `AsyncClient` is shared with configured timeouts and is properly closed: `worker.py:248,271` → `services.aclose()` → `sources.aclose()` (`sources.py:129-133`) → provider `aclose()` (`openrouter.py:86-87,233-235`). Responses are Pydantic-validated. **All 14 evidence-provenance-auditor invariant checks pass**, including the critical one: the API key is never persisted, request headers are never stored, and `TransportEvidence` captures response headers through an explicit allowlist (`evidence.py:130-134`). Only LOW-04 applies here.
|
||||||
|
|
||||||
|
### Testing & Quality Tooling
|
||||||
|
377 tests pass with `-m "not external"`. The project test contract is honored: `--strict-markers` with all three markers (`unit`, `integration`, `external`) declared, `asyncio_mode = "strict"` with **every** `async def test_` correctly decorated across all 17 async test files, `external` properly excluded from default runs, and **no unawaited-coroutine warnings** — the `filterwarnings` error promotion is clean.
|
||||||
|
|
||||||
|
Contract coverage is genuinely strong for structural rules. Confirmed *mutation-sensitive* enforcement exists for: append-only evidence history (3 independent tests, including full before/after field-tuple snapshots), stuck-in-`PROCESSING` prevention, the complete 10-category error mapping, and both boundary rules.
|
||||||
|
|
||||||
|
The critical gap is transaction atomicity (HIGH-04) — the audit verdict is **"Effective with Conditions / Go with Conditions"**, blocking on the two missing atomicity tests. Secondary items are the low-signal assertions (LOW-10) and wall-clock flakiness (LOW-11).
|
||||||
|
|
||||||
|
**Prune/strengthen backlog:**
|
||||||
|
|
||||||
|
| Priority | Task | Location |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| High | Add Transaction B atomicity test (fault injected between transcript and status writes) | new `tests/integration/test_pipeline_atomicity.py` |
|
||||||
|
| High | Add Transaction C atomicity test (retry: `error_detail` + `retry_count` + `QUEUED`) | same file |
|
||||||
|
| Medium | Delete tautological assertions on same-file dict literals | `tests/test_traceability.py:54-57` |
|
||||||
|
| Medium | Remove unfalsifiable `assert processed is True` | `tests/integration/test_pipeline_flow.py:135-140,446-452` |
|
||||||
|
| Medium | Replace `>= 200` snapshot threshold with set-membership assertion | `tests/test_orphan_sweep.py:119` |
|
||||||
|
| Medium | Assert mapped test files contain ≥1 test, not merely that they exist | `tests/test_traceability.py:59-60` |
|
||||||
|
| Low | Widen timing slack or mock the clock | `tests/services/test_workflows_reliability.py:157-196` |
|
||||||
|
| Low | Drop `assert result is not None` guards shadowed by following assertions | `tests/services/test_workflows_reliability.py:105,178,241,317,375` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. Duplication & Consolidation Report
|
||||||
|
|
||||||
|
| Pattern / Duplication | Locations | Proposed Canonical Home | Estimated Lines Removed |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| Hand-rolled `ui.notify(str(exc), type="negative")` | `home_page.py:212,220,228,255`; `people_page.py:265,321,330,339` | `ui/components/error_presenter.py::show_error` (already exists) | ~16 |
|
||||||
|
| Optional-session `if session is None: async with _session_scope()` preamble | `workflows.py:557-561`, `workflows.py:591-595`, and sibling service write paths | `services/base.py::unit_of_work(services, session)` context manager | ~30 |
|
||||||
|
| Synchronous I/O not wrapped in `asyncio.to_thread` | `store.py:401`, `homepage_store.py:25,32` | `services/base.py::run_blocking` helper | ~6 |
|
||||||
|
| Read-then-increment `MAX(n) + 1` with uniqueness retry | `sources.py:540-546` (uncaught) vs `sources.py:531-534` (caught) | `services/base.py::insert_with_sequence_retry` | ~20 |
|
||||||
|
|
||||||
|
### Proposed Canonical Abstractions
|
||||||
|
|
||||||
|
```python
|
||||||
|
# src/transcription/services/base.py
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def unit_of_work(
|
||||||
|
services: ServiceBundle,
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> AsyncIterator[AsyncSession]:
|
||||||
|
"""Single transaction entry point. Yields a session and commits once on clean exit.
|
||||||
|
|
||||||
|
Replaces the `if session is None: async with X._session_scope()` preamble and the
|
||||||
|
private-member access at workflows.py:558,592. Makes the two-commit split of
|
||||||
|
HIGH-01 structurally hard to reintroduce.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
async def run_blocking[T](fn: Callable[[], T]) -> T:
|
||||||
|
"""Run a CPU- or disk-bound callable off the event loop."""
|
||||||
|
return await asyncio.to_thread(fn)
|
||||||
|
|
||||||
|
|
||||||
|
async def insert_with_sequence_retry(
|
||||||
|
session: AsyncSession,
|
||||||
|
*,
|
||||||
|
build: Callable[[int], SQLModel],
|
||||||
|
next_value: Callable[[], Awaitable[int]],
|
||||||
|
attempts: int = 3,
|
||||||
|
) -> SQLModel:
|
||||||
|
"""Insert a row carrying a derived sequence number, retrying on IntegrityError."""
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. Meta-Tooling & Instruction Update Recommendations
|
||||||
|
|
||||||
|
1. **Add `tests/integration/test_pipeline_atomicity.py`** (HIGH-04). The single highest-value enforcement change. Write it before fixing HIGH-01 so it demonstrably fails first.
|
||||||
|
2. **Extend `tests/test_ui_boundaries.py`** with an AST check forbidding `ui.notify(..., type="negative")` in `PAGES_DIR`, routing all error display through `error_presenter`. Converts `ui.instructions.md:42` from steering into enforcement.
|
||||||
|
3. **Extend `tests/ui/test_error_presenter.py`** with a case asserting that a path-bearing exception does not surface its path, enforcing `error-handling.instructions.md:74`.
|
||||||
|
4. **Adopt a `ty` suppression policy** — targeted `# ty: ignore[...]` with rationale at the 10 known sites, documented in `docs/` — then **flip the pre-commit `ty` hook from advisory to blocking**. Until this happens the type checker provides no gate.
|
||||||
|
5. **Add `ruff format --check` to the pre-commit gate**, preceded by one isolated formatting commit across the 35 drifted files.
|
||||||
|
6. **Enable ruff rule set `G`** (`flake8-logging-format`) to catch f-string logging (LOW-09).
|
||||||
|
7. **Extend `tests/test_orphan_sweep.py`** to public methods on service classes, seeding `KNOWN_ORPHANS` with current results (LOW-08). Then resolve all four existing "uncertain" entries to definite outcomes.
|
||||||
|
8. **Extend `tests/test_service_boundaries.py`** with an AST check forbidding `_session_scope` attribute access outside its owning service module (MED-05). Also address the noted classification gap: the test excludes orchestration modules by hardcoded stem name (`store`, `workflows`, `__init__`), so a new orchestration module under a different name would be misclassified as a service.
|
||||||
|
9. **Update `.github/instructions/services.instructions.md`** to state the `asyncio.to_thread` rule for blocking I/O and to name the single `unit_of_work` transaction entry point.
|
||||||
|
10. **Update `docs/production-runbook.md`** with the stale-job recovery contract (interval, threshold, and its relationship to the container termination grace period), and note single-worker as a current precondition until MED-03 is fixed.
|
||||||
|
11. **Note for `test_ui_boundaries.py`:** the forbidden-import lists are fixed string sets, so a future persistence helper under a new name would escape the check. Consider inverting to an allowlist of permitted imports for pages.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. Prioritized Dependency-Ordered Action Plan
|
||||||
|
|
||||||
|
**Phase 1: Blocking fixes**
|
||||||
|
1. Write the two atomicity tests (HIGH-04) and confirm they **fail** against current `main`.
|
||||||
|
2. Fix the split commit boundary (HIGH-01) and confirm the tests now pass.
|
||||||
|
3. Fix the filesystem-path leak in `classify_unexpected_error` (HIGH-03).
|
||||||
|
4. Replace the 8 hand-rolled error notifications with `show_error` (HIGH-02).
|
||||||
|
|
||||||
|
**Phase 2: Enforcement hardening**
|
||||||
|
5. Add the `ui.notify` AST guard and the path-leak presenter test, locking in items 3-4.
|
||||||
|
6. Adopt the `ty` suppression policy and make the pre-commit hook blocking (LOW-05).
|
||||||
|
7. Run `ruff format .` as an isolated commit, then add `ruff format --check` to the gate (LOW-06).
|
||||||
|
8. Enable ruff rule set `G` and fix the resulting logging call sites (LOW-09).
|
||||||
|
|
||||||
|
**Phase 3: Reliability & concurrency**
|
||||||
|
9. Move stale-job recovery to a periodic worker task with a dedicated setting (MED-01).
|
||||||
|
10. Gate retries on `error_category` and add backoff (MED-02) — do this before ever raising `worker_max_retries` above 0.
|
||||||
|
11. Derive the shutdown budget from the provider timeout (MED-04).
|
||||||
|
12. Handle `IntegrityError` on the attempt-number flush (MED-03) — a hard precondition for running more than one worker replica.
|
||||||
|
13. Move `sha256` and homepage-store I/O off the event loop (LOW-01, LOW-02); move the poll interval into `Settings` (LOW-03).
|
||||||
|
|
||||||
|
**Phase 4: Consolidation & refactoring**
|
||||||
|
14. Introduce `unit_of_work` and migrate `workflows.py` off private `_session_scope` access (MED-05); add the corresponding boundary guard.
|
||||||
|
15. Extract `run_blocking` and `insert_with_sequence_retry` (§7).
|
||||||
|
16. Prune the low-signal assertions and reduce timing flakiness (LOW-10, LOW-11).
|
||||||
|
|
||||||
|
**Phase 5: Non-blocking governance/documentation depth**
|
||||||
|
17. Extend the orphan sweep to methods and resolve the four uncertain orphans (LOW-07, LOW-08).
|
||||||
|
18. Update `services.instructions.md` and `docs/production-runbook.md` per §8 items 9-10.
|
||||||
|
19. Log incomplete request manifests (LOW-04).
|
||||||
|
20. Consider inverting the UI boundary check to an allowlist.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. Preserved Strengths
|
||||||
|
|
||||||
|
- **Evidence and provenance integrity is exemplary.** All 14 provenance-auditor invariants pass. `ExecutionAttempt` history is genuinely append-only, retries append rather than rewrite, and projection writes are cleanly distinguished from history mutation. Three independent tests — including full before/after field-tuple snapshots — make any mutation regression fail loudly.
|
||||||
|
- **Secret hygiene is correct by construction.** The response-header **allowlist** (`evidence.py:130-134`) fails closed: a newly-introduced sensitive header is excluded by default rather than requiring someone to remember to block it. Request headers are never captured, and `SecretStr` is used end-to-end.
|
||||||
|
- **Atomic job claiming.** `jobs.py:212-222` uses a conditional `UPDATE ... WHERE status = QUEUED ... RETURNING` — a true compare-and-swap that makes double-claiming impossible under concurrency, rather than the common read-then-write race.
|
||||||
|
- **`lazy="raise"` paired with `expire_on_commit=False`.** This combination turns accidental lazy loads into immediate errors instead of silent N+1 queries, and it is the reason no N+1 patterns exist in the codebase. Keep it.
|
||||||
|
- **Architectural rules are mechanically enforced, not merely documented.** AST-based boundary tests for service-to-service imports and UI persistence access are the right pattern; this review's main recommendation is simply to apply that same pattern to three more rules.
|
||||||
|
- **Configuration discipline.** Zero `os.getenv` calls outside `config.py`, `.env` untracked and gitignored, clean Pydantic V2 throughout with no V1 residue.
|
||||||
|
- **Path containment on media serving.** `print_api.py:42-49` uses proper `relative_to` validation rather than string prefix matching.
|
||||||
|
- **Async worker fundamentals.** Task references retained, `CancelledError` re-raised, provider calls outside DB transactions, timeouts resolving to terminal states, no tight polling loop. `asyncio.shield` in `_persist_page_outcome_durably` is a genuinely thoughtful durability mechanism — the fix in HIGH-01 should preserve it for intermediate pages.
|
||||||
|
- **Test contract rigor.** `--strict-markers`, `asyncio_mode = "strict"` honored across all 17 async test files with no missing decorators, and coroutine-never-awaited promoted to a hard error with a clean run.
|
||||||
@@ -0,0 +1,896 @@
|
|||||||
|
# Architecture & Code Review Report
|
||||||
|
|
||||||
|
**Repository Target:** `C:\Github\transcription\`
|
||||||
|
**Target Stack:** Python 3.12+ | FastAPI | NiceGUI | SQLModel/SQLAlchemy | Pydantic V2 | asyncio | OpenRouter
|
||||||
|
**Review Date:** 2026-09-02
|
||||||
|
**Canonical Baseline:** V6.1 (`docs/index.md`)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 0. Verification Commands and Outcomes
|
||||||
|
|
||||||
|
All four commands were executed in this checkout before any finding was written. This report
|
||||||
|
records the exact outcomes rather than assuming them.
|
||||||
|
|
||||||
|
| Command | Outcome |
|
||||||
|
| :--- | :--- |
|
||||||
|
| `uv run pytest -q -m "not external"` | **410 passed, 0 failed, 0 errors** (exit 0) |
|
||||||
|
| `uv run ruff check .` | **All checks passed!** |
|
||||||
|
| `uv run ruff format --check .` | **191 files already formatted** |
|
||||||
|
| `uv run ty check` | **All checks passed!** |
|
||||||
|
|
||||||
|
The stated green baseline is real. No finding below is a test failure; every finding is a
|
||||||
|
behavior, contract, or guard-coverage defect that the passing suite does not detect.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Executive Summary
|
||||||
|
|
||||||
|
- **The system's core evidence guarantees hold.** `ExecutionAttempt` is genuinely append-only,
|
||||||
|
attempt numbering is allocated with bounded conflict retry, transport evidence is captured at
|
||||||
|
the HTTP boundary before SDK parsing, and header persistence uses a true allowlist. Provenance
|
||||||
|
invariant families A–E and G pass.
|
||||||
|
- **Both competing atomicity invariants in `services/workflows.py` are real and both guards
|
||||||
|
genuinely enforce them.** I injected-fault-verified the tests rather than trusting the
|
||||||
|
docstrings: `test_pipeline_atomicity.py` fails on a split final-page commit, and
|
||||||
|
`test_workflows_reliability.py:318` reads intermediate attempts through a *separate session*,
|
||||||
|
so it would fail if intermediate pages stopped committing individually.
|
||||||
|
- **The most significant defect is a privacy leak that a prior review believed it had closed.**
|
||||||
|
The 2026-08-23 review moved root-cause text out of `AppError.message` into `AppError.detail`
|
||||||
|
to keep filesystem paths away from users. That text now reaches users anyway, because the UI
|
||||||
|
renders `ExecutionAttempt.error_detail` verbatim (HIGH-01). The leak was relocated, not closed.
|
||||||
|
- **A second, independent path leak exists in five explicit `raise` sites** that the existing
|
||||||
|
guard never covered — it tests only `classify_unexpected_error` (HIGH-02).
|
||||||
|
- **Provenance invariant family F (path safety) fails**, and it fails *inconsistently within one
|
||||||
|
file*: `sources_page.py:443` carefully sanitizes a stored path through
|
||||||
|
`public_media_path_label`, then `sources_page.py:484` dumps raw `error_detail` forty lines later.
|
||||||
|
- **The orphan sweep does not do what its docstring claims.** It matches definitions by bare name,
|
||||||
|
so an entirely dead *module* passes whenever its function names collide with live ones.
|
||||||
|
`ui/pages/tags_page.py` is the proof: 93 lines never imported by anything (MED-01/LOW-01).
|
||||||
|
- **On the three flagged open items:** the V4/V6.1 doc drift is confirmed (MED-02); the
|
||||||
|
`.env.production` coupling is real but currently correct and loud-failing, so Medium not High
|
||||||
|
(MED-03); and the Tags roadmap is **right** — the route is genuinely not registered, so the
|
||||||
|
module is dead code rather than a live retired route.
|
||||||
|
- **Two latent concurrency defects carry ordering constraints** and must be fixed *before* the
|
||||||
|
changes that would make them live (MED-04, MED-05), not after.
|
||||||
|
- **Guidance-file accuracy:** the recently revised `.github/instructions/*` files were verified
|
||||||
|
against code rather than trusted. They are accurate as written; the code is what diverges from
|
||||||
|
them. The one exception is that `error-handling.instructions.md` states a `detail` rule the UI
|
||||||
|
layer has never followed, which makes it an unenforced claim rather than a wrong one.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Executive Architecture Assessment
|
||||||
|
|
||||||
|
**Verdict: architecturally sound, with a concentrated failure in the *last mile* of error
|
||||||
|
presentation.**
|
||||||
|
|
||||||
|
Domain cohesion and dependency direction are good and, unusually, mechanically enforced.
|
||||||
|
`test_service_boundaries.py` and `test_ui_boundaries.py` AST-scan for violations using
|
||||||
|
*allowlists* rather than blocklists, which is the correct choice — a newly added persistence
|
||||||
|
helper cannot slip through under an unlisted name. `workflows.py` imports only the abstract
|
||||||
|
`providers` types and never `openrouter`, so provider details genuinely stop at the adapter.
|
||||||
|
Transaction ownership is explicit and well-reasoned: `ServiceBase._finalize` commits for
|
||||||
|
service-owned sessions and flushes for caller-owned ones, which is what lets orchestration
|
||||||
|
modules compose multi-aggregate writes without services importing each other.
|
||||||
|
|
||||||
|
The evidence layer is the strongest part of the system and shows real care. The distinction
|
||||||
|
between transport response, SDK-parsed response, and normalized metadata is maintained in code,
|
||||||
|
not just in prose — `_CapturingAsyncClient` exists specifically to retain the exact wire body
|
||||||
|
before the SDK can discard unknown fields, and `TransportEvidence(response_received=False)`
|
||||||
|
explicitly represents "no response was received" rather than conflating it with an empty one.
|
||||||
|
|
||||||
|
The weakness is at the boundary where internal diagnostic text becomes pixels. Every layer
|
||||||
|
*below* the UI respects the message/detail split; the UI layer reads the internal field directly
|
||||||
|
and renders it. The architecture defines the contract correctly and then has no enforcement at
|
||||||
|
the one layer that violates it.
|
||||||
|
|
||||||
|
**Top systemic risks:**
|
||||||
|
|
||||||
|
1. **Internal diagnostic text reaches users through the evidence display path** (HIGH-01). The
|
||||||
|
rule is documented in three places and enforced in none of them at the UI boundary.
|
||||||
|
2. **Path-safety discipline is applied per-call-site rather than structurally** (HIGH-02, HIGH-01).
|
||||||
|
It is correct wherever someone remembered; there is no guard that makes forgetting fail.
|
||||||
|
3. **Guard coverage is narrower than guard docstrings claim.** Two guards
|
||||||
|
(`test_orphan_sweep.py`, `test_errors.py`) assert something meaningfully weaker than the
|
||||||
|
invariant they are named for, which converts them into a false sense of enforcement.
|
||||||
|
4. **Worker safety currently rests on single-process sequential execution, not on configuration**
|
||||||
|
(MED-04, MED-05). Nothing is wrong today; two plausible future changes each make something wrong.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Findings by Severity
|
||||||
|
|
||||||
|
### Critical Severity
|
||||||
|
|
||||||
|
*None.* No evidence loss, append-only violation, secret leakage, or silent-wrong-output defect
|
||||||
|
was found. The candidates in this class (provider evidence mis-attribution, stale-job double
|
||||||
|
processing) are latent and are reported at High/Medium with their unblocking conditions.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### High Severity
|
||||||
|
|
||||||
|
#### [HIGH-01] Internal-only `error_detail` is rendered directly to users, reopening the leak the 2026-08-23 fix was meant to close
|
||||||
|
|
||||||
|
- **Location:**
|
||||||
|
- Write side: `src/transcription/errors.py:99-139` (`classify_unexpected_error` → `detail`, `format_error_detail` → persisted text)
|
||||||
|
- Persist: `src/transcription/services/workflows.py:720` (`error_detail=format_error_detail(page.error)`)
|
||||||
|
- **Render (Source Detail):** `src/transcription/ui/pages/sources_page.py:481-484`
|
||||||
|
- **Render (Sources list):** `src/transcription/ui/pages/sources_page.py:121` → `src/transcription/ui/components/table/sources.py:39,90-95` ("Error Detail" column)
|
||||||
|
- **Render (Maintenance):** `src/transcription/ui/pages/settings_page.py:562`, written by `src/transcription/services/maintenance.py:206`
|
||||||
|
- Contract violated: `docs/error_handling.md:107-114`; `.github/instructions/error-handling.instructions.md:86`; `docs/invariant/error_handling.md:59`; `docs/invariant/ai_evidence_and_provenance.md:103`
|
||||||
|
|
||||||
|
- **Reachability:** **Live.** Concrete path, no configuration required: a page fails with any
|
||||||
|
non-`AppError` exception → `workflows.py:388` calls `classify_unexpected_error(exc)` →
|
||||||
|
`errors.py:118` sets `detail=f"{type(exc).__name__}: {exc}"` → `format_error_detail`
|
||||||
|
(`errors.py:135-139`) emits `... | detail=OSError: [Errno 13] Permission denied: '/app/uploads/documents/<uuid>/page-1.jpg' | ...`
|
||||||
|
→ persisted to `ExecutionAttempt.error_detail` → rendered verbatim at
|
||||||
|
`sources_page.py:484` and in the `/sources` table column. A SQLAlchemy `OperationalError`
|
||||||
|
carries the database path by the same route.
|
||||||
|
|
||||||
|
- **Problem & Consequence:** `docs/error_handling.md:110` states `detail` is *"Internal only"* and
|
||||||
|
that its only surfaces are `format_error_detail` (evidence) and logs;
|
||||||
|
`error-handling.instructions.md:86` says *"Never rendered to users or serialized into an
|
||||||
|
envelope."* The UI reads it anyway. The consequence is not hypothetical drift — it is the
|
||||||
|
precise defect the previous review's fix existed to prevent. That fix made `message` generic and
|
||||||
|
moved the root cause to `detail` on the stated grounds that `detail` never reaches users. That
|
||||||
|
premise was never true: `error_detail` had a UI consumer the whole time. The result is that the
|
||||||
|
filesystem-path leak was relocated from the notification banner to the Source Detail card and
|
||||||
|
the Sources table, while the test suite records the leak as fixed
|
||||||
|
(`tests/test_errors.py:56-78`).
|
||||||
|
|
||||||
|
The inconsistency is visible inside a single file: `sources_page.py:443` deliberately routes a
|
||||||
|
stored path through `public_media_path_label` (`ui/components/media_urls.py:58-72`), which
|
||||||
|
correctly degrades an absolute path to its bare filename — and then `sources_page.py:484`
|
||||||
|
renders unsanitized text that may contain an absolute path.
|
||||||
|
|
||||||
|
- **Blast Radius:** Enumerated by grepping every reader of `.detail` and `error_detail`:
|
||||||
|
- `errors.py:137` — `format_error_detail`, the only reader of `AppError.detail`. **Must keep the root cause.**
|
||||||
|
- `services/workflows.py:720` — the only writer of `ExecutionAttempt.error_detail`.
|
||||||
|
- `services/maintenance.py:206` — the only writer of `MaintenanceRun.error_detail`.
|
||||||
|
- `services/evidence.py:195` — `build_evidence_export` emits `error_detail`. Export is an
|
||||||
|
operator-initiated evidence artifact; per invariant 3.7.1 it **must** retain it.
|
||||||
|
- `db/models.py:508-522` — `Source.latest_error_detail` projection, consumed only by `sources_page.py:121`.
|
||||||
|
- `ui/pages/sources_page.py:481-484`, `ui/components/table/sources.py`, `ui/pages/settings_page.py:562` — the three render sites.
|
||||||
|
- Tests asserting on persisted text: `tests/test_v42_evidence.py:284`,
|
||||||
|
`tests/services/test_workflows_reliability.py` (timeout detail),
|
||||||
|
`tests/services/test_maintenance_service.py`. A fix that changes *what is stored* breaks these;
|
||||||
|
a fix that changes *what is displayed* does not.
|
||||||
|
|
||||||
|
- **Recommendation — two invariants conflict here; both must be named.**
|
||||||
|
|
||||||
|
**Invariant 1 (evidence):** `ExecutionAttempt.error_detail` must retain the root cause.
|
||||||
|
`docs/requirements.md:30` (REQ-4-021) and `docs/invariant/ai_evidence_and_provenance.md:33`
|
||||||
|
require it; guarded by `tests/test_v42_evidence.py::test_attempts_are_append_only_and_exported_with_integrity`
|
||||||
|
and `tests/services/test_workflows_reliability.py`.
|
||||||
|
|
||||||
|
**Invariant 2 (privacy):** user-facing surfaces must not expose local filesystem details.
|
||||||
|
`docs/invariant/error_handling.md:59`; guarded (partially) by
|
||||||
|
`tests/test_errors.py::test_unexpected_error_does_not_leak_filesystem_paths`.
|
||||||
|
|
||||||
|
**The over-correction to avoid is stripping root-cause text out of `detail` or
|
||||||
|
`format_error_detail` to make the UI safe.** That is exactly the mistake documented in the
|
||||||
|
reviewer skill's worked example, and it would silently destroy the provenance record this
|
||||||
|
system exists to preserve while making every guard still pass.
|
||||||
|
|
||||||
|
Fix at the **render** boundary, not the write boundary. Add a presentation-layer projection and
|
||||||
|
route all three UI sites through it, leaving the persisted evidence untouched:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# src/transcription/ui/components/error_presenter.py (new)
|
||||||
|
def display_failure_detail(error_detail: str | None) -> str | None:
|
||||||
|
"""Render persisted failure detail without machine-local paths.
|
||||||
|
|
||||||
|
`ExecutionAttempt.error_detail` is provenance and keeps the full root cause
|
||||||
|
(docs/error_handling.md). This projection is the only thing a page may show.
|
||||||
|
"""
|
||||||
|
```
|
||||||
|
|
||||||
|
It should preserve the `[category]`, `suggestion=`, and `error_id=` segments (which are what
|
||||||
|
make the display actionable) and reduce any absolute path inside `detail=` to its basename,
|
||||||
|
mirroring `public_media_path_label`. The operator keeps diagnosability — required by
|
||||||
|
`docs/ui/pages/sources.md:43` and `docs/requirements.md:59` (REQ-6-014) — without the container
|
||||||
|
filesystem layout being published to the browser.
|
||||||
|
|
||||||
|
Then decide and record which resolution was chosen: either the UI shows the sanitized
|
||||||
|
projection (recommended), or `docs/error_handling.md:107-114` and
|
||||||
|
`error-handling.instructions.md:86` are revised to state that operator-facing evidence displays
|
||||||
|
may render `error_detail` **and** that the guarantee moves to "no machine-local detail ever
|
||||||
|
enters `detail`" — which would be a much harder guarantee to keep. Do not leave the current
|
||||||
|
state, where the docs claim one thing and three pages do another.
|
||||||
|
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [HIGH-02] Absolute filesystem paths are embedded in user-facing `AppError.message` at five explicit raise sites
|
||||||
|
|
||||||
|
- **Location:**
|
||||||
|
- `src/transcription/services/sources.py:856` — `f"Prompt file not found: {prompt_path}"`
|
||||||
|
- `src/transcription/services/sources.py:864` — `f"Prompt file is empty: {prompt_path}"`
|
||||||
|
- `src/transcription/services/sources.py:914` — `f"Source file not found: {path}"`
|
||||||
|
- `src/transcription/services/prompts.py:99` — `f"Prompt directory is unavailable: {root}"`
|
||||||
|
- `src/transcription/services/prompts.py:186-191` — `_filesystem_error` builds `f"{message}: {exc}"`
|
||||||
|
- Contract violated: `.github/instructions/error-handling.instructions.md:74,85`; `docs/invariant/error_handling.md:59`
|
||||||
|
|
||||||
|
- **Reachability:** **Live**, on an ordinary user path. `sources.py:845` resolves
|
||||||
|
`prompt_root = runtime_settings.prompt_dir.resolve()`, so `prompt_path` is absolute
|
||||||
|
(`/app/prompts/transcribe_document.md` in the container). `load_prompt_text` is invoked by
|
||||||
|
`build_prompt_execution` (`sources.py:829-831`), which runs on **every document upload** via
|
||||||
|
`services/store.py:94` and `store.py:162`. The resulting `PromptLoadError` is an `AppError`
|
||||||
|
subclass, so it flows through `run_ui_action` → `show_error`
|
||||||
|
(`ui/components/error_presenter.py:51-66`), which renders `error.message` into both a
|
||||||
|
`ui.notify` banner and a card label, and through `build_error_envelope` (`errors.py:88-96`)
|
||||||
|
into API responses.
|
||||||
|
|
||||||
|
- **Problem & Consequence:** `error-handling.instructions.md:85` requires `message` to *"Stay
|
||||||
|
generic. Never embed exception text, provider payloads, or filesystem paths."* These five sites
|
||||||
|
embed exactly that. `prompts.py:186-191` violates the rule in **both** directions at once: it
|
||||||
|
puts `{exc}` — an `OSError` whose `str()` includes the offending filename — into `message`, and
|
||||||
|
it sets **no `detail=`**, so the internal field that is supposed to carry the root cause is
|
||||||
|
empty while the user-facing field carries all of it.
|
||||||
|
|
||||||
|
This is not a new regression; it is coverage that the existing guard never had.
|
||||||
|
`tests/test_errors.py:56-78` verifies only that `classify_unexpected_error` — the *catch-all*
|
||||||
|
path — does not leak. Every deliberate `raise SomeError(f"... {path}")` in the codebase is
|
||||||
|
outside its scope, so the suite reports the invariant as enforced while five live sites violate it.
|
||||||
|
|
||||||
|
- **Blast Radius:** Verified by grepping all consumers of these exception types.
|
||||||
|
`PromptLoadError`/`PromptStoreError`/`TranscriptionError` messages are consumed by:
|
||||||
|
`ui/components/error_presenter.py:55,63` (render), `errors.py:92` (API envelope),
|
||||||
|
`errors.py:135` (`format_error_detail` → evidence). Because the recommended change *adds* a
|
||||||
|
`detail` and *shortens* `message`, `format_error_detail` output still contains the path — so
|
||||||
|
evidence value is preserved, not reduced. Tests asserting on these messages:
|
||||||
|
`tests/test_prompts.py`, `tests/services/test_prompt_store.py`,
|
||||||
|
`tests/services/test_transcription_service.py`. These assert on message prefixes
|
||||||
|
(`"Prompt file not found"`), not on the interpolated path, and were checked to survive the change —
|
||||||
|
but re-run them, since `prompts.py:186` currently produces a message whose suffix some
|
||||||
|
assertion could depend on.
|
||||||
|
|
||||||
|
- **Recommendation:** Apply the pattern `errors.py:113-119` already establishes — generic
|
||||||
|
`message`, root cause on `detail`, `raise ... from exc`. Use `path.name` when a filename is
|
||||||
|
genuinely useful to the user.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# sources.py:855 — before
|
||||||
|
raise PromptLoadError(f"Prompt file not found: {prompt_path}", ...)
|
||||||
|
# after
|
||||||
|
raise PromptLoadError(
|
||||||
|
f"Prompt file not found: {prompt_path.name}",
|
||||||
|
category=ErrorCategory.INFRA_PERSISTENT,
|
||||||
|
suggestion="Verify PROMPT_DIR and prompt file configuration, then retry.",
|
||||||
|
detail=f"Prompt file missing at {prompt_path}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# prompts.py:186 — before
|
||||||
|
return PromptStoreError(f"{message}: {exc}", category=..., suggestion=...)
|
||||||
|
# after
|
||||||
|
return PromptStoreError(
|
||||||
|
message,
|
||||||
|
category=ErrorCategory.INFRA_PERSISTENT,
|
||||||
|
suggestion="Check prompt directory permissions and available disk space, then retry.",
|
||||||
|
detail=f"{type(exc).__name__}: {exc}",
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
Then widen the guard so this class cannot recur — see MED-07. Note the dependency: HIGH-02 and
|
||||||
|
HIGH-01 must be fixed **together**, because moving the path from `message` to `detail` while the
|
||||||
|
UI still renders `error_detail` relocates the leak instead of closing it. That is the same
|
||||||
|
mistake that produced HIGH-01.
|
||||||
|
|
||||||
|
- **Effort:** S (fix) / M (with the guard)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Medium Severity
|
||||||
|
|
||||||
|
#### [MED-01] The orphan sweep matches by bare name and therefore cannot detect a dead module
|
||||||
|
|
||||||
|
- **Location:** `tests/test_orphan_sweep.py:101-163` (`_public_definitions`, `_orphans`)
|
||||||
|
- **Reachability:** **Live** — the guard is running now and reporting a clean sweep that is not clean.
|
||||||
|
- **Problem & Consequence:** `_public_definitions()` keys definitions by bare name
|
||||||
|
(`definitions[node.name]`, line 111) and `_orphans()` marks a definition referenced if that
|
||||||
|
bare name appears **anywhere** in `src/`, `tests/`, or `tools/` (lines 157-162). Two different
|
||||||
|
modules that define the same public name are therefore indistinguishable, and neither can ever
|
||||||
|
be reported as an orphan.
|
||||||
|
|
||||||
|
`src/transcription/ui/pages/tags_page.py` demonstrates the consequence. Its only public
|
||||||
|
definition is `register_page` (line 18). Seven live page modules define a function of the same
|
||||||
|
name and `ui/__init__.py:37-43` calls all seven — so `register_page` is heavily referenced and
|
||||||
|
`tags_page.register_page` is scored as reachable. In fact **nothing imports `tags_page` at all**
|
||||||
|
(verified: the only repo-wide references to the module are the file itself and
|
||||||
|
`tests/ui/test_tags_page.py`, which merely asserts the route 404s). 93 lines of code, including a
|
||||||
|
lazy-load-unsafe relationship traversal at `tags_page.py:71-74`, sit outside the sweep's reach.
|
||||||
|
|
||||||
|
The sweep also never asks whether a *module* is imported, only whether its definitions' names
|
||||||
|
appear somewhere — so this is a structural gap, not a one-off miss.
|
||||||
|
|
||||||
|
- **Blast Radius:** `tests/test_orphan_sweep.py` only; `KNOWN_ORPHANS` entries are keyed by the
|
||||||
|
same bare/dotted names and would need re-keying if qualification is added. Expect the stricter
|
||||||
|
sweep to surface additional true orphans on first run — triage them into `KNOWN_ORPHANS` with
|
||||||
|
rationales rather than weakening the check.
|
||||||
|
- **Recommendation:** Qualify definitions by module (`f"{module_path}:{name}"`) and add a separate,
|
||||||
|
cheap module-reachability pass: a module under `src/transcription/` is reachable if any other
|
||||||
|
module imports it, or it is a declared entrypoint (`app.py`, `__main__.py`, `worker_service.py`).
|
||||||
|
Report unreachable modules as orphans in their own right. Also fix
|
||||||
|
`test_public_definitions_are_discovered` (line 169), whose `>= 420` snapshot threshold is a
|
||||||
|
weak assertion that drifts upward silently — the 2026-08-23 review already flagged the same
|
||||||
|
pattern at the then-current `>= 200` and it was raised rather than replaced.
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [MED-02] Canonical invariant document declares a V4 baseline while the canonical baseline is V6.1
|
||||||
|
|
||||||
|
- **Location:** `docs/invariant/ai_evidence_and_provenance.md:130`
|
||||||
|
- **Reachability:** **Live** (documentation), no runtime impact.
|
||||||
|
- **Problem & Consequence:** Section 6.1 reads *"Canonical V4 architecture, schema, requirements,
|
||||||
|
and error-policy documents define how current behavior satisfies this invariant."*
|
||||||
|
`docs/index.md:1,29-32` establishes V6.1 as the baseline and states that every canonical document
|
||||||
|
asserts the same baseline. This is the **ownership clause of the invariant that governs the
|
||||||
|
entire evidence model** — the clause that tells a reader which documents are authoritative — and
|
||||||
|
it points at a superseded generation. A reader following it lands on stale authority precisely
|
||||||
|
when resolving an evidence question, which is the highest-stakes case.
|
||||||
|
|
||||||
|
A baseline-currency guard **does** exist —
|
||||||
|
`tests/test_meta_contract_guards.py::test_canonical_docs_declare_one_consistent_baseline`
|
||||||
|
(lines 89-112) — and `docs/invariant/ai_evidence_and_provenance.md` is **not** in
|
||||||
|
`BASELINE_SCAN_EXCLUSIONS` (lines 56-64), so the file is scanned. The claim escapes for two
|
||||||
|
independent reasons, either of which alone would be sufficient:
|
||||||
|
1. `_CURRENT_VERSION_CLAIM` (line 67) matches only the words `current` or `active` before a
|
||||||
|
version. This line says "**Canonical** V4", a third phrasing the pattern does not know.
|
||||||
|
2. Both patterns require `V(\d+\.\d+)` — a mandatory minor version. The bare token `V4` cannot
|
||||||
|
match either regex under any phrasing.
|
||||||
|
|
||||||
|
The guard is therefore not absent but *phrase-shaped*: it enforces currency only for the two
|
||||||
|
sentence forms someone thought of, against version strings that carry a minor. That is a weaker
|
||||||
|
property than its docstring implies ("Every canonical doc that names the current baseline must
|
||||||
|
name the same one").
|
||||||
|
- **Blast Radius:** Documentation only; no code reads this string. Widening the guard's patterns
|
||||||
|
will re-scan all canonical docs — expect it to surface further stale mentions on first run
|
||||||
|
(`docs/architecture.md`, `docs/schema.md`, `docs/requirements.md`, and `docs/error_handling.md`
|
||||||
|
each contain 2-3 version tokens), which should be triaged rather than excluded.
|
||||||
|
- **Recommendation:** Two parts, and the second matters more than the first.
|
||||||
|
1. Change "Canonical V4" to "Canonical V6.1" at line 130.
|
||||||
|
2. Fix the guard's shape rather than adding a third phrase to the list. Accept an optional minor
|
||||||
|
(`V(\d+)(?:\.(\d+))?`) and invert the matching: flag **every** `V<n>` token in a scanned
|
||||||
|
canonical doc that is not the declared baseline, rather than only those preceded by an
|
||||||
|
approved adjective. Phrase-list matching fails open — each new phrasing silently reopens the
|
||||||
|
hole — whereas token matching fails closed and forces an explicit exclusion.
|
||||||
|
|
||||||
|
See §8.1 for the alternative the maintainer is considering: dropping version labels from
|
||||||
|
canonical docs entirely, which removes the failure mode instead of guarding it.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [MED-03] Settings resolve `.env.production` relative to the process working directory, and the isolation fix exists only in the test harness
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/config.py:66-75` (`env_file=".env.production"`);
|
||||||
|
workaround at `tests/conftest.py:27-50`; guarded by `tests/test_config_isolation.py`;
|
||||||
|
depended on by `.github/workflows/quality-gate.yml` and `docker-compose.production.yml`
|
||||||
|
- **Reachability:** **Live but currently correct.** I verified the production path rather than
|
||||||
|
assuming it: `Dockerfile` sets `WORKDIR /app` in the runtime stage, and
|
||||||
|
`docker-compose.production.yml` mounts `./.env.production` to `/app/.env.production` for both the
|
||||||
|
`app` and `worker` services, so the relative path resolves correctly today.
|
||||||
|
- **Problem & Consequence:** Correct configuration loading depends on an **implicit, undocumented
|
||||||
|
contract between `config.py` and the process working directory.** Nothing in `config.py` states
|
||||||
|
it, and nothing tests it. The failure mode is not silent — `openrouter_api_key` is required with
|
||||||
|
no default, so a wrong cwd produces a `ValidationError` at startup rather than a partially
|
||||||
|
configured process — which is why this is Medium rather than High.
|
||||||
|
|
||||||
|
The more telling symptom is what the coupling forced on the test harness. `conftest.py:45-50`
|
||||||
|
cannot escape it by passing an argument; it must **mutate the Pydantic class-level
|
||||||
|
`model_config` dict at runtime** and restore it in a `finally`. That is a global, order-sensitive
|
||||||
|
side effect adopted because the module offers no seam. It also silently repairs a second
|
||||||
|
consumer: `ui/runtime_settings_store.py:402` reads the same `Settings.model_config["env_file"]`
|
||||||
|
to decide where the Settings page writes. Two subsystems are coupled through a mutable class
|
||||||
|
attribute.
|
||||||
|
- **Blast Radius:** Every `Settings` construction. Consumers of `model_config["env_file"]`:
|
||||||
|
`ui/runtime_settings_store.py:402` (write-target resolution, contract documented at
|
||||||
|
`docs/ui/pages/settings.md:27`) and `tests/conftest.py:45-50`. A change must preserve the
|
||||||
|
documented three-step resolution order — explicit override, `RUNTIME_SETTINGS_ENV_FILE`, then the
|
||||||
|
configured default — or `docs/ui/pages/settings.md:27` becomes wrong.
|
||||||
|
- **Recommendation:** Introduce one explicit resolution function that both `Settings` construction
|
||||||
|
and `runtime_settings_store` call, honoring an `ENV_FILE` environment variable and falling back
|
||||||
|
to a path anchored to a known root rather than to `os.getcwd()`. Tests then pass a path instead of
|
||||||
|
mutating class state, and `tests/test_config_isolation.py` can assert against the seam rather
|
||||||
|
than against the monkeypatch. If instead the cwd contract is accepted as deliberate, document it
|
||||||
|
in `config.py` and in `docs/production-runbook.md` and add a guard asserting `WORKDIR`/cwd
|
||||||
|
alignment — an implicit contract with a container image is exactly the kind of rule the invariant
|
||||||
|
routing table exists to place.
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [MED-04] Stale-job reclaim threshold is not derived from maximum job duration; safety currently comes from single-process sequencing
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/config.py:116-117`
|
||||||
|
(`worker_provider_timeout_seconds=30.0`, `worker_stale_job_seconds=30.0`);
|
||||||
|
sweep at `src/transcription/worker.py:222-228`; reclaim at
|
||||||
|
`src/transcription/services/jobs.py:242-268`
|
||||||
|
- **Reachability:** **Latent.** Unblocked by *either* of: (a) running more than one worker replica
|
||||||
|
(adding `deploy.replicas > 1` to the `worker` service in `docker-compose.production.yml`), or
|
||||||
|
(b) setting `RUN_EMBEDDED_WORKER=true` on the `app` service while the standalone `worker`
|
||||||
|
container is also running. It is safe today only because
|
||||||
|
`docker-compose.production.yml` sets `RUN_EMBEDDED_WORKER: "false"` on `app` and defines exactly
|
||||||
|
one `worker`, and because within a single loop `run_worker_loop` awaits
|
||||||
|
`process_next_queued_job` to completion before returning to the stale sweep — so the sweep can
|
||||||
|
never observe a job that this same process is actively working.
|
||||||
|
- **Problem & Consequence:** The stale threshold (30s) **equals** the per-page provider timeout
|
||||||
|
(30s), leaving zero margin even for a single-page job. A multi-page document is legitimately
|
||||||
|
`PROCESSING` for up to N × 30s. `Job.date_updated` carries an `onupdate`
|
||||||
|
(`db/models.py:372-375`), but between the initial claim and the terminal write the only touch
|
||||||
|
is `sources.py:522-523` reassigning `job.provider`/`job.model` to values they usually already
|
||||||
|
hold, which SQLAlchemy resolves to no net change and therefore no `UPDATE`. I did not empirically
|
||||||
|
confirm the no-`UPDATE` behavior, so treat that specific step as unverified — but the finding does
|
||||||
|
not depend on it, because even a per-page refresh leaves only a 30s margin against a 30s timeout.
|
||||||
|
|
||||||
|
With a second concurrent worker, the sweep would requeue a job that is mid-provider-call. Both
|
||||||
|
workers then process the same job, producing duplicate `ExecutionAttempt` rows for the same
|
||||||
|
logical work and racing terminal status writes. Append-only history would be *preserved* but no
|
||||||
|
longer *faithful*: the evidence would show attempts that do not correspond to distinct
|
||||||
|
application decisions.
|
||||||
|
|
||||||
|
This is worth flagging because `jobs.py:191-197` explicitly implements and documents
|
||||||
|
`SKIP LOCKED` row locking "so concurrent workers never contend for the same job." The claim path
|
||||||
|
is built for multi-worker operation; the reclaim path is not. A reader who trusts the claim
|
||||||
|
docstring would reasonably scale the worker.
|
||||||
|
- **Blast Radius:** `requeue_stale_processing_jobs` has one production caller (`worker.py:226`) and
|
||||||
|
tests in `tests/test_worker.py` and `tests/services/test_job_service.py`. Changing the *default*
|
||||||
|
affects `tests/test_config.py` declared-defaults assertions — check those before editing the default.
|
||||||
|
- **Recommendation:** **Fix before adding a second worker replica, not after.** Two parts:
|
||||||
|
(1) Make the threshold a function of the real bound rather than a coincidental peer of the
|
||||||
|
page timeout — at minimum default `worker_stale_job_seconds` to a multiple of
|
||||||
|
`worker_provider_timeout_seconds` with headroom, and add a model validator rejecting a stale
|
||||||
|
threshold at or below the provider timeout.
|
||||||
|
(2) Preferably make reclaim heartbeat-based: have `_persist_page_outcome` bump `Job.date_updated`
|
||||||
|
explicitly so liveness reflects progress rather than elapsed time since claim.
|
||||||
|
Add a guard asserting a multi-page job in flight is not reclaimed by a concurrently-invoked sweep.
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [MED-05] Provider evidence capture is per-instance mutable state, making the adapter non-reentrant by contract
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/providers/openrouter.py:197-199, 264-267, 274-275, 297-298, 397-412`;
|
||||||
|
`_CapturingAsyncClient.last_response`/`last_body` at `openrouter.py:66-94`;
|
||||||
|
contract at `src/transcription/providers/base.py:110-118`
|
||||||
|
(`current_request_manifest`, `current_transport_evidence`)
|
||||||
|
- **Reachability:** **Latent.** Unblocked by any concurrent `transcribe()` on a single adapter
|
||||||
|
instance — most plausibly by processing a job's pages in parallel (`workflows.py:274` is
|
||||||
|
currently a sequential `for` loop) or by any second consumer sharing one
|
||||||
|
`SourceService.provider`. Verified safe today: `workflows.py:272` resolves one provider for the
|
||||||
|
loop and awaits each page; the worker's `ServiceBundle` (`worker.py:206`) is distinct from
|
||||||
|
`app.state.services` (`app.py:43`), so the UI cannot share the worker's adapter instance, and
|
||||||
|
the UI only enqueues jobs (`ui/pages/jobs_page.py:186-208`).
|
||||||
|
- **Problem & Consequence:** The `TranscriptionProvider` protocol defines evidence retrieval as
|
||||||
|
"the most recent call" state read *after* the fact. `workflows.py:369-370` relies on this on the
|
||||||
|
timeout path, reading `provider.current_request_manifest` / `current_transport_evidence` when no
|
||||||
|
result object exists. Under concurrency, page B's response overwrites
|
||||||
|
`_CapturingAsyncClient.last_response` before page A's timeout handler reads it, and page A's
|
||||||
|
`ExecutionAttempt` is written with page B's transport evidence.
|
||||||
|
|
||||||
|
The consequence is **evidence mis-attribution** — a provenance-integrity failure, which this
|
||||||
|
project's own rubric treats as its most serious class. It would also be near-undetectable after
|
||||||
|
the fact: the attempt row would be well-formed, internally consistent, and wrong. The
|
||||||
|
application-level design that makes this safe (sequential pages) is not expressed in the
|
||||||
|
provider contract, so the constraint lives only in `workflows.py`'s loop structure.
|
||||||
|
- **Blast Radius:** Changing the protocol touches `providers/base.py:102-136`,
|
||||||
|
`providers/openrouter.py:221-231`, the two read sites at `workflows.py:369-370`, and the fakes in
|
||||||
|
`tests/providers/test_openrouter.py`, `tests/services/test_workflows_reliability.py`, and
|
||||||
|
`tests/test_provider_boundaries.py`, all of which implement or assert the current property-based
|
||||||
|
contract.
|
||||||
|
- **Recommendation:** **Fix before introducing any intra-job page concurrency.** The durable fix is
|
||||||
|
to stop returning evidence through instance state: attach `request_manifest` and
|
||||||
|
`transport_evidence` to the raised exception on every failure path — which `ProviderError`
|
||||||
|
already supports (`providers/base.py:18-29`) and which the timeout path cannot currently use
|
||||||
|
because `asyncio.wait_for` raises `TimeoutError` from outside the adapter. A narrower option is
|
||||||
|
to have `transcribe()` accept a caller-owned capture sink so evidence is scoped to the call
|
||||||
|
rather than to the adapter. As an immediate, near-zero-cost step, document the non-reentrancy on
|
||||||
|
the protocol in `providers/base.py` so the constraint is visible where it is depended upon.
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [MED-06] Provider error bodies reach user-facing text while three provider failure paths persist no `detail`
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/services/sources.py:923-947` (`handle_transcription_errors`);
|
||||||
|
message construction at `src/transcription/providers/openrouter.py:414-431`
|
||||||
|
(`_transport_error_message`)
|
||||||
|
- **Reachability:** **Live** for the message half (any provider failure during a UI-initiated
|
||||||
|
transcription surfaces through `show_error`).
|
||||||
|
- **Problem & Consequence:** Two mirrored halves of the same rule are broken in one function.
|
||||||
|
- `sources.py:943` builds `f"Provider transcription failed: {exc}"`, and `exc` is a
|
||||||
|
`ProviderError` whose message may embed up to 500 characters of the provider's error body
|
||||||
|
(`openrouter.py:430`). That is a provider payload in `message`, which
|
||||||
|
`error-handling.instructions.md:85` explicitly forbids.
|
||||||
|
- None of the three handlers (lines 929, 935, 942) passes `detail=`. Per
|
||||||
|
`error-handling.instructions.md:89-92`, omitting it degrades the provenance record.
|
||||||
|
|
||||||
|
I checked whether the provenance half is actually harmful before reporting it, and it is
|
||||||
|
**substantially mitigated**: `workflows.py:391` calls `_find_provider_error`, which walks
|
||||||
|
`__cause__`/`__context__` (`workflows.py:806-813`) to recover the original `ProviderError` and
|
||||||
|
persists its `transport_evidence` — status code, safe headers, and the exact response body — onto
|
||||||
|
the attempt. So the root cause is preserved in transport evidence even though `error_detail` is
|
||||||
|
thin. This is why the finding is Medium rather than High. The residual cost is that the
|
||||||
|
human-readable failure summary is uninformative for the two paths (`ProviderAuthError`,
|
||||||
|
`ProviderResponseError`) whose messages are entirely generic.
|
||||||
|
- **Blast Radius:** `handle_transcription_errors` is used on the transcription path in
|
||||||
|
`sources.py`; `TranscriptionError.message` is consumed by `error_presenter.show_error`,
|
||||||
|
`build_error_envelope`, and `format_error_detail`. Assertions on these messages live in
|
||||||
|
`tests/services/test_transcription_service.py` and `tests/providers/test_openrouter.py`.
|
||||||
|
- **Recommendation:** Move the interpolated provider text from `message` to `detail` on all three
|
||||||
|
handlers, keeping the generic message the other two already use:
|
||||||
|
```python
|
||||||
|
except ProviderError as exc:
|
||||||
|
raise TranscriptionError(
|
||||||
|
"Provider transcription failed",
|
||||||
|
category=ErrorCategory.EXTERNAL_PROVIDER,
|
||||||
|
suggestion="Retry the transcription from jobs. If repeated, check provider availability.",
|
||||||
|
retriable=True,
|
||||||
|
detail=f"{type(exc).__name__}: {exc}",
|
||||||
|
) from exc
|
||||||
|
```
|
||||||
|
Apply the same `detail=` addition to the `ProviderAuthError` and `ProviderResponseError`
|
||||||
|
handlers. Note the interaction with HIGH-01: until the render boundary is sanitized, moving text
|
||||||
|
into `detail` still reaches users through the `error_detail` display. Sequence accordingly.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### [MED-07] No deterministic guard covers the `message`/`detail` split at explicit raise sites
|
||||||
|
|
||||||
|
- **Location:** `tests/test_errors.py:56-78`; rule at
|
||||||
|
`.github/instructions/error-handling.instructions.md:78-98`; canonical statement at
|
||||||
|
`docs/error_handling.md:102-115`
|
||||||
|
- **Reachability:** **Live** — this coverage gap is what allowed HIGH-02 and MED-06 to exist in a
|
||||||
|
fully green suite.
|
||||||
|
- **Problem & Consequence:** `docs/error_handling.md:115` names
|
||||||
|
`tests/test_errors.py::test_unexpected_error_does_not_leak_filesystem_paths` as the enforcement
|
||||||
|
for the message/detail split. That test exercises exactly one function,
|
||||||
|
`classify_unexpected_error`. Every direct `raise SomeAppError(...)` in `src/` — roughly 50 sites
|
||||||
|
by grep — is unenforced. The documentation therefore overstates the enforcement, which is worse
|
||||||
|
than having no guard: a contributor reading `error_handling.md:115` reasonably concludes the rule
|
||||||
|
is mechanically protected.
|
||||||
|
|
||||||
|
Per the reviewer skill, where a check is unenforced, recommending the deterministic test is
|
||||||
|
itself a finding.
|
||||||
|
- **Blast Radius:** Tests only.
|
||||||
|
- **Recommendation:** Add an AST guard, `tests/test_error_message_safety.py`, that scans `src/`
|
||||||
|
for `raise <AppError subclass>(...)` and fails when the first positional argument is an f-string
|
||||||
|
containing a formatted value whose name matches a path-like or exception-like identifier
|
||||||
|
(`path`, `_path`, `root`, `dir`, `exc`, `err`, `e`). Model it on the existing AST guards, which
|
||||||
|
are the established pattern here (`test_ui_boundaries.py`, `test_service_boundaries.py`,
|
||||||
|
`test_orphan_sweep.py`). Pair it with a second guard asserting that no UI module reads
|
||||||
|
`error_detail` without routing through the sanitizing projection from HIGH-01 — that one closes
|
||||||
|
the render side, which is where the real leak is.
|
||||||
|
- **Effort:** M
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Low Severity
|
||||||
|
|
||||||
|
#### [LOW-01] `ui/pages/tags_page.py` is dead code; the V6.1 roadmap is correct
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/ui/pages/tags_page.py` (93 lines);
|
||||||
|
registration list at `src/transcription/ui/__init__.py:37-43`
|
||||||
|
- **Reachability:** **Not reachable.** This resolves the flagged open item: the route is genuinely
|
||||||
|
**not** registered. `register_pages` calls seven page registrars and `tags_page` is not among
|
||||||
|
them; nothing anywhere imports the module. `docs/roadmap_plan.md:47` ("Retire the Tags page") is
|
||||||
|
accurate, and `tests/ui/test_tags_page.py` correctly asserts `/ui/tags` returns 404 — though it
|
||||||
|
passes trivially, since an unimported module cannot register anything.
|
||||||
|
- **Problem & Consequence:** No runtime risk; purely stranded code. It is worth noting that if it
|
||||||
|
*were* ever re-registered, `tags_page.py:71-74` traverses `document.document_tags` and
|
||||||
|
`link.tag_ref` inside a page render, and those relationships are configured `lazy="raise"`
|
||||||
|
(`docs/architecture.md:200-203`) — so re-enabling this module without adding eager loads to
|
||||||
|
`list_documents` would raise on first render.
|
||||||
|
- **Recommendation:** Delete `src/transcription/ui/pages/tags_page.py`. Retain
|
||||||
|
`tests/ui/test_tags_page.py` as the retirement guard. Fixing MED-01 first would make this
|
||||||
|
finding reproducible by the suite rather than by manual inspection.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
#### [LOW-02] `benchmarking.py` ships in the runtime package but is referenced only by tests
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/benchmarking.py` (69 lines); sole consumers
|
||||||
|
`tests/test_v42_evidence.py:15-16` (`EditorialAssessment`, `score_transcription`)
|
||||||
|
- **Reachability:** Live as importable API; never invoked by application code.
|
||||||
|
- **Problem & Consequence:** No defect. It supports the model-evaluation policy in
|
||||||
|
`docs/invariant/ai_evidence_and_provenance.md:113-126`, which is legitimate, but it currently has
|
||||||
|
no production caller and no tooling entrypoint, so it is indistinguishable from drift.
|
||||||
|
- **Recommendation:** Either move it under `tools/` alongside the other operator utilities, or add
|
||||||
|
a `KNOWN_ORPHANS`-style rationale recording that it is retained as the evaluation-policy
|
||||||
|
implementation. Do not silently keep it unlabeled.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
#### [LOW-03] Two overlapping prompt error types split across modules
|
||||||
|
|
||||||
|
- **Location:** `src/transcription/services/errors.py:15-16` (`PromptLoadError`) and
|
||||||
|
`src/transcription/services/prompts.py:19` (`PromptStoreError`)
|
||||||
|
- **Reachability:** Live; no misbehavior observed.
|
||||||
|
- **Problem & Consequence:** `services/errors.py:1-8` documents itself as the neutral home for
|
||||||
|
exceptions raised by more than one service, precisely so a caller's `except` clause does not
|
||||||
|
change when an operation moves. `PromptStoreError` is defined outside that module and covers an
|
||||||
|
overlapping domain (prompt file access), so a caller wanting to handle "any prompt failure" must
|
||||||
|
import from two modules and know which is which. `sources.py:855` raises `PromptLoadError` for a
|
||||||
|
missing prompt file while `prompts.py:132` raises `PromptStoreError` for the same condition
|
||||||
|
reached through the Settings page.
|
||||||
|
- **Recommendation:** Move `PromptStoreError` into `services/errors.py` next to `PromptLoadError`,
|
||||||
|
or make one a subclass of the other so a single `except` covers prompt failures. Low urgency; do
|
||||||
|
it opportunistically when HIGH-02 touches both files anyway.
|
||||||
|
- **Effort:** S
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Architectural Drift & Gap Analysis
|
||||||
|
|
||||||
|
| Area / Component | Direction | Documented / Intended Rule | Actual Implementation State | Severity | Recommended Resolution |
|
||||||
|
| :--- | :--- | :--- | :--- | :--- | :--- |
|
||||||
|
| Error presentation | `doc->code` | `docs/error_handling.md:110` — `detail` is internal only, surfaced by `format_error_detail` and logs | `sources_page.py:484`, `table/sources.py:90`, `settings_page.py:562` render `error_detail` verbatim to users | High | Sanitizing render projection (HIGH-01); do **not** strip `detail` |
|
||||||
|
| User-facing messages | `doc->code` | `invariant/error_handling.md:59` — no local filesystem detail in user-facing messages | 5 live sites interpolate absolute paths into `AppError.message` | High | Generic `message`, path on `detail` (HIGH-02) |
|
||||||
|
| Evidence invariant ownership | `doc->doc` | `docs/index.md:1` — baseline is V6.1 | `invariant/ai_evidence_and_provenance.md:130` names "Canonical V4"; the currency guard scans the file but its regexes match neither the phrasing nor a minor-less `V4` | Medium | Update text; make the guard token-based, or drop version labels entirely (MED-02, §8.1) |
|
||||||
|
| Enforcement claim | `doc->code` | `docs/error_handling.md:115` — split "Enforced by `tests/test_errors.py::…`" | That test covers only `classify_unexpected_error`; explicit raises unguarded | Medium | Add AST guard (MED-07) |
|
||||||
|
| Orphan sweep | `doc->code` | `test_orphan_sweep.py:1-13` — sweep is "deterministic" and "conservative" | Bare-name matching; cannot see a dead module (`tags_page.py`) | Medium | Qualify by module + module-reachability pass (MED-01) |
|
||||||
|
| Worker scaling | `code->doc` | `jobs.py:191-197` — `SKIP LOCKED` so "concurrent workers never contend" | Claim path is multi-worker-safe; stale-reclaim path is not | Medium | Derive stale threshold from job duration; document single-worker constraint until fixed (MED-04) |
|
||||||
|
| Provider adapter contract | `code->doc` | `providers/base.py:110-118` — evidence read as "most recent call" state | Contract is silently non-reentrant; safety lives in `workflows.py`'s sequential loop | Medium | Scope evidence to the call; document non-reentrancy (MED-05) |
|
||||||
|
| Settings env file | `code->doc` | `config.py:66-75` — `env_file=".env.production"` | Correctness depends on an undocumented cwd contract with `Dockerfile` `WORKDIR /app` | Medium | Explicit resolver seam, or document + guard the contract (MED-03) |
|
||||||
|
| Tags page | *(no drift)* | `roadmap_plan.md:47` — Tags page retired | Route genuinely unregistered; module is stranded code | Low | Delete the module (LOW-01) |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Invariant Inventory & Routing Recommendations
|
||||||
|
|
||||||
|
| Invariant / Constraint | Current Location | Recommended Target Layer | Rationale |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `detail`/`error_detail` never rendered to users | docs + instructions | **Deterministic test** + sanitizing projection | Stated in three documents and violated in three files; prose has demonstrably failed to hold it |
|
||||||
|
| `message` carries no paths or exception text | instructions; partial test | **Deterministic test** (AST, all raise sites) | Existing guard covers one function; the gap produced HIGH-02 |
|
||||||
|
| `ExecutionAttempt.error_detail` retains root cause | docs + `test_v42_evidence.py` | **Keep in tests** — already correct | Counterweight to the above; must be named in any fix so it is not over-corrected |
|
||||||
|
| Intermediate pages commit individually | `workflows.py` docstring + `test_workflows_reliability.py:318` | **Keep in tests** — verified genuine | Cross-session read makes it a real durability assertion |
|
||||||
|
| Final page atomic with terminal status | `services.instructions.md` + `test_pipeline_atomicity.py` | **Keep in tests** — verified genuine | Fault injection makes a split commit fail |
|
||||||
|
| Canonical baseline version consistency | `docs/index.md` + `test_meta_contract_guards.py:89` | **Repair existing test, or remove the labels** | Guard exists but matches by approved phrase and requires a minor version, so it fails open on new phrasings (MED-02) |
|
||||||
|
| Module-level reachability / dead modules | `test_orphan_sweep.py` (ineffective) | **Deterministic test** (repair existing) | Guard exists but cannot detect the case (MED-01) |
|
||||||
|
| Stale threshold > max job duration | *(unenforced)* | **Config validator + test** | Currently a coincidence of two equal defaults (MED-04) |
|
||||||
|
| Provider adapter non-reentrancy | *(unenforced, implicit)* | **Instructions** + protocol docstring | A design constraint callers must know before adding concurrency (MED-05) |
|
||||||
|
| Env-file resolution independent of cwd | `tests/conftest.py` monkeypatch | **Code seam** + `docs/production-runbook.md` | A test-only fix for a production coupling is misrouted enforcement (MED-03) |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. Stack-Specific Analysis
|
||||||
|
|
||||||
|
**Python 3.12+.** Modern and consistent. PEP 695 generics are used correctly and non-trivially
|
||||||
|
(`RegistryService[ModelT: RegistryEntry]` in `services/registry.py:58`, `UiActionOutcome[T]`,
|
||||||
|
`_get_or_raise[ModelT]`), `type` statements appear in `db/session.py:15,50`, and `X | None` is
|
||||||
|
used throughout. `structural Protocol` bounds (`RegistryEntry`, `WorkerNotifier`,
|
||||||
|
`TranscriptionProvider`) are used to avoid type suppressions rather than to decorate. `ty` passes
|
||||||
|
clean with no suppressions found. The two `# noqa` uses (`workflows.py:228` `PLR0915`,
|
||||||
|
`workflows.py:383` `BLE001`) are both justified in context — the broad catch is a deliberate
|
||||||
|
per-page containment boundary that immediately classifies and re-records.
|
||||||
|
|
||||||
|
**FastAPI.** Lifespan is handled via `@asynccontextmanager` (`app.py:36`), not the deprecated
|
||||||
|
`@app.on_event`. Session factories are injected through `Depends` (`SessionFactoryDep`,
|
||||||
|
`db/session.py:50`) rather than reached as globals from routes. `api/errors.py` centralizes
|
||||||
|
envelope translation. One residual: `get_settings` is `@cache`d and read as a module-level
|
||||||
|
fallback in ~10 modules; this is acceptable given the documented restart-to-apply contract
|
||||||
|
(`docs/ui/pages/settings.md:28`) but means the cache is process-lifetime and unclearable.
|
||||||
|
|
||||||
|
**NiceGUI (pinned `3.13.0`).** The pin is a recorded release-stability decision and is not
|
||||||
|
reported as a defect. Boundaries are enforced structurally: `test_ui_boundaries.py` uses an
|
||||||
|
import **allowlist**, which is the right polarity. Blocking work is dispatched off the event loop
|
||||||
|
via `run_blocking` (`settings_page.py:818,822`). The one boundary that is *not* enforced is
|
||||||
|
presentation of internal fields (HIGH-01) — pages are prevented from touching persistence but not
|
||||||
|
from rendering internal-only text.
|
||||||
|
|
||||||
|
**SQLModel / SQLAlchemy.** Strong. `lazy="raise"` on relationships forces explicit eager loading;
|
||||||
|
read paths declare `selectinload` chains with comments explaining *why* each is needed
|
||||||
|
(`sources.py:309-316` is a good example). `expire_on_commit=False` (`db/session.py:28`) is set
|
||||||
|
deliberately, which is what makes post-commit attribute access in `evidence.py:159-202` safe.
|
||||||
|
`claim_next_queued_job` (`jobs.py:186-241`) branches correctly on dialect — `SKIP LOCKED` on
|
||||||
|
PostgreSQL, conditional `UPDATE ... RETURNING` on SQLite — rather than assuming one engine.
|
||||||
|
Attempt-number allocation uses `begin_nested()` with bounded retry (`sources.py:598-616`), the
|
||||||
|
right pattern for a monotonic per-parent sequence. No N+1 patterns were found in the read paths
|
||||||
|
sampled.
|
||||||
|
|
||||||
|
**Pydantic V2 & Settings.** Fully V2; no `@validator`, `class Config`, `.dict()`, or `parse_obj`
|
||||||
|
anywhere. Evidence contracts use `ConfigDict(extra="forbid", frozen=True)` (`providers/evidence.py:47`),
|
||||||
|
which is exactly right for persisted provenance — an unexpected field fails loudly rather than
|
||||||
|
being silently dropped. `SecretStr` guards the API key. The discriminated
|
||||||
|
`SqliteSettings | PostgresSettings` union is clean. `normalize_provider_models` correctly runs
|
||||||
|
`mode="before"` so the derived tuple is produced by construction rather than by mutating a frozen
|
||||||
|
model — a subtlety that is easy to get wrong. Sole issue: the cwd-coupled `env_file` (MED-03).
|
||||||
|
|
||||||
|
**Asyncio Workers.** Notably careful. `asyncio.shield` wraps both the per-page commit and the
|
||||||
|
terminal commit (`workflows.py:601-614`, `650-663`), with the `except CancelledError: await task;
|
||||||
|
raise` pattern that actually completes the shielded work rather than merely deferring cancellation —
|
||||||
|
a detail most implementations get wrong. `handle_worker_exceptions` (`worker.py:157-182`)
|
||||||
|
distinguishes retriable from non-retriable faults and stops the loop rather than spinning.
|
||||||
|
`_advance_job_with_containment` (`worker.py`/`workflows.py:507-540`) guarantees a claimed job
|
||||||
|
cannot strand in `PROCESSING`. `worker_consumer_lifespan` has a bounded shutdown with escalation to
|
||||||
|
`cancel()`. Gaps are MED-04 and MED-05, both latent and both with stated unblocking conditions.
|
||||||
|
|
||||||
|
**OpenRouter / Adapter Boundary.** Encapsulation holds: `test_provider_boundaries.py` enforces it,
|
||||||
|
and `workflows.py` imports only `providers` abstractions. `_CapturingAsyncClient` is a
|
||||||
|
well-judged design — it captures the exact transport body before SDK parsing without altering what
|
||||||
|
the SDK consumes, including the streamed case. Timeout construction (`openrouter.py:200-206`)
|
||||||
|
correctly overrides httpx's 5s per-phase default that would otherwise silently cap the configured
|
||||||
|
budget. `SAFE_RESPONSE_HEADERS` (`providers/evidence.py:29-41`) was reviewed field-by-field:
|
||||||
|
all nine entries are non-secret correlation, content, or rate-limit headers, and
|
||||||
|
`filter_safe_response_headers` is a true allowlist filter with no redaction-after-capture — this
|
||||||
|
satisfies invariant 3.8.2 exactly. `_replace_embedded_media` correctly substitutes a source
|
||||||
|
reference for base64 payloads, satisfying 3.8.3. The one structural weakness is MED-05.
|
||||||
|
|
||||||
|
**Testing & Quality Tooling.** 410 tests, all green, with genuinely strong contract guards
|
||||||
|
(boundaries, model contract, media path safety, evidence append-only, atomicity). Marker strictness
|
||||||
|
and `asyncio_mode = "strict"` are configured, and no unawaited-coroutine warnings appeared. Two
|
||||||
|
guards, however, assert meaningfully less than their names and docstrings claim
|
||||||
|
(`test_orphan_sweep.py` — MED-01; `test_errors.py` path-leak coverage — MED-07), and the
|
||||||
|
`>= 420` snapshot threshold at `test_orphan_sweep.py:169` repeats a weak-assertion pattern the
|
||||||
|
2026-08-23 review already flagged at `>= 200`; it was raised rather than replaced with
|
||||||
|
set-membership.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. Duplication & Consolidation Report
|
||||||
|
|
||||||
|
| Pattern / Duplication | Locations | Proposed Canonical Home | Estimated Lines Removed |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `f"{type(exc).__name__}: {exc}"` detail construction | `errors.py:118`, `maintenance.py:71,95,135,206`, `runtime_settings_store.py:388,476,554` | `errors.py::exception_detail(exc)` | ~8 (consistency > line count) |
|
||||||
|
| Filesystem `AppError` construction from `OSError` | `prompts.py:186-191`, `runtime_settings_store.py:384-389,472-477,550-555` | `errors.py::filesystem_error(message, exc, *, suggestion)` | ~20 |
|
||||||
|
| Overlapping prompt error types | `services/errors.py:15`, `services/prompts.py:19` | `services/errors.py` (LOW-03) | ~5 |
|
||||||
|
| Duplicated `provider_duration_ms` / `processing_duration_ms` max-clamp arithmetic | `workflows.py:341-345, 361-368, 397-404` | `workflows.py::_page_durations(started_at, finished_at, monotonic_started_at)` | ~20 |
|
||||||
|
| `_utc_now_naive` defined per module | `workflows.py:51`, `jobs.py:29`, `db/models.py`, `sources.py` | Single helper in `db/models.py`, imported | ~12 |
|
||||||
|
|
||||||
|
### Proposed Canonical Abstractions
|
||||||
|
|
||||||
|
```python
|
||||||
|
# src/transcription/errors.py
|
||||||
|
def exception_detail(exc: BaseException) -> str:
|
||||||
|
"""Internal-only root-cause text for AppError.detail. Never user-facing."""
|
||||||
|
|
||||||
|
|
||||||
|
def filesystem_error[E: AppError](error_type: type[E], message: str, exc: OSError, *, suggestion: str) -> E:
|
||||||
|
"""Build a filesystem AppError with a generic message and the path on detail."""
|
||||||
|
|
||||||
|
|
||||||
|
# src/transcription/ui/components/error_presenter.py
|
||||||
|
def display_failure_detail(error_detail: str | None) -> str | None:
|
||||||
|
"""Sanitize persisted failure detail for UI rendering (HIGH-01)."""
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. Meta-Tooling & Instruction Update Recommendations
|
||||||
|
|
||||||
|
1. **`docs/invariant/ai_evidence_and_provenance.md:130`** — resolve the V4 label. Two viable
|
||||||
|
routes, and the maintainer has proposed the second:
|
||||||
|
- **(a) Repair the guard.** Fix the text to V6.1 and make
|
||||||
|
`test_canonical_docs_declare_one_consistent_baseline` token-based rather than phrase-based
|
||||||
|
(MED-02). Keeps version labels as navigational anchors.
|
||||||
|
- **(b) Remove version labels from canonical docs.** While the project has a single principal
|
||||||
|
user and no released versions to support, "canonical" and "current" are the same thing, so the
|
||||||
|
label carries no information a reader can act on — it only creates a second thing to keep in
|
||||||
|
sync. Retain the baseline declaration in `docs/index.md` alone as the release marker, keep
|
||||||
|
version language in `docs/roadmap_plan.md` and the migration/deployment docs (already
|
||||||
|
excluded from the scan for exactly this reason), and replace in-body references with
|
||||||
|
unversioned phrasing ("the canonical architecture, schema, requirements, and error-policy
|
||||||
|
documents"). The guard then inverts: assert that no canonical doc outside the exclusion set
|
||||||
|
contains a version token at all, which is a stricter and much cheaper property to hold than
|
||||||
|
agreement between many labels. Requirement IDs (`REQ-4-021`, `REQ-6-014`) are stable
|
||||||
|
identifiers, not currency claims, and should be left alone.
|
||||||
|
2. **`docs/error_handling.md:107-115`** — either add the sanitizing-projection rule for UI display
|
||||||
|
of `error_detail`, or revise the `detail` "Surfaces" row to admit operator-facing evidence
|
||||||
|
displays. Update the "Enforced by" line once MED-07's guard lands, since it currently overstates
|
||||||
|
coverage.
|
||||||
|
3. **`.github/instructions/error-handling.instructions.md`** — add an explicit clause under
|
||||||
|
"User-Safe Messaging" stating that *persisted* `error_detail` is subject to the same no-paths
|
||||||
|
rule at any render boundary. The current table (line 86) states the rule for `AppError.detail`
|
||||||
|
and stops there, so the persisted-then-rendered path falls between the lines.
|
||||||
|
4. **`.github/instructions/providers.instructions.md`** — record the adapter non-reentrancy
|
||||||
|
constraint (MED-05); it is currently an undocumented precondition of `workflows.py`.
|
||||||
|
5. **`.github/instructions/services.instructions.md`** — the two competing atomicity invariants are
|
||||||
|
well described and both guards verified; no change needed. Worth adding the stale-reclaim
|
||||||
|
threshold constraint (MED-04) alongside them, since it is a third worker-lifecycle rule with no
|
||||||
|
documented home.
|
||||||
|
6. **`tests/test_orphan_sweep.py`** — repair per MED-01 and replace the `>= 420` threshold with
|
||||||
|
set-membership assertions.
|
||||||
|
7. **New `tests/test_error_message_safety.py`** — AST guard per MED-07, covering both the raise
|
||||||
|
sites and the UI render sites.
|
||||||
|
8. **`docs/production-runbook.md`** — document the cwd/`WORKDIR` contract for `.env.production`
|
||||||
|
resolution if MED-03 is resolved by documentation rather than by a code seam.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. Prioritized Dependency-Ordered Action Plan
|
||||||
|
|
||||||
|
**Phase 1 — Blocking fixes (privacy; ordered, HIGH-01 first)**
|
||||||
|
1. **HIGH-01** — add `display_failure_detail` and route `sources_page.py:484`,
|
||||||
|
`table/sources.py:90-95`, and `settings_page.py:562` through it. Do this **first**: it closes
|
||||||
|
the render boundary, so the Phase-1.2 fix cannot relocate a leak again.
|
||||||
|
2. **HIGH-02** — move paths from `message` to `detail` at the five sites, including the
|
||||||
|
`prompts.py:186` double violation.
|
||||||
|
3. **MED-06** — move provider payload text to `detail`; add `detail=` to all three
|
||||||
|
`handle_transcription_errors` handlers.
|
||||||
|
|
||||||
|
**Phase 2 — Enforcement hardening (make Phase 1 permanent)**
|
||||||
|
4. **MED-07** — AST guard for raise-site `message` safety **and** for UI reads of `error_detail`.
|
||||||
|
5. **MED-01** — qualify orphan definitions by module; add module-reachability; replace the
|
||||||
|
snapshot threshold.
|
||||||
|
6. **MED-02** — fix the V4/V6.1 text and extend the meta-contract guard to baseline-version currency.
|
||||||
|
|
||||||
|
**Phase 3 — Reliability & concurrency (latent; each must precede its unblocking change)**
|
||||||
|
7. **MED-04** — derive `worker_stale_job_seconds` from `worker_provider_timeout_seconds` with a
|
||||||
|
rejecting validator, ideally plus a progress heartbeat. **Must land before any second worker
|
||||||
|
replica.**
|
||||||
|
8. **MED-05** — scope provider evidence to the call rather than the instance. **Must land before
|
||||||
|
any intra-job page concurrency.** Document non-reentrancy immediately as an interim step.
|
||||||
|
|
||||||
|
**Phase 4 — Consolidation & refactoring**
|
||||||
|
9. **MED-03** — explicit env-file resolution seam shared by `Settings` and `runtime_settings_store`;
|
||||||
|
remove the `model_config` monkeypatch from `conftest.py`.
|
||||||
|
10. **LOW-01** — delete `tags_page.py` (after MED-01, so the suite reproduces the finding).
|
||||||
|
11. **LOW-03** and the §7 consolidations — fold in opportunistically while Phase 1 touches these files.
|
||||||
|
|
||||||
|
**Phase 5 — Non-blocking governance/documentation depth**
|
||||||
|
12. **LOW-02** — relocate or annotate `benchmarking.py`.
|
||||||
|
13. Instruction/doc updates §8.3–§8.5, §8.8.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. Preserved Strengths
|
||||||
|
|
||||||
|
- **Append-only evidence is real, not aspirational.** Every provider call produces a distinct
|
||||||
|
`ExecutionAttempt`; no runtime path mutates a historical row. Projection writes onto
|
||||||
|
`Source.raw_transcription` are clearly separated from history, and `promote_machine_attempt`
|
||||||
|
(`evidence.py:117-146`) repoints the projection without rewriting evidence — with a docstring
|
||||||
|
that explains exactly why that one write lives in a read-oriented service.
|
||||||
|
- **Transport-layer terminology is honored in code.** `_CapturingAsyncClient` exists specifically so
|
||||||
|
the stored body is the application-boundary capture rather than an SDK-parsed object, and
|
||||||
|
`TransportEvidence(response_received=False)` explicitly represents "no response" instead of
|
||||||
|
conflating it with an empty one. This is invariant 3.4/3.5 implemented rather than asserted.
|
||||||
|
- **Header allowlisting is done the hard, correct way** — filter-before-store with an explicit
|
||||||
|
frozenset, never capture-then-redact (`providers/evidence.py:29-41,130-134`).
|
||||||
|
- **Boundaries are enforced by allowlist, not blocklist.** `test_ui_boundaries.py:20-25` states the
|
||||||
|
reasoning explicitly; it means a newly added persistence helper cannot slip through under an
|
||||||
|
unlisted name.
|
||||||
|
- **The two competing atomicity invariants are both correctly implemented and both genuinely
|
||||||
|
guarded**, with the tests structured so that the naive over-correction fails.
|
||||||
|
- **Cancellation safety in the worker is unusually well handled** — `asyncio.shield` plus
|
||||||
|
`await task` on `CancelledError` actually completes the commit rather than merely deferring
|
||||||
|
cancellation.
|
||||||
|
- **Comments explain rationale, not mechanics.** `workflows.py:269-271`, `openrouter.py:200-202`,
|
||||||
|
`config.py:114-115`, and `jobs.py:191-197` each record *why* a non-obvious choice was made,
|
||||||
|
several citing the review log entry that motivated it. This is what made verifying the atomicity
|
||||||
|
and timeout invariants tractable in this review.
|
||||||
|
- **Documentation-to-code traceability is strong overall.** Page contracts, schema field tables,
|
||||||
|
and requirement IDs are maintained and guarded; the drift found in this review is narrow and
|
||||||
|
specific rather than systemic.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Appendix A — Repo-Specific Deterministic Checks
|
||||||
|
|
||||||
|
| # | Check | Result | Evidence |
|
||||||
|
| :-- | :--- | :--- | :--- |
|
||||||
|
| 1 | Service boundary rule: no service-to-service imports | **Pass** | `tests/test_service_boundaries.py` green; AST scan, allowlist-based; `workflows.py` composes via `ServiceBundle` |
|
||||||
|
| 2 | UI boundary rule: no persistence access from pages/components | **Pass (structurally)** | `tests/test_ui_boundaries.py` green. Caveat: it guards *data access*, not presentation of internal-only fields — see HIGH-01 |
|
||||||
|
| 3 | Status vocabulary conformance; no stringly-typed literals | **Pass** | `tests/test_model_contract_guards.py` green; enum members verified against `db/models.py` |
|
||||||
|
| 4 | Evidence ownership: append-only history, projections not history mutation | **Pass** | `test_v42_evidence.py::test_attempts_are_append_only_and_exported_with_integrity` verified non-vacuous (asserts both retained attempts and export integrity at lines 281-290) |
|
||||||
|
| 5 | Canonical authority: findings resolve against `docs/*` first | **Pass with defect** | `test_canonical_authority_references_are_present` green. The companion baseline-currency guard (`test_canonical_docs_declare_one_consistent_baseline`) scans the offending file but fails open on its phrasing and on minor-less version tokens — MED-02 |
|
||||||
|
| 6 | Schema contract fidelity: `docs/schema.md` field-accurate | **Pass** | `test_model_contract_guards.py` + `test_meta_contract_guards.py` green |
|
||||||
|
| 7 | Media boundary: record-validated media, controlled URL resolver | **Pass** | `test_media_path_safety.py`, `tests/ui/test_media_urls.py` green; `public_media_path_label` verified path-safe |
|
||||||
|
| 8 | Eager-loading conformance vs `lazy="raise"` | **Pass** | Declaration-side guard green; sampled read paths declare explicit `selectinload` chains. Note: dead `tags_page.py:71-74` would violate it if re-registered (LOW-01) |
|
||||||
|
| 9 | Cross-cutting error conformance | **FAIL** | Guards green but coverage is narrower than documented: HIGH-01, HIGH-02, MED-06, MED-07 |
|
||||||
|
| 10 | Orphan/dead-code conformance | **FAIL** | Guard green but structurally unable to detect a dead module: MED-01, proven by LOW-01 |
|
||||||
|
|
||||||
|
## Appendix B — Evidence & Provenance Auditor Families
|
||||||
|
|
||||||
|
| Family | Subject | Result | Evidence |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| A | Attempt history append-only | **Pass** | No update/delete path to `ExecutionAttempt`; insert-only with `begin_nested` + bounded sequence retry (`sources.py:556-616`) |
|
||||||
|
| B | Attempt numbering monotonic per source | **Pass** | `insert_with_sequence_retry`; uniqueness constraint plus retry on conflict |
|
||||||
|
| C | Transport evidence captured at the transport boundary | **Pass** | `_CapturingAsyncClient` retains the exact wire body pre-SDK-parse (`openrouter.py:66-94`) |
|
||||||
|
| D | Absent response distinguished from empty response | **Pass** | `TransportEvidence.response_received` is explicit, not inferred |
|
||||||
|
| E | Response header persistence is allowlist-based | **Pass** | `SAFE_RESPONSE_HEADERS` (`providers/evidence.py:29-41`) — all nine entries verified non-secret; filter-before-store |
|
||||||
|
| F | No machine-local detail on user-facing surfaces | **FAIL** | `error_detail` rendered verbatim at three UI sites (HIGH-01); paths in `message` at five sites (HIGH-02) |
|
||||||
|
| G | Request manifest excludes embedded media payloads | **Pass** | `_replace_embedded_media` (`openrouter.py:377-395`) substitutes a source reference for base64 data |
|
||||||
|
| H | Evidence attribution is correct under concurrency | **Pass today / at risk** | Correct in the current sequential single-worker deployment; the contract itself is non-reentrant (MED-05) and reclaim has no margin (MED-04) |
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
# Review Reports
|
||||||
|
|
||||||
|
Dated architecture and code review reports generated by
|
||||||
|
`.github/skills/python-code-reviewer/skill.md`.
|
||||||
|
|
||||||
|
**These files are not canonical authority.** Everything in `docs/reviews/**` is a
|
||||||
|
point-in-time observation, not a contract. Canonical intent lives in `docs/index.md`,
|
||||||
|
`docs/architecture.md`, `docs/requirements.md`, `docs/schema.md`,
|
||||||
|
`docs/error_handling.md`, and `docs/invariant/**`. When a report and a canonical
|
||||||
|
document disagree, the canonical document wins until it is deliberately updated.
|
||||||
|
|
||||||
|
Naming: `<YYYY-MM-DD>-code-review.md` for review reports, and
|
||||||
|
`<YYYY-MM-DD>-remediation-handoff.md` for the implementation plan derived from one.
|
||||||
|
|
||||||
|
## Current
|
||||||
|
|
||||||
|
- [`2026-08-23-code-review.md`](./2026-08-23-code-review.md) — full review. 0 critical,
|
||||||
|
4 high, 5 medium, 11 low. **All findings remediated.** Retained as a record of the
|
||||||
|
reasoning, not as a list of open work. Note that a few of its recommendations were
|
||||||
|
wrong on contact and were corrected during implementation; the code and the guard
|
||||||
|
tests are authoritative over the report text.
|
||||||
@@ -0,0 +1,210 @@
|
|||||||
|
# Roadmap Plan (Starting at V6.0)
|
||||||
|
|
||||||
|
This roadmap starts at **V6.0** and tracks forward-looking work only.
|
||||||
|
|
||||||
|
## V6.0 - Hosting Migration
|
||||||
|
|
||||||
|
Objective: move from local-only operation to secure, stable remote hosting.
|
||||||
|
Status: **Completed**
|
||||||
|
|
||||||
|
Detailed plan: [`v6_0_hosting_migration_plan.md`](v6_0_hosting_migration_plan.md)
|
||||||
|
|
||||||
|
### Scope
|
||||||
|
1. Containerize app runtime for production deployment.
|
||||||
|
2. Run PostgreSQL in Docker and migrate from SQLite.
|
||||||
|
3. Add Cloudflare Tunnel exposure with Access protection.
|
||||||
|
4. Add operational safeguards (health checks, restart policies, backups).
|
||||||
|
|
||||||
|
### Deliverables
|
||||||
|
- Production-ready `docker-compose` deployment for app + database + tunnel.
|
||||||
|
- Environment-based configuration for DB, uploads, prompts, and logging.
|
||||||
|
- Verified data migration path into PostgreSQL.
|
||||||
|
- Runbook updates for deploy, rollback, and backup/restore.
|
||||||
|
|
||||||
|
### Exit Criteria
|
||||||
|
- `/healthz` reports healthy app and worker in deployed environment.
|
||||||
|
- One end-to-end document -> source -> job workflow succeeds remotely.
|
||||||
|
- Backup and restore procedure is tested.
|
||||||
|
|
||||||
|
### Accomplished
|
||||||
|
1. Delivered production Docker deployment with split `app`/`worker`, `postgres`, and `cloudflared`.
|
||||||
|
2. Landed SQLite -> PostgreSQL migration tooling and runbook coverage.
|
||||||
|
3. Added production health/reliability wiring and operational runbooks for deploy/rollback/recovery.
|
||||||
|
4. Established host-visible backup workflow and restore path for PostgreSQL plus media/config assets.
|
||||||
|
|
||||||
|
## V6.1 - Testing and Refinement
|
||||||
|
|
||||||
|
Objective: improve navigation and operational workflows after user feedback.
|
||||||
|
Status: **Completed**
|
||||||
|
|
||||||
|
### Scope
|
||||||
|
1. Make Document Detail the primary source-page workspace:
|
||||||
|
- Use Source-style pan/zoom + previous/next page controls.
|
||||||
|
- Move editable revision controls into Document Detail.
|
||||||
|
- Move archival/system metadata to dedicated Document Info route.
|
||||||
|
2. Simplify top navigation:
|
||||||
|
- Remove top-level Tags and Sources entries.
|
||||||
|
- Retire the Tags page and the global Source Asset Records entry flow.
|
||||||
|
3. Improve list/detail clarity:
|
||||||
|
- Add Document transcription status to Archival Documents list.
|
||||||
|
- Add Document Date in People Detail -> Linked Documents table.
|
||||||
|
4. Add worker-backed Settings maintenance runs:
|
||||||
|
- Add `maintenance_run` persistence (`id`, `job_type`, `status`, `started_at`, `finished_at`, `triggered_by`, `summary`, `log_path`, `error_detail`).
|
||||||
|
- Add Run Backup and Run Storage Reconciliation actions that enqueue runs and execute in the worker.
|
||||||
|
- Add run history with status, duration, summary, and log view/download.
|
||||||
|
- Defer daily/weekly scheduling controls to V6.2.
|
||||||
|
|
||||||
|
### Accomplished
|
||||||
|
1. Refactored Document Detail into the primary source-page workspace (pan/zoom viewer, previous/next page navigation, editable revision flow) and moved archival/system metadata to Document Info.
|
||||||
|
2. Simplified top navigation by removing Tags/Sources entries and retiring the Tags page/global Source Asset Records flow.
|
||||||
|
3. Improved data clarity with document transcription status in Archival Documents and Document Date in People Detail linked documents.
|
||||||
|
4. Implemented queue-backed maintenance operations (`maintenance_run` model/service/worker/UI) with run history and log view/download.
|
||||||
|
5. Hardened runtime settings operations in production:
|
||||||
|
- runtime settings writes target mounted `.env.production`,
|
||||||
|
- fallback write path for single-file bind mounts,
|
||||||
|
- explicit hidden/deployment-key disclosure in Settings UI.
|
||||||
|
6. Simplified backup configuration and behavior:
|
||||||
|
- standardized on `BACKUP_DIR` + `BACKUP_RETENTION_DAYS`,
|
||||||
|
- backup script uses `DATABASE__*` persistence keys,
|
||||||
|
- compose maps Postgres container init values from `DATABASE__*`,
|
||||||
|
- env contract drift tests now guard `.env.production.example`.
|
||||||
|
|
||||||
|
## V6.2 - GEDCOM Data Layer
|
||||||
|
|
||||||
|
Objective: introduce a genealogical data layer sourced from GEDCOM exports, bridged to
|
||||||
|
existing `Person` records via FamilySearch ID, without disrupting document-focused Person
|
||||||
|
workflows.
|
||||||
|
|
||||||
|
### Scope
|
||||||
|
1. Manual `.ged` file upload only. No FamilySearch credentials are stored or used by the
|
||||||
|
app; the user runs the third-party `getmyancestors` tool themselves and uploads the
|
||||||
|
resulting export.
|
||||||
|
2. Four new tables: `genealogy_person`, `genealogy_family`, `genealogy_family_child`, and
|
||||||
|
`genealogy_citation` (raw GEDCOM `SOUR` citations, reusable in a later version to record
|
||||||
|
when a transcribed document itself becomes citation evidence for FamilySearch).
|
||||||
|
3. Upsert-based import keyed on FamilySearch ID (`fs_id`) so repeat imports update existing
|
||||||
|
records in place without breaking existing `Person.family_search_id` links or duplicating
|
||||||
|
surrogate keys.
|
||||||
|
4. Reuse the existing V6.1 worker-backed `maintenance_run` pattern for import runs (run
|
||||||
|
history, status, summary, log view/download) rather than new infrastructure.
|
||||||
|
|
||||||
|
### Deliverables
|
||||||
|
- GEDCOM parser/importer producing the four genealogy tables.
|
||||||
|
- `MaintenanceJobType` entry for GEDCOM import with upsert semantics and a run summary
|
||||||
|
(records added/updated).
|
||||||
|
- Settings UI entry to upload a `.ged` file, trigger an import run, and view history.
|
||||||
|
|
||||||
|
### Exit Criteria
|
||||||
|
- Importing the same `.ged` file twice does not duplicate or orphan data.
|
||||||
|
- Existing `Person.family_search_id` values continue to resolve to the correct
|
||||||
|
`genealogy_person` row after import.
|
||||||
|
- Import run history is visible with status, duration, and summary, consistent with other
|
||||||
|
maintenance runs.
|
||||||
|
|
||||||
|
## V6.3 - Reporting and Genealogy-Enriched Features
|
||||||
|
|
||||||
|
Objective: improve research value with person-centric outputs, grounded in both archival
|
||||||
|
documents and the V6.2 genealogical data layer.
|
||||||
|
|
||||||
|
This version is broken into five sequential sub-versions because of real dependency
|
||||||
|
ordering: entity linking must exist before GEDCOM data can be targeted per-person; the
|
||||||
|
Facts/Events mechanism must exist before timelines or reconciliation have anything
|
||||||
|
meaningful to consume.
|
||||||
|
|
||||||
|
### V6.3.1 - Manual Entity Linking
|
||||||
|
|
||||||
|
- Search/browse UI over `genealogy_person` to find and link a candidate match to an
|
||||||
|
application `Person`, setting `family_search_id`. Linking is reversible (unlink).
|
||||||
|
- Once linked, GEDCOM vitals display alongside the `Person` record without requiring any
|
||||||
|
schema change to `Person`.
|
||||||
|
|
||||||
|
### V6.3.2 - Person Facts and Events
|
||||||
|
|
||||||
|
- New fact/event table capturing: person, fact type (birth/death/event/free-form), date
|
||||||
|
(+raw), place, free-text description, and a link to the source document as evidence.
|
||||||
|
- Manual tagging UI while reviewing a transcribed document: select a passage, choose the
|
||||||
|
person and fact type, record the date/description.
|
||||||
|
- One-time migration of existing `Person.birth_date`/`birth_date_raw`/`birth_place`/
|
||||||
|
`death_date`/`death_date_raw`/`death_place` values into fact/event rows (tagged as
|
||||||
|
legacy/no-document-evidence where no source document is known), followed by retiring those
|
||||||
|
six columns from `Person`. Birth/death become Facts/Events like any other locally-known
|
||||||
|
fact, for both linked and unlinked people. `Person` permanently keeps `last_name`,
|
||||||
|
`given_names`, `biography`, `family_search_id`, `metadata_`, tags, photos, and document
|
||||||
|
associations.
|
||||||
|
|
||||||
|
### V6.3.3 - Person Timelines
|
||||||
|
|
||||||
|
- Timeline query merging GEDCOM milestones (birth, marriage, children's births, death) for
|
||||||
|
linked persons with locally recorded Facts/Events.
|
||||||
|
- Timeline UI on Person Detail with clear ordering/filters; entries link back to their
|
||||||
|
originating document or GEDCOM record.
|
||||||
|
|
||||||
|
### V6.3.4 - Reconciliation
|
||||||
|
|
||||||
|
- Compares Facts/Events (the real, document-evidenced local signal) against corresponding
|
||||||
|
`genealogy_person` fields for linked persons.
|
||||||
|
- Persisted reconciliation record: person, field, local value with evidence-document link,
|
||||||
|
GEDCOM value, and status (open / submitted / dismissed).
|
||||||
|
- Re-evaluated automatically as part of each GEDCOM import maintenance run: opens new
|
||||||
|
discrepancies, auto-resolves ones where GEDCOM now matches, leaves others unchanged.
|
||||||
|
- Reconciliation review UI functions as a manual to-do list for updating FamilySearch; the
|
||||||
|
app does not write back to FamilySearch itself.
|
||||||
|
|
||||||
|
### V6.3.5 - AI-Assisted Biography Generation
|
||||||
|
|
||||||
|
- Prompted narrative generation grounded in GEDCOM facts, Facts/Events, and relevant
|
||||||
|
document snippets as structured input, using existing evidence-safe prompting patterns.
|
||||||
|
- Output cites back to source documents and FamilySearch records.
|
||||||
|
- Saved/printable report presentation for review; reports do not modify archival source
|
||||||
|
data.
|
||||||
|
|
||||||
|
### Exit Criteria (applies across V6.3.1-V6.3.5)
|
||||||
|
- Entity links are reversible and do not alter document associations.
|
||||||
|
- Timelines are reproducible from persisted records.
|
||||||
|
- Reconciliation items always carry a link to the document evidence justifying the local
|
||||||
|
value, and re-running GEDCOM import correctly opens, resolves, or leaves items unchanged.
|
||||||
|
- Narrative generation is traceable to source records and prompts.
|
||||||
|
- Reports can be reviewed without modifying archival source data.
|
||||||
|
|
||||||
|
## V6.4 - Access Control and Multi-User Readiness
|
||||||
|
|
||||||
|
[ *More thoughts on user accounts:*
|
||||||
|
* *Create a generic "view only" user that does not have the rights to alter any of the data*
|
||||||
|
* *Limit user accounts access to data by Tag. I have distant family members that I would want to share the transcribed data with, but they would only be interested in a subset of it. For example my Cochran cousins would have no interest in Lancaster documents, so limit the Cochra Clan cousins to view-only access to documents tagged "cochran clan"* ]
|
||||||
|
|
||||||
|
Objective: prepare for managed collaboration beyond single-user operation.
|
||||||
|
|
||||||
|
### Scope
|
||||||
|
1. Introduce application-level authentication.
|
||||||
|
2. Add role-based authorization (admin/editor/contributor/viewer).
|
||||||
|
3. Add audit visibility for user-attributed write actions.
|
||||||
|
|
||||||
|
### Deliverables
|
||||||
|
- User identity model and login/session flow.
|
||||||
|
- Route/page/service authorization enforcement.
|
||||||
|
- Audit metadata for sensitive create/update/delete workflows.
|
||||||
|
|
||||||
|
### Exit Criteria
|
||||||
|
- Unauthorized operations are blocked consistently across UI/API.
|
||||||
|
- Role policies are enforced by deterministic tests.
|
||||||
|
- User-attributed changes are visible for audit/review.
|
||||||
|
|
||||||
|
## Deferred / Future Ideas (not committed scope)
|
||||||
|
|
||||||
|
Captured for later consideration, not yet scheduled to a version:
|
||||||
|
* AI-assisted entity disambiguation (kinship co-occurrence, chronological plausibility
|
||||||
|
filtering) when linking document mentions to people.
|
||||||
|
* Kinship-aware `@mention` tagging while transcribing.
|
||||||
|
* Relationship-calculator badges (e.g., "3rd Great-Grandmother") in the document viewer.
|
||||||
|
* Interactive migration/geography mapping from GEDCOM and document place mentions.
|
||||||
|
* AI-suggested document discovery by date/location overlap with known persons.
|
||||||
|
* Ability to search within a document to find potential people to add to the People table.
|
||||||
|
|
||||||
|
## Planning Notes
|
||||||
|
|
||||||
|
- Keep architecture, schema, and UI contracts synchronized in `docs/` as each version lands.
|
||||||
|
- Prefer explicit schema migration over runtime compatibility write paths.
|
||||||
|
- Preserve evidence/provenance guarantees when adding new AI-powered features.
|
||||||
|
- GEDCOM/FamilySearch data is external, collaborative, and mutable; treat it as a managed
|
||||||
|
cache bridged via `fs_id`, never as a replacement for archival evidence recorded from
|
||||||
|
transcribed documents.
|
||||||
+391
@@ -0,0 +1,391 @@
|
|||||||
|
# Data Model and Persistence Schema (Current Baseline: V6.1)
|
||||||
|
|
||||||
|
This document is the field-accurate V6.1 schema contract aligned to `src/transcription/db/models.py`.
|
||||||
|
|
||||||
|
## Source of Truth Anchors
|
||||||
|
|
||||||
|
- `src/transcription/db/models.py` (status and purpose enums, including maintenance lifecycle enums)
|
||||||
|
- `src/transcription/db/models.py:80-120` (`DocumentType`, `PersonRole`)
|
||||||
|
- `src/transcription/db/models.py:122-172` (`Tag`, `Document`)
|
||||||
|
- `src/transcription/db/models.py` (`Person`, `GenealogyPerson`, `GenealogyFamily`, `GenealogyFamilyChild`, `GenealogyCitation`)
|
||||||
|
- `src/transcription/db/models.py` (`Photo`, `DocumentPerson`, `DocumentTag`)
|
||||||
|
- `src/transcription/db/models.py:285-347` (`Job`)
|
||||||
|
- `src/transcription/db/models.py` (`MaintenanceRun`)
|
||||||
|
- `src/transcription/db/models.py:350-462` (`Source`, `JobSource`)
|
||||||
|
- `src/transcription/db/models.py:465-522` (`ExecutionAttempt`)
|
||||||
|
|
||||||
|
## Entity Relationship Overview
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
erDiagram
|
||||||
|
DocumentType ||--o{ Document : classifies
|
||||||
|
Document ||--o{ Job : has
|
||||||
|
Document ||--o{ Source : has
|
||||||
|
Document ||--o{ DocumentPerson : links
|
||||||
|
Document ||--o{ DocumentTag : tagged
|
||||||
|
Person ||--o{ DocumentPerson : links
|
||||||
|
Person ||--o{ PersonTag : tagged
|
||||||
|
Person ||--o{ Photo : owns
|
||||||
|
GenealogyPerson ||--o{ GenealogyFamily : husband
|
||||||
|
GenealogyPerson ||--o{ GenealogyFamily : wife
|
||||||
|
GenealogyPerson ||--o{ GenealogyFamilyChild : child
|
||||||
|
GenealogyFamily ||--o{ GenealogyFamilyChild : includes
|
||||||
|
GenealogyPerson ||--o{ GenealogyCitation : cited
|
||||||
|
GenealogyFamily ||--o{ GenealogyCitation : cited
|
||||||
|
Document ||--o{ GenealogyCitation : evidence
|
||||||
|
PersonRole ||--o{ DocumentPerson : labels
|
||||||
|
Tag ||--o{ DocumentTag : labels
|
||||||
|
Tag ||--o{ PersonTag : labels
|
||||||
|
Job ||--o{ JobSource : includes
|
||||||
|
Source ||--o{ JobSource : participates
|
||||||
|
JobSource ||--o{ ExecutionAttempt : attempts
|
||||||
|
MaintenanceRun {
|
||||||
|
uuid id PK
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Authoritative Enumerations
|
||||||
|
|
||||||
|
### JobStatus
|
||||||
|
|
||||||
|
- `queued`
|
||||||
|
- `processing`
|
||||||
|
- `transcribed`
|
||||||
|
- `partial_success`
|
||||||
|
- `failed`
|
||||||
|
|
||||||
|
### JobSourceStatus
|
||||||
|
|
||||||
|
- `pending`
|
||||||
|
- `transcribed`
|
||||||
|
- `failed`
|
||||||
|
- `cancelled`
|
||||||
|
|
||||||
|
### JobPurpose
|
||||||
|
|
||||||
|
- `transcription`
|
||||||
|
- `retranscription`
|
||||||
|
|
||||||
|
### MaintenanceJobType
|
||||||
|
|
||||||
|
- `backup`
|
||||||
|
- `storage_reconciliation`
|
||||||
|
- `gedcom_import`
|
||||||
|
|
||||||
|
### MaintenanceRunStatus
|
||||||
|
|
||||||
|
- `queued`
|
||||||
|
- `processing`
|
||||||
|
- `succeeded`
|
||||||
|
- `failed`
|
||||||
|
|
||||||
|
## Field-Accurate Table Contracts
|
||||||
|
|
||||||
|
### `DocumentType`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `semantic_key` | `str \| None` | nullable unique, indexed |
|
||||||
|
| `label` | `str` | required |
|
||||||
|
| `normalized_label` | `str` | unique, indexed |
|
||||||
|
| `is_active` | `bool` | default `True` |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
| `updated_at` | `datetime` | default now, onupdate |
|
||||||
|
|
||||||
|
### `PersonRole`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `semantic_key` | `str \| None` | nullable unique, indexed |
|
||||||
|
| `label` | `str` | required |
|
||||||
|
| `normalized_label` | `str` | unique, indexed |
|
||||||
|
| `is_active` | `bool` | default `True` |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
| `updated_at` | `datetime` | default now, onupdate |
|
||||||
|
|
||||||
|
### `Tag`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `semantic_key` | `str \| None` | nullable unique, indexed |
|
||||||
|
| `label` | `str` | required |
|
||||||
|
| `normalized_label` | `str` | unique, indexed |
|
||||||
|
| `is_active` | `bool` | default `True` |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
| `updated_at` | `datetime` | default now, onupdate |
|
||||||
|
|
||||||
|
### `Document`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `name` | `str` | required |
|
||||||
|
| `document_type_id` | `UUID \| None` | FK -> `document_type.id`, indexed |
|
||||||
|
| `document_date` | `date \| None` | optional |
|
||||||
|
| `document_date_raw` | `str \| None` | optional |
|
||||||
|
| `location_created` | `str \| None` | optional |
|
||||||
|
| `notes` | `str \| None` | optional |
|
||||||
|
| `archive_identifier` | `str \| None` | optional |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
| `updated_at` | `datetime` | default now, onupdate |
|
||||||
|
|
||||||
|
### `Person`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `last_name` | `str` | required |
|
||||||
|
| `given_names` | `str` | required |
|
||||||
|
| `birth_date` | `date \| None` | optional |
|
||||||
|
| `birth_date_raw` | `str \| None` | optional |
|
||||||
|
| `birth_place` | `str \| None` | optional |
|
||||||
|
| `death_date` | `date \| None` | optional |
|
||||||
|
| `death_date_raw` | `str \| None` | optional |
|
||||||
|
| `death_place` | `str \| None` | optional |
|
||||||
|
| `biography` | `str \| None` | optional |
|
||||||
|
| `family_search_id` | `str \| None` | nullable unique |
|
||||||
|
| `metadata_` | `dict[str, JsonValue] \| None` | stored as DB column `metadata` (`JSONBCompat`) |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
| `updated_at` | `datetime` | default now, onupdate |
|
||||||
|
|
||||||
|
### `GenealogyPerson`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `fs_id` | `str` | unique, indexed FamilySearch identifier |
|
||||||
|
| `full_name` | `str` | required |
|
||||||
|
| `birth_date` | `date \| None` | optional |
|
||||||
|
| `birth_date_raw` | `str \| None` | optional |
|
||||||
|
| `birth_place` | `str \| None` | optional |
|
||||||
|
| `death_date` | `date \| None` | optional |
|
||||||
|
| `death_date_raw` | `str \| None` | optional |
|
||||||
|
| `death_place` | `str \| None` | optional |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
| `updated_at` | `datetime` | default now, onupdate |
|
||||||
|
|
||||||
|
### `GenealogyFamily`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `fs_family_id` | `str` | unique, indexed FamilySearch family identifier |
|
||||||
|
| `husband_id` | `UUID \| None` | nullable FK -> `genealogy_person.id`, indexed |
|
||||||
|
| `wife_id` | `UUID \| None` | nullable FK -> `genealogy_person.id`, indexed |
|
||||||
|
| `marriage_date` | `date \| None` | optional |
|
||||||
|
| `marriage_date_raw` | `str \| None` | optional |
|
||||||
|
| `marriage_place` | `str \| None` | optional |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
| `updated_at` | `datetime` | default now, onupdate |
|
||||||
|
|
||||||
|
### `GenealogyFamilyChild`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `family_id` | `UUID` | FK -> `genealogy_family.id`, indexed |
|
||||||
|
| `child_id` | `UUID` | FK -> `genealogy_person.id`, indexed |
|
||||||
|
| `relationship_type` | `str \| None` | optional |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
|
||||||
|
Constraint:
|
||||||
|
- `UniqueConstraint(family_id, child_id)` named `uq_genealogy_family_child`
|
||||||
|
|
||||||
|
### `GenealogyCitation`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `genealogy_person_id` | `UUID \| None` | nullable FK -> `genealogy_person.id`, indexed |
|
||||||
|
| `genealogy_family_id` | `UUID \| None` | nullable FK -> `genealogy_family.id`, indexed |
|
||||||
|
| `fact_type` | `GenealogyCitationFactType` | enum: `birth`, `death`, `marriage`, `other` |
|
||||||
|
| `raw_citation_text` | `str` | required raw GEDCOM citation text |
|
||||||
|
| `source_kind` | `GenealogyCitationSourceKind` | enum: `familysearch_imported`, `transcription_evidence` |
|
||||||
|
| `document_id` | `UUID \| None` | nullable FK -> `document.id`, indexed |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
|
||||||
|
### `Photo`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `person_id` | `UUID \| None` | nullable FK -> `person.id`, indexed (`NULL` = homepage photo) |
|
||||||
|
| `path` | `str` | required upload-root-relative POSIX path (`photos/...`) |
|
||||||
|
| `description` | `str \| None` | optional |
|
||||||
|
| `is_primary` | `bool` | default `False`; owner-level "featured/primary" marker |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
| `updated_at` | `datetime` | default now, onupdate |
|
||||||
|
|
||||||
|
### `DocumentPerson`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `document_id` | `UUID` | FK -> `document.id`, indexed |
|
||||||
|
| `person_id` | `UUID` | FK -> `person.id`, indexed |
|
||||||
|
| `role_id` | `UUID` | FK -> `person_role.id`, indexed |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
| `updated_at` | `datetime` | default now, onupdate |
|
||||||
|
|
||||||
|
Constraint:
|
||||||
|
- `UniqueConstraint(document_id, person_id)` named `uq_document_person`
|
||||||
|
|
||||||
|
### `DocumentTag`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `document_id` | `UUID` | FK -> `document.id`, indexed |
|
||||||
|
| `tag_id` | `UUID` | FK -> `tag.id`, indexed |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
| `updated_at` | `datetime` | default now, onupdate |
|
||||||
|
|
||||||
|
Constraint:
|
||||||
|
- `UniqueConstraint(document_id, tag_id)` named `uq_document_tag`
|
||||||
|
|
||||||
|
### `PersonTag`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `person_id` | `UUID` | FK -> `person.id`, indexed |
|
||||||
|
| `tag_id` | `UUID` | FK -> `tag.id`, indexed |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
| `updated_at` | `datetime` | default now, onupdate |
|
||||||
|
|
||||||
|
Constraint:
|
||||||
|
- `UniqueConstraint(person_id, tag_id)` named `uq_person_tag`
|
||||||
|
|
||||||
|
### `Job`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `document_id` | `UUID` | FK -> `document.id`, indexed |
|
||||||
|
| `status` | `JobStatus` | non-null enum (stored as enum values) |
|
||||||
|
| `retry_count` | `int` | default `0`, `ge=0` |
|
||||||
|
| `purpose` | `JobPurpose` | non-null enum, default `transcription` |
|
||||||
|
| `date_created` | `datetime` | default now |
|
||||||
|
| `date_updated` | `datetime` | default now, onupdate |
|
||||||
|
| `provider` | `str \| None` | optional |
|
||||||
|
| `model` | `str \| None` | optional |
|
||||||
|
| `prompt_name` | `str \| None` | optional |
|
||||||
|
| `prompt_hash` | `str \| None` | optional |
|
||||||
|
| `system_prompt` | `str \| None` | optional |
|
||||||
|
| `user_prompt` | `str \| None` | optional |
|
||||||
|
| `temperature` | `float \| None` | optional |
|
||||||
|
| `top_p` | `float \| None` | optional |
|
||||||
|
|
||||||
|
Index:
|
||||||
|
- `Index("ix_job_status_date_created", "status", "date_created")`
|
||||||
|
|
||||||
|
### `MaintenanceRun`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `job_type` | `MaintenanceJobType` | non-null enum |
|
||||||
|
| `status` | `MaintenanceRunStatus` | non-null enum, default `queued` |
|
||||||
|
| `started_at` | `datetime \| None` | optional |
|
||||||
|
| `finished_at` | `datetime \| None` | optional |
|
||||||
|
| `triggered_by` | `str \| None` | optional |
|
||||||
|
| `summary` | `str \| None` | optional |
|
||||||
|
| `log_path` | `str \| None` | optional, log-root-relative POSIX path |
|
||||||
|
| `error_detail` | `str \| None` | optional internal failure detail |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
| `updated_at` | `datetime` | default now, onupdate |
|
||||||
|
|
||||||
|
### `Source`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `document_id` | `UUID` | FK -> `document.id`, indexed |
|
||||||
|
| `page_number` | `int` | default `1`, `ge=1` |
|
||||||
|
| `upload_name` | `str` | required |
|
||||||
|
| `filename` | `str` | required |
|
||||||
|
| `file_path` | `str` | required upload-root-relative POSIX path (`documents/...`) |
|
||||||
|
| `file_hash` | `str` | required |
|
||||||
|
| `file_size_bytes` | `int` | `BigInteger`, non-null |
|
||||||
|
| `raw_transcription` | `str \| None` | projection field |
|
||||||
|
| `preferred_execution_attempt_id` | `UUID \| None` | nullable FK -> `execution_attempt.id`, indexed (`use_alter`) |
|
||||||
|
| `revised_text` | `str \| None` | optional human revision |
|
||||||
|
| `date_uploaded` | `datetime` | default now |
|
||||||
|
| `date_revised` | `datetime \| None` | optional |
|
||||||
|
|
||||||
|
### `JobSource`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `job_id` | `UUID` | FK -> `job.id`, indexed |
|
||||||
|
| `source_id` | `UUID` | FK -> `source.id`, indexed |
|
||||||
|
| `status` | `JobSourceStatus` | non-null enum, default `pending` |
|
||||||
|
|
||||||
|
Constraint:
|
||||||
|
- `UniqueConstraint(job_id, source_id)` named `uq_job_source_job_source`
|
||||||
|
|
||||||
|
Runtime reconciliation:
|
||||||
|
- Startup database operations remove retired V4.6 `job_source` evidence columns (`raw_transcription`, `ai_metadata`, `raw_api_response`, `error_detail`, `executed_at`) when present so persisted schema matches this contract.
|
||||||
|
|
||||||
|
### `ExecutionAttempt`
|
||||||
|
|
||||||
|
| Field | Type | Notes |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| `id` | `UUID` | PK |
|
||||||
|
| `job_source_id` | `UUID` | FK -> `job_source.id`, indexed |
|
||||||
|
| `job_id` | `UUID` | FK -> `job.id`, indexed |
|
||||||
|
| `source_id` | `UUID` | FK -> `source.id`, indexed |
|
||||||
|
| `attempt_number` | `int` | `ge=1` |
|
||||||
|
| `status` | `JobSourceStatus` | non-null enum, value-stable with `JobSource.status` |
|
||||||
|
| `provider` | `str` | required |
|
||||||
|
| `model` | `str \| None` | optional |
|
||||||
|
| `request_manifest` | `dict[str, JsonValue] \| None` | JSONBCompat |
|
||||||
|
| `request_manifest_sha256` | `str \| None` | optional |
|
||||||
|
| `request_manifest_schema_version` | `str \| None` | optional |
|
||||||
|
| `response_received` | `bool` | default `False` |
|
||||||
|
| `transport_status_code` | `int \| None` | optional |
|
||||||
|
| `transport_body` | `bytes \| None` | LargeBinary |
|
||||||
|
| `transport_content_type` | `str \| None` | optional |
|
||||||
|
| `transport_content_encoding` | `str \| None` | optional |
|
||||||
|
| `transport_safe_headers` | `dict[str, JsonValue] \| None` | JSONBCompat |
|
||||||
|
| `router_request_id` | `str \| None` | optional |
|
||||||
|
| `router_generation_id` | `str \| None` | optional |
|
||||||
|
| `sdk_response_snapshot` | `dict[str, JsonValue] \| None` | JSONBCompat |
|
||||||
|
| `normalized_metadata` | `dict[str, JsonValue] \| None` | JSONBCompat; may include app-namespaced `processing_timing` (`provider_call_duration_ms`, `processing_duration_ms`) |
|
||||||
|
| `software_context` | `dict[str, JsonValue] \| None` | JSONBCompat |
|
||||||
|
| `raw_transcription` | `str \| None` | optional |
|
||||||
|
| `error_category` | `str \| None` | optional |
|
||||||
|
| `error_detail` | `str \| None` | optional |
|
||||||
|
| `failure_phase` | `str \| None` | optional |
|
||||||
|
| `started_at` | `datetime` | required |
|
||||||
|
| `finished_at` | `datetime` | required |
|
||||||
|
| `duration_ms` | `int` | `ge=0` |
|
||||||
|
| `created_at` | `datetime` | default now |
|
||||||
|
|
||||||
|
Constraint:
|
||||||
|
- `UniqueConstraint(job_id, source_id, attempt_number)` named `uq_execution_attempt_number`
|
||||||
|
|
||||||
|
## Relationship Loading Contract
|
||||||
|
|
||||||
|
- Most ORM relationships are configured with `lazy="raise"`.
|
||||||
|
- `JobSource.execution_attempts` is intentionally `lazy="noload"` with ordered attempts.
|
||||||
|
- Service/UI read paths must explicitly eager-load required relationships before access.
|
||||||
|
|
||||||
|
## Persistence Invariants (Ground Truth)
|
||||||
|
|
||||||
|
1. `ExecutionAttempt` is append-only runtime evidence.
|
||||||
|
2. `JobSource.status` represents queue/projection execution state and is not a full evidence container.
|
||||||
|
3. `Source.raw_transcription` is a mutable projection and not authoritative attempt history.
|
||||||
|
4. `Job` terminal status derives from page outcomes (`JobSource` state), not from a separate summary table.
|
||||||
|
5. `DocumentType.semantic_key` and `PersonRole.semantic_key` are nullable-unique semantic identifiers.
|
||||||
|
|
||||||
|
## Cross-Reference
|
||||||
|
|
||||||
|
- [System Architecture](architecture.md)
|
||||||
|
- [System Requirements](requirements.md)
|
||||||
|
- [Error Handling Policy](error_handling.md)
|
||||||
|
- [AI Evidence and Provenance Invariant](./invariant/ai_evidence_and_provenance.md)
|
||||||
+4
-5
@@ -13,6 +13,7 @@ These documents are written for maintainers and AI contributors. They are behavi
|
|||||||
- [People](pages/people.md)
|
- [People](pages/people.md)
|
||||||
- [Jobs](pages/jobs.md)
|
- [Jobs](pages/jobs.md)
|
||||||
- [Sources](pages/sources.md)
|
- [Sources](pages/sources.md)
|
||||||
|
- [Settings](pages/settings.md)
|
||||||
|
|
||||||
NiceGUI registers the routes shown in each contract without the `/ui` prefix. The application mounts NiceGUI under `/ui`, so `/documents` in page code is served to a browser as `/ui/documents`.
|
NiceGUI registers the routes shown in each contract without the `/ui` prefix. The application mounts NiceGUI under `/ui`, so `/documents` in page code is served to a browser as `/ui/documents`.
|
||||||
|
|
||||||
@@ -25,9 +26,8 @@ When documents disagree, use this order:
|
|||||||
3. UI dependency and ownership boundaries: [UI contributor instructions](../../.github/instructions/ui.instructions.md).
|
3. UI dependency and ownership boundaries: [UI contributor instructions](../../.github/instructions/ui.instructions.md).
|
||||||
4. Durable failure behavior: [Error Handling invariant](../invariant/error_handling.md).
|
4. Durable failure behavior: [Error Handling invariant](../invariant/error_handling.md).
|
||||||
5. Durable AI evidence behavior: [Digital Evidence and AI Processing Provenance](../invariant/ai_evidence_and_provenance.md).
|
5. Durable AI evidence behavior: [Digital Evidence and AI Processing Provenance](../invariant/ai_evidence_and_provenance.md).
|
||||||
6. Data definitions and relationships: current models plus the [V4 schema](../ver4/schema_v4.md).
|
6. Data definitions and relationships: current models plus the [schema contract](../schema.md).
|
||||||
7. Planned behavior changes: the applicable V4.x scope and implementation documents.
|
7. Implementation truth: current code and tests.
|
||||||
8. Implementation truth: current code and tests.
|
|
||||||
|
|
||||||
If code intentionally changes accepted page behavior, update the corresponding page contract in the same change. If code accidentally differs, correct the implementation rather than rewriting intent to match a defect.
|
If code intentionally changes accepted page behavior, update the corresponding page contract in the same change. If code accidentally differs, correct the implementation rather than rewriting intent to match a defect.
|
||||||
|
|
||||||
@@ -57,5 +57,4 @@ Each page contract contains:
|
|||||||
|
|
||||||
## Current Baseline
|
## Current Baseline
|
||||||
|
|
||||||
These contracts describe the V4 baseline with completed V4.1 behavior and V4.2 evidence/provenance behavior.
|
These contracts describe the current V6.1 baseline.
|
||||||
Draft V4.3 Settings changes are not described as current behavior.
|
|
||||||
|
|||||||
+57
-12
@@ -11,21 +11,26 @@ Documents manages the archival record for each historical artifact independently
|
|||||||
| `/documents` | Searchable archival Document list. |
|
| `/documents` | Searchable archival Document list. |
|
||||||
| `/documents/new` | Create a Document. |
|
| `/documents/new` | Create a Document. |
|
||||||
| `/documents/{document_id}` | View one Document and its related records. |
|
| `/documents/{document_id}` | View one Document and its related records. |
|
||||||
| `/documents/{document_id}/edit` | Edit metadata and people-by-role links. |
|
| `/documents/{document_id}/info` | View archival metadata and system logistics for one Document. |
|
||||||
|
| `/documents/{document_id}/edit` | Edit metadata and the complete Linked People set. |
|
||||||
| `/documents/{document_id}/delete` | Confirm or block deletion. |
|
| `/documents/{document_id}/delete` | Confirm or block deletion. |
|
||||||
| `/documents/{document_id}/jobs` | Show Jobs belonging to the Document. |
|
| `/documents/{document_id}/jobs` | Show Jobs belonging to the Document. |
|
||||||
| `/documents/{document_id}/sources` | Redirect to the Document-filtered Sources list. |
|
| `/documents/{document_id}/sources` | Source-image gallery for the Document. |
|
||||||
|
| `/documents/{document_id}/print` | Preview and browser-print the persisted Document. |
|
||||||
|
|
||||||
## List Behavior
|
## List Behavior
|
||||||
|
|
||||||
- The title is **Archival Documents**.
|
- The title is **Archival Documents**.
|
||||||
- **Create new document** opens the create route.
|
- **Create new document** opens the create route.
|
||||||
- The table defaults to Document Title order and supports search and column sorting.
|
- The table defaults to Document Title order and supports search and column sorting.
|
||||||
- Columns are Document Title, Type, Author, Document Date, and Archive Ref.
|
- Columns are Document Title, Author, Tags, Document Date, Type, # Sources, and Transcription Status.
|
||||||
- Document Title is left-aligned; the remaining columns are centered.
|
- Document Title is left-aligned; the remaining columns are centered.
|
||||||
- Author lists all linked people in the `author` role.
|
- Author lists all linked people in the `author` role.
|
||||||
|
- # Sources reflects the count of linked Source rows for each Document.
|
||||||
|
- Transcription Status reflects the most recent Job status for that Document; documents with no Jobs show a blank marker.
|
||||||
- Date display prefers exact date, then approximate date, then `Unknown`.
|
- Date display prefers exact date, then approximate date, then `Unknown`.
|
||||||
- Selecting a row opens Document Detail.
|
- Selecting a row opens Document Detail.
|
||||||
|
- Row navigation includes list context so Document Detail provides **Back to Documents**.
|
||||||
- No records displays `No documents found in repository.`
|
- No records displays `No documents found in repository.`
|
||||||
|
|
||||||
## Create and Edit Behavior
|
## Create and Edit Behavior
|
||||||
@@ -42,30 +47,64 @@ Optional:
|
|||||||
- Document location.
|
- Document location.
|
||||||
- Archive identifier.
|
- Archive identifier.
|
||||||
- Notes.
|
- Notes.
|
||||||
- Multiple people for every configured Person Role.
|
- Tags.
|
||||||
|
- Linked People, with exactly one Person Role per linked Person.
|
||||||
|
|
||||||
Rules:
|
Rules:
|
||||||
|
|
||||||
- Exact date must parse as `YYYY-MM-DD`; browser presentation may follow locale.
|
- Exact date must parse as `YYYY-MM-DD`; browser presentation may follow locale.
|
||||||
|
- The exact-date input is labeled **Document date**.
|
||||||
- Existing people appear with disambiguating labels.
|
- Existing people appear with disambiguating labels.
|
||||||
|
- Tag assignment supports selecting existing tags and adding new labels inline.
|
||||||
- **Create new person** opens Person creation.
|
- **Create new person** opens Person creation.
|
||||||
- `person_id` may preselect that Person in the author role on Document creation.
|
- `person_id` may preselect that Person in the author role on Document creation.
|
||||||
- An invalid requested Person produces a warning rather than a broken form.
|
- An invalid requested Person produces a warning rather than a broken form.
|
||||||
- `return_to=jobs_new` returns a successful create to Job creation with the new Document selected.
|
- `return_to=jobs_new` returns a successful create to Job creation with the new Document selected.
|
||||||
- Edit includes active and inactive Document Types so historical values remain maintainable.
|
- Edit includes active and inactive Document Types so historical values remain maintainable.
|
||||||
|
- One Linked People table contains Select, Person, and Role columns.
|
||||||
|
- Add and Edit use an inline Person/Role editor; Save, Cancel, and Delete change staged UI state only.
|
||||||
|
- A Person may appear once per Document regardless of role.
|
||||||
|
- Existing inactive-role links remain visible; only active roles may be newly assigned.
|
||||||
|
- Document fields and the complete staged link set commit atomically on the main save.
|
||||||
- Save success returns to Document Detail.
|
- Save success returns to Document Detail.
|
||||||
|
|
||||||
## Detail Behavior
|
## Detail Behavior
|
||||||
|
|
||||||
- The heading shows name, type, and internal ID.
|
- The heading shows name, type, and internal ID.
|
||||||
- The first Source, when present, appears in the dark-room viewer.
|
- The header includes a contextual back action: **Back to Documents** by default, **Back to Person** when opened from Person Detail, and **Back to Job** when opened from Job Detail.
|
||||||
- Archival Metadata shows authors, compact Document date, location, and archive identifier. Notes appear in a separate archival-notes block within the same card.
|
- The detail workspace shows a Source-style pan/zoom media viewer with **Previous Page** / **Next Page** navigation for document source pages.
|
||||||
- System Logistics shows created and updated timestamps.
|
- The center column is **Editable Revision** for the active source page.
|
||||||
- Related People are grouped by role and link to Person Detail.
|
- Related People are grouped by role and link to Person Detail.
|
||||||
- **Sources & Pipeline Jobs** shows counts and actions for filtered Sources, Document Jobs, and adding a Job.
|
- **Source Pages & Transcriptions** shows source/job counts and actions for source-image gallery, document jobs, and adding a Job.
|
||||||
- **Edit Document** and **Delete** are available from the header.
|
- **Edit Document**, **Print**, **Document Details**, **View Source Detail**, and **Delete** are available from the header.
|
||||||
- Invalid IDs and missing Documents produce explicit states without rendering a partial page.
|
- Invalid IDs and missing Documents produce explicit states without rendering a partial page.
|
||||||
|
|
||||||
|
## Document Source Images Behavior
|
||||||
|
|
||||||
|
- `/documents/{document_id}/sources` shows the current Document's source pages in a thumbnail gallery.
|
||||||
|
- Each card shows the page number, stored filename, and an **Open Source Detail** action.
|
||||||
|
- The page includes a **Back to Document** action.
|
||||||
|
- No source pages displays an explicit empty state.
|
||||||
|
|
||||||
|
## Document Info Behavior
|
||||||
|
|
||||||
|
- `/documents/{document_id}/info` contains **Archival Metadata** and **System Logistics**.
|
||||||
|
- It includes a **Back to Document** action.
|
||||||
|
- Archival metadata includes authors, document type, tags, document date, location (linked when present), archive identifier, and notes.
|
||||||
|
|
||||||
|
## Print Behavior
|
||||||
|
|
||||||
|
- Print opens a dedicated preview for persisted Document data.
|
||||||
|
- **Facsimile** places each Source image beside its current transcription and starts every Source on a new printed sheet.
|
||||||
|
- **Text only** omits images, joins single line breaks inside paragraphs, and preserves blank-line paragraph boundaries.
|
||||||
|
- Non-null revised text takes precedence over raw transcription, including an intentionally empty revision.
|
||||||
|
- Archival metadata resolves Author through the hidden built-in semantic identity, not its mutable label.
|
||||||
|
- Archival metadata includes the Document Type label.
|
||||||
|
- Metadata tables use a narrow non-wrapping label column and wider wrapping data columns rather than stretching across the page.
|
||||||
|
- Job metadata uses one oldest-to-newest column per Job and ends with Status.
|
||||||
|
- Stored text is escaped and Source media uses record-validated application URLs rather than local file paths.
|
||||||
|
- Printing uses the browser print dialog; server-generated PDFs are not provided.
|
||||||
|
|
||||||
## Document Jobs Behavior
|
## Document Jobs Behavior
|
||||||
|
|
||||||
- The page lists the Document's Jobs newest first with status and Job ID.
|
- The page lists the Document's Jobs newest first with status and Job ID.
|
||||||
@@ -84,10 +123,12 @@ Rules:
|
|||||||
|
|
||||||
- List columns, alignment, search, sorting, date fallback, and row navigation match this contract.
|
- List columns, alignment, search, sorting, date fallback, and row navigation match this contract.
|
||||||
- Create/edit enforce name, registered type, and valid exact-date input.
|
- Create/edit enforce name, registered type, and valid exact-date input.
|
||||||
- Multiple people can be selected independently for each configured role.
|
- Linked People staging enforces one role and one row per Person.
|
||||||
|
- Document and Linked People writes never partially commit.
|
||||||
- Person-first Document creation preselects the requested Person as author.
|
- Person-first Document creation preselects the requested Person as author.
|
||||||
- Detail links people, Sources, and Jobs to the correct records.
|
- Detail links people, Sources, and Jobs to the correct records.
|
||||||
- Delete never removes a Document with Source or Job dependencies.
|
- Delete never removes a Document with Source or Job dependencies.
|
||||||
|
- Both print formats preserve the frozen content, ordering, text-precedence, and safety contracts.
|
||||||
- Service failures use the shared error presenter and never report false success.
|
- Service failures use the shared error presenter and never report false success.
|
||||||
|
|
||||||
## Implementation Anchors
|
## Implementation Anchors
|
||||||
@@ -96,10 +137,14 @@ Rules:
|
|||||||
- `src/transcription/ui/components/table/documents.py`
|
- `src/transcription/ui/components/table/documents.py`
|
||||||
- `src/transcription/services/documents.py`
|
- `src/transcription/services/documents.py`
|
||||||
- `src/transcription/services/people.py`
|
- `src/transcription/services/people.py`
|
||||||
|
- `src/transcription/services/workflows.py`
|
||||||
|
- `src/transcription/ui/components/linked_people.py`
|
||||||
|
- `src/transcription/ui/pages/print_preview_page.py`
|
||||||
|
- `src/transcription/api/print_api.py`
|
||||||
- `tests/ui/test_documents_page.py`
|
- `tests/ui/test_documents_page.py`
|
||||||
- `tests/services/test_document_service.py`
|
- `tests/services/test_document_service.py`
|
||||||
|
|
||||||
## Known Limitations and Deferred Work
|
## Known Limitations and Deferred Work
|
||||||
|
|
||||||
- Document creation persists the Document before adding relationship links; a later link failure is surfaced but is not currently one atomic write.
|
- Source page ordering remains read-only.
|
||||||
- Source ordering controls are deferred to the [draft V4.3 scope](../../ver4.3/scope_boundary_v4_3.md).
|
- Printing other entities, batch printing, and server-side export formats are deferred.
|
||||||
|
|||||||
+15
-14
@@ -2,48 +2,50 @@
|
|||||||
|
|
||||||
## Purpose
|
## Purpose
|
||||||
|
|
||||||
Home provides a user-maintained landing page for the local archive. It combines one current image with Markdown text and lets the operator edit both without changing application source or prompt assets.
|
Home provides a user-maintained landing page for the local archive. It combines a database-backed image gallery with Markdown text and lets the operator edit both without changing application source or prompt assets.
|
||||||
|
|
||||||
## Routes
|
## Routes
|
||||||
|
|
||||||
| Route | Browser path | Purpose |
|
| Route | Browser path | Purpose |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| `/homepage` | `/ui/homepage` | View current homepage image and Markdown. |
|
| `/homepage` | `/ui/homepage` | View homepage gallery and Markdown. |
|
||||||
| `/homepage/edit` | `/ui/homepage/edit` | Upload an image and edit Markdown. |
|
| `/homepage/edit` | `/ui/homepage/edit` | Upload images, manage image metadata, and edit Markdown. |
|
||||||
|
|
||||||
The application root and `/ui` redirect to `/ui/homepage`.
|
The application root and `/ui` redirect to `/ui/homepage`.
|
||||||
|
|
||||||
## View Behavior
|
## View Behavior
|
||||||
|
|
||||||
- The visible page heading is **Home**; the browser tab title is **VibeScribe Home**.
|
- The visible page heading is **Home**; the browser tab title is **VibeScribe Home**.
|
||||||
- The latest homepage image appears in the shared dark-room viewer.
|
- The featured homepage image (`photo.is_primary`) is shown first; remaining images are shown in random order.
|
||||||
|
- The current image appears in the shared dark-room viewer with its description.
|
||||||
- Saved Markdown is rendered in the **Home Text** card.
|
- Saved Markdown is rendered in the **Home Text** card.
|
||||||
- Missing text displays `No homepage text saved yet.`
|
- Missing text displays `No homepage text saved yet.`
|
||||||
- Missing image displays the viewer's empty state.
|
- Missing image displays the viewer's empty state.
|
||||||
- **Edit Home Page** opens the edit route.
|
- **Edit Home Page** opens the edit route.
|
||||||
|
- The same Home Text content is also editable from **Settings → Home Page Text**.
|
||||||
|
|
||||||
## Edit Behavior
|
## Edit Behavior
|
||||||
|
|
||||||
- The image upload accepts JPEG, PNG, GIF, WebP, BMP, and TIFF files.
|
- The image upload accepts JPEG, PNG, GIF, WebP, BMP, and TIFF files and supports multi-file uploads.
|
||||||
- A successful upload immediately stores the file, updates the preview to that image, and displays a positive notification.
|
- A successful upload immediately stores files in the shared `photo` table/media layout and displays a positive notification.
|
||||||
|
- The editor supports per-image description edits, setting a featured image, and deleting the current image.
|
||||||
- The Markdown textarea is initialized from the currently stored homepage text.
|
- The Markdown textarea is initialized from the currently stored homepage text.
|
||||||
- **Save** writes the textarea content, displays `Homepage saved`, and returns to Home.
|
- **Save** writes the textarea content, displays `Homepage saved`, and returns to Home.
|
||||||
- **Cancel** returns to Home without saving textarea changes. An image already uploaded during the edit session remains stored.
|
- **Cancel** returns to Home without saving textarea changes. An image already uploaded during the edit session remains stored.
|
||||||
|
|
||||||
## Storage Contract
|
## Storage Contract
|
||||||
|
|
||||||
- Homepage content is mutable application data under `data/homepage`.
|
- Homepage markdown text is mutable application data at `UPLOAD_DIR/homepage.md`.
|
||||||
- Markdown is stored in `homepage.md`.
|
- Homepage images are stored as `photo` rows (`person_id = NULL`) with files under `UPLOAD_DIR/photos/`.
|
||||||
- Uploaded images keep a sanitized basename.
|
- Uploaded images are renamed to `{photo_id}{suffix}`.
|
||||||
- The view selects the supported image with the most recent modification time.
|
- Homepage images are database records; markdown remains file-backed.
|
||||||
- Homepage files are not transcription prompts and are not database records.
|
|
||||||
|
|
||||||
## Acceptance Checklist
|
## Acceptance Checklist
|
||||||
|
|
||||||
- `/`, `/ui`, and the application brand reach Home.
|
- `/`, `/ui`, and the application brand reach Home.
|
||||||
- Home renders with or without stored Markdown and image content.
|
- Home renders with or without stored Markdown and image content.
|
||||||
- Edit loads existing Markdown.
|
- Edit loads existing Markdown.
|
||||||
- A supported image upload updates the preview and becomes the latest homepage image.
|
- Supported image upload stores one or more images and makes the first image featured when no featured image exists yet.
|
||||||
- Save persists Markdown and returns to Home.
|
- Save persists Markdown and returns to Home.
|
||||||
- Cancel does not save changed Markdown.
|
- Cancel does not save changed Markdown.
|
||||||
|
|
||||||
@@ -58,6 +60,5 @@ The application root and `/ui` redirect to `/ui/homepage`.
|
|||||||
|
|
||||||
## Known Limitations
|
## Known Limitations
|
||||||
|
|
||||||
- Homepage storage is fixed under the repository/application `data` directory rather than a configured application-data root.
|
- Homepage markdown storage location is `UPLOAD_DIR/homepage.md` and must remain writable in the active runtime environment.
|
||||||
- Uploading an image is immediate and is not rolled back by Cancel.
|
- Uploading an image is immediate and is not rolled back by Cancel.
|
||||||
- The editor does not currently delete or select among previously uploaded images.
|
|
||||||
|
|||||||
+11
-6
@@ -19,10 +19,12 @@ Jobs manages transcription processing runs. A Job belongs to one Document, links
|
|||||||
|
|
||||||
- The title is **Transcription Pipeline Jobs**.
|
- The title is **Transcription Pipeline Jobs**.
|
||||||
- **Create job** opens Job creation and **Refresh** reloads the table.
|
- **Create job** opens Job creation and **Refresh** reloads the table.
|
||||||
- Columns are Job ID, Status, Source Filename, Retries, Created, and Updated.
|
- Columns are Job ID, Status, Document Name, # Sources, Retries, and Updated.
|
||||||
- Search covers Job ID, filename, and status.
|
- Updated is the primary date/sort field.
|
||||||
|
- Search covers Job ID, document name, and status.
|
||||||
- Status is displayed as a semantic status chip.
|
- Status is displayed as a semantic status chip.
|
||||||
- Selecting a row opens Job Detail.
|
- Selecting a row opens Job Detail.
|
||||||
|
- Global Job-list row navigation includes list context so Job Detail provides **Back to Jobs**.
|
||||||
- No records displays `No job records found in repository.`
|
- No records displays `No job records found in repository.`
|
||||||
|
|
||||||
## Create Behavior
|
## Create Behavior
|
||||||
@@ -30,18 +32,21 @@ Jobs manages transcription processing runs. A Job belongs to one Document, links
|
|||||||
- A Target Document and at least one source file are required.
|
- A Target Document and at least one source file are required.
|
||||||
- `document_id` may preselect a Target Document.
|
- `document_id` may preselect a Target Document.
|
||||||
- If no Documents exist, the page explains the prerequisite and links to Document creation with a return path.
|
- If no Documents exist, the page explains the prerequisite and links to Document creation with a return path.
|
||||||
- Provider and Model are optional request overrides.
|
- Provider and Model are selectable when creating a new Job.
|
||||||
- Upload accepts JPEG, PNG, TIFF, and PDF files and supports multiple/folder selection.
|
- Upload accepts JPEG, PNG, TIFF, and PDF files and supports multiple/folder selection.
|
||||||
- The visible upload queue is sorted alphabetically by original filename.
|
- The visible upload queue is sorted alphabetically by original filename.
|
||||||
- Files can be removed individually or cleared before submission.
|
- Files can be removed individually or cleared before submission.
|
||||||
- Helper text explains numeric filename prefixes for page ordering.
|
- Helper text explains numeric filename prefixes for page ordering.
|
||||||
- Submission creates the Job, Source records, and JobSource links, notifies the worker, and opens Job Detail.
|
- Submission creates the Job, Source records, and JobSource links, notifies the worker, and opens Job Detail.
|
||||||
|
- When opened with `source_id`, creation becomes a retranscription flow: Source and Document are locked, Provider is
|
||||||
|
read-only, Model is restricted to `PROVIDER_MODELS`, no upload is accepted, and one existing Source is linked.
|
||||||
|
|
||||||
## Detail and Lifecycle Behavior
|
## Detail and Lifecycle Behavior
|
||||||
|
|
||||||
- The heading shows Job ID and a status badge.
|
- The heading shows Job ID and a status badge.
|
||||||
|
- Job Detail includes a contextual back action: **Back to Jobs** by default and **Back to Document** when opened from a Document-filtered Job list.
|
||||||
- Execution Logistics shows provider, model, prompt, retry count, and last update.
|
- Execution Logistics shows provider, model, prompt, retry count, and last update.
|
||||||
- Document Links open the parent Document and Job-filtered Sources.
|
- Document Links show a clickable Document Name (with Job context), Sources count, and a single **View Sources** action using job filtering.
|
||||||
- Queued and processing Jobs show an auto-refresh notice and reload every four seconds.
|
- Queued and processing Jobs show an auto-refresh notice and reload every four seconds.
|
||||||
- Polling stops when the Job becomes terminal or a refresh fails.
|
- Polling stops when the Job becomes terminal or a refresh fails.
|
||||||
- Queued and processing Jobs expose **Cancel**.
|
- Queued and processing Jobs expose **Cancel**.
|
||||||
@@ -51,7 +56,7 @@ Jobs manages transcription processing runs. A Job belongs to one Document, links
|
|||||||
|
|
||||||
## Cancel Behavior
|
## Cancel Behavior
|
||||||
|
|
||||||
- The confirmation explains that processing stops and remaining non-transcribed Sources become failed.
|
- The confirmation explains that processing stops and remaining pending Sources become cancelled.
|
||||||
- The service decides whether the current state permits cancellation.
|
- The service decides whether the current state permits cancellation.
|
||||||
- Success updates the Job, notifies the worker, and returns to Job Detail.
|
- Success updates the Job, notifies the worker, and returns to Job Detail.
|
||||||
|
|
||||||
@@ -61,7 +66,7 @@ Jobs manages transcription processing runs. A Job belongs to one Document, links
|
|||||||
- The page explains that resubmission queues failed linked Sources while preserving immutable prior attempt evidence.
|
- The page explains that resubmission queues failed linked Sources while preserving immutable prior attempt evidence.
|
||||||
- The service blocks submission while processing is active or when no failed Sources exist.
|
- The service blocks submission while processing is active or when no failed Sources exist.
|
||||||
- `JobSource` remains the latest compatibility projection, while every provider call appends an `ExecutionAttempt`.
|
- `JobSource` remains the latest compatibility projection, while every provider call appends an `ExecutionAttempt`.
|
||||||
- The latest successful `Source.raw_transcription` projection remains available while a retry is pending or fails.
|
- The selected `Source.raw_transcription` projection remains available while a retry is pending or fails.
|
||||||
- Success reports the number of resubmitted Sources and returns to Job Detail.
|
- Success reports the number of resubmitted Sources and returns to Job Detail.
|
||||||
|
|
||||||
## Delete Behavior
|
## Delete Behavior
|
||||||
|
|||||||
+31
-15
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## Purpose
|
## Purpose
|
||||||
|
|
||||||
People manages reusable historical-person records. A Person may appear in many Documents under different relationship roles and may optionally carry a portrait and FamilySearch identifier.
|
People manages reusable historical-person records. A Person may appear in many Documents under different relationship roles and may optionally carry one or more photos plus a FamilySearch identifier.
|
||||||
|
|
||||||
## Routes
|
## Routes
|
||||||
|
|
||||||
@@ -11,6 +11,7 @@ People manages reusable historical-person records. A Person may appear in many D
|
|||||||
| `/people` | Searchable People list. |
|
| `/people` | Searchable People list. |
|
||||||
| `/people/new` | Create a Person. |
|
| `/people/new` | Create a Person. |
|
||||||
| `/people/{person_id}` | View one Person and linked Documents. |
|
| `/people/{person_id}` | View one Person and linked Documents. |
|
||||||
|
| `/people/{person_id}/photos` | Manage Person photos. |
|
||||||
| `/people/{person_id}/edit` | Edit the Person. |
|
| `/people/{person_id}/edit` | Edit the Person. |
|
||||||
| `/people/{person_id}/delete` | Confirm permanent deletion. |
|
| `/people/{person_id}/delete` | Confirm permanent deletion. |
|
||||||
|
|
||||||
@@ -18,61 +19,76 @@ People manages reusable historical-person records. A Person may appear in many D
|
|||||||
|
|
||||||
- The title is **Archival Entities: People**.
|
- The title is **Archival Entities: People**.
|
||||||
- **Create new person** opens the create route.
|
- **Create new person** opens the create route.
|
||||||
- The table defaults to Full Name order and supports search and column sorting.
|
- The table defaults to Name order (`Last Name, First & Middle`) and supports search and column sorting.
|
||||||
- Columns are Full Name, Display Name, Maiden Name, Birth Date, and Death Date.
|
- Columns are Last Name, First & Middle; Tags; FamilySearch ID; Birth Date; Death Date; and # Documents.
|
||||||
- Full Name is left-aligned; Display Name, Maiden Name, and date columns are centered.
|
- Name and Tags are left-aligned; FamilySearch ID, date columns, and # Documents are centered.
|
||||||
|
- # Documents reflects how many linked Documents each Person is connected to.
|
||||||
- Birth and death values independently prefer exact date, then approximate date, then `Unknown`.
|
- Birth and death values independently prefer exact date, then approximate date, then `Unknown`.
|
||||||
- Selecting a row opens Person Detail.
|
- Selecting a row opens Person Detail.
|
||||||
|
- Row navigation includes list context so Person Detail provides **Back to People**.
|
||||||
- No records displays `No person records found in repository.`
|
- No records displays `No person records found in repository.`
|
||||||
|
|
||||||
## Create and Edit Behavior
|
## Create and Edit Behavior
|
||||||
|
|
||||||
Required:
|
Required:
|
||||||
|
|
||||||
- Full name.
|
- Last name.
|
||||||
|
- First & middle names.
|
||||||
|
|
||||||
Optional:
|
Optional:
|
||||||
|
|
||||||
- Display name and maiden name.
|
|
||||||
- Exact and approximate birth/death dates.
|
- Exact and approximate birth/death dates.
|
||||||
- Birth/death places.
|
- Birth/death places.
|
||||||
- Biography.
|
- Biography.
|
||||||
- Portrait path or uploaded portrait.
|
|
||||||
- FamilySearch ID.
|
- FamilySearch ID.
|
||||||
|
- Tags.
|
||||||
|
|
||||||
Rules:
|
Rules:
|
||||||
|
|
||||||
- Missing Full name blocks save with a warning.
|
- Missing last name or first/middle names blocks save with a warning.
|
||||||
- Exact date inputs are native browser date inputs.
|
- Exact date inputs are native browser date inputs.
|
||||||
- FamilySearch IDs are normalized and validated by `PeopleService`.
|
- FamilySearch IDs are normalized and validated by `PeopleService`.
|
||||||
- Portrait uploads are stored under the configured upload root in a Person-specific directory and update Portrait path.
|
- Tags use the shared Tag registry and support inline add/select behavior.
|
||||||
|
- Photos are managed from Person Detail via `/people/{person_id}/photos` (not in create/edit form fields).
|
||||||
- Metadata JSON remains hidden.
|
- Metadata JSON remains hidden.
|
||||||
- Save success returns to Person Detail.
|
- Save success returns to Person Detail.
|
||||||
|
|
||||||
## Detail Behavior
|
## Detail Behavior
|
||||||
|
|
||||||
- The header provides **New Document**, **Edit Person**, and **Delete**.
|
- The header provides **New Document**, **Edit Person**, **Edit Photo(s)**, and **Delete**.
|
||||||
|
- The header includes a contextual back action: **Back to People** by default, and **Back to Document** when opened from Document Detail.
|
||||||
- **New Document** opens Document creation with this Person requested for author preselection.
|
- **New Document** opens Document creation with this Person requested for author preselection.
|
||||||
- The portrait viewer resolves supported relative upload paths and absolute HTTP/data URLs.
|
- Person Detail shows a single-photo viewer with **Previous/Next** navigation; the page-level **Edit Photo(s)** header action opens photo management.
|
||||||
- Biographical Record shows names, compact birth/death dates, places, and an **Open in FamilySearch** link when an ID exists.
|
- Photo management (upload, description edit, set-primary, delete) is intentionally moved to `/people/{person_id}/photos`.
|
||||||
|
- Biographical Record shows split names, computed full name, tags, compact birth/death dates, and places.
|
||||||
|
- Birth and death place values are clickable links to Google Maps when present.
|
||||||
|
- FamilySearch ID is shown as a metadata value and is clickable to the FamilySearch person details route when present.
|
||||||
- Biography has an explicit empty value.
|
- Biography has an explicit empty value.
|
||||||
- Linked Documents show Document name, relationship role, and an action to open Document Detail.
|
- Linked Documents render as a table with **Document Name**, **Document Date**, **Role**, and **Number of Pages**; selecting a row opens Document Detail.
|
||||||
- No links shows both an empty state and guidance to link from a Document workflow.
|
- No links shows both an empty state and guidance to link from a Document workflow.
|
||||||
- System Logistics shows created and updated timestamps.
|
- System Logistics shows created and updated timestamps.
|
||||||
|
|
||||||
## Delete Behavior
|
## Delete Behavior
|
||||||
|
|
||||||
- The page warns when linked Document relationships exist.
|
- The page warns when linked Document relationships exist.
|
||||||
|
- Delete is blocked when related Photos exist.
|
||||||
- Confirmed deletion removes the Person and its relationship links; it does not delete Documents.
|
- Confirmed deletion removes the Person and its relationship links; it does not delete Documents.
|
||||||
- Success returns to the People list.
|
- Success returns to the People list.
|
||||||
- Missing or already-deleted records return to a safe list state.
|
- Missing or already-deleted records return to a safe list state.
|
||||||
|
|
||||||
|
## Photo Gallery Behavior (`/people/{person_id}/photos`)
|
||||||
|
|
||||||
|
- Upload is triggered from a header-level **Upload Photo(s)** control beside **Back to Person**.
|
||||||
|
- The gallery renders all photos in a responsive grid (3-4 tiles wide on larger screens).
|
||||||
|
- Description text is shown as an overlay at the bottom of each image for quick context.
|
||||||
|
- The editor provides **Save Description**, **Set Primary** (when applicable), and **Delete Photo** actions.
|
||||||
|
|
||||||
## Acceptance Checklist
|
## Acceptance Checklist
|
||||||
|
|
||||||
- List fields, alignment, date fallback, search, sorting, and navigation match this contract.
|
- List fields, alignment, date fallback, search, sorting, and navigation match this contract.
|
||||||
- Full name is enforced on create and edit.
|
- Last name and first/middle names are enforced on create and edit.
|
||||||
- FamilySearch ID validation and link generation use the fixed supported identifier format.
|
- FamilySearch ID validation and link generation use the fixed supported identifier format.
|
||||||
- Portrait upload and rendering remain constrained to supported media paths.
|
- Photo upload and rendering remain constrained to supported media paths.
|
||||||
- New Document carries the Person context.
|
- New Document carries the Person context.
|
||||||
- Linked Documents show the correct role and target.
|
- Linked Documents show the correct role and target.
|
||||||
- Delete wording distinguishes removal of relationship links from deletion of Documents.
|
- Delete wording distinguishes removal of relationship links from deletion of Documents.
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
# Settings Page Contract
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
Settings manages installation-local registries, safe runtime .env settings, and editable text assets from one route.
|
||||||
|
|
||||||
|
## Route
|
||||||
|
|
||||||
|
| Route | Purpose |
|
||||||
|
| --- | --- |
|
||||||
|
| `/settings` | Manage Runtime Settings, Document Types, Person Roles, Tags, Prompts, Home Page Text, and Maintenance runs. |
|
||||||
|
|
||||||
|
## Behavior
|
||||||
|
|
||||||
|
- The page title is **Settings**.
|
||||||
|
- Configuration surfaces are grouped as tabs:
|
||||||
|
- **Document Types**
|
||||||
|
- **Person Roles**
|
||||||
|
- **Tags**
|
||||||
|
- **Prompts**
|
||||||
|
- **Home Page Text**
|
||||||
|
- **Maintenance**
|
||||||
|
- **Runtime Settings**
|
||||||
|
- Runtime Settings exposes an allowlisted set of non-secret fields synchronized with `Settings` model fields except excluded secret/unsafe fields.
|
||||||
|
- Runtime Settings is rendered as a compact two-column editor (**Setting**, **Value**) in a centered, narrower responsive container.
|
||||||
|
- Runtime Settings persists changes to the resolved runtime env file, validates by constructing a `Settings` instance, and reports validation failures through the shared UI error presenter.
|
||||||
|
- `Settings` resolves its env file in this order: explicit `_env_file`, `ENV_FILE`, then the repository-root `.env.production`.
|
||||||
|
- Runtime Settings resolves its write target in this order: explicit function override (tests/tools), `RUNTIME_SETTINGS_ENV_FILE` environment variable (deployment override), `ENV_FILE`, then the repository-root `.env.production`.
|
||||||
|
- Runtime Settings changes require application restart to take effect.
|
||||||
|
- Runtime Settings renders a host-side restart command (`docker compose -f docker-compose.production.yml up -d --force-recreate app worker`) so operators can apply saved values without granting Docker control to the app container.
|
||||||
|
- Runtime Settings includes an explicit "Other settings not shown here" markdown table listing:
|
||||||
|
- secrets (`OPENROUTER_API_KEY`, `DATABASE__PASSWORD`)
|
||||||
|
- high-risk database connection settings (`DATABASE__DRIVER`, `DATABASE__PATH`, `DATABASE__HOST`, `DATABASE__PORT`, `DATABASE__DATABASE`, `DATABASE__USER`)
|
||||||
|
and deployment/helper keys (`CLOUDFLARE_TUNNEL_TOKEN`, `BACKUP_DIR`, `BACKUP_RETENTION_DAYS`, `RUNTIME_SETTINGS_ENV_FILE`, `ENV_FILE`, `COMPOSE_FILE`) plus legacy/deprecated keys (`POSTGRES_*`, `DATABASE_BACKUP_DIR`, `APP_DATA_BACKUP_DIR`, `UPLOADS_BACKUP_DIR`, `SYNOLOGY_BACKUP_DIR`), and directs edits for those keys to the resolved runtime env file path.
|
||||||
|
- Document Types, Person Roles, and Tags support Add/Edit/Delete with existing guardrails.
|
||||||
|
- Prompts exposes only `transcribe_document.md` for editing and restore-from-backup.
|
||||||
|
- Home Page Text edits the same Markdown content rendered on `/homepage`.
|
||||||
|
- Maintenance provides queue-backed **Run Backup** and **Run Storage Reconciliation** actions.
|
||||||
|
- Maintenance also provides GEDCOM upload and **Run GEDCOM Import** actions, using the same queue-backed `MaintenanceRun` history/log flow.
|
||||||
|
- Maintenance run history shows job type, status, started/finished timestamps, duration, summary, and log view/download actions.
|
||||||
|
- Maintenance actions enqueue work and signal the worker; the page itself does not execute shell commands directly.
|
||||||
|
|
||||||
|
## Acceptance Checklist
|
||||||
|
|
||||||
|
- `/ui/settings` renders all seven tabs.
|
||||||
|
- Registry and prompt workflows keep existing validation and error handling.
|
||||||
|
- Runtime Settings excludes secret fields and rejects invalid values.
|
||||||
|
- Saving Home Page Text persists content for the homepage view.
|
||||||
|
|
||||||
|
## Implementation Anchors
|
||||||
|
|
||||||
|
- `src/transcription/ui/pages/settings_page.py`
|
||||||
|
- `src/transcription/ui/runtime_settings_store.py`
|
||||||
|
- `src/transcription/ui/homepage_store.py`
|
||||||
|
- `tests/ui/test_pages_registration.py`
|
||||||
@@ -8,7 +8,7 @@ Sources manages individual archived page/file records. It provides source-media
|
|||||||
|
|
||||||
| Route | Purpose |
|
| Route | Purpose |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| `/sources` | Global or filtered Source list. |
|
| `/sources` | Document-filtered or Job-filtered Source list; global route redirects to Documents. |
|
||||||
| `/sources/{source_id}` | View media, transcription, revision, metadata, and evidence. |
|
| `/sources/{source_id}` | View media, transcription, revision, metadata, and evidence. |
|
||||||
| `/sources/{source_id}/delete` | Confirm or block deletion. |
|
| `/sources/{source_id}/delete` | Confirm or block deletion. |
|
||||||
|
|
||||||
@@ -16,12 +16,13 @@ The list accepts optional `document_id` and `job_id` query parameters. Document
|
|||||||
|
|
||||||
## List Behavior
|
## List Behavior
|
||||||
|
|
||||||
- The title is **Source Asset Records**, **Sources for Document**, or **Sources for Job** according to context.
|
- The global `/sources` route redirects to `/documents`.
|
||||||
- Global context provides **Create Job**.
|
- Filtered list titles are **Sources for Document** and **Sources for Job**.
|
||||||
- Filtered context provides **Back to Document** or **Back to Job**.
|
- Filtered context provides **Back to Document** or **Back to Job**.
|
||||||
- Rows are ordered by page number and then upload name.
|
- Rows are ordered by page number and then upload name.
|
||||||
- Columns are Document Name, Page Number, Upload Title, Status, and Error Detail.
|
- Columns are Upload Title, Page Number, Document Name, Status, and Error Detail.
|
||||||
- Document Name, Upload Title, and Error Detail are left-aligned; Status is centered.
|
- Document Name, Upload Title, and Error Detail are left-aligned; Status is centered.
|
||||||
|
- Status labels are presented in uppercase for consistency with Jobs.
|
||||||
- Stored Filename is intentionally absent from the list.
|
- Stored Filename is intentionally absent from the list.
|
||||||
- Selecting a row opens Source Detail.
|
- Selecting a row opens Source Detail.
|
||||||
- No records displays `No source asset records found in repository.`
|
- No records displays `No source asset records found in repository.`
|
||||||
@@ -29,15 +30,22 @@ The list accepts optional `document_id` and `job_id` query parameters. Document
|
|||||||
## Detail Behavior
|
## Detail Behavior
|
||||||
|
|
||||||
- The heading shows page number, upload name, and Source ID.
|
- The heading shows page number, upload name, and Source ID.
|
||||||
- **Back to Sources** returns to the global list.
|
- **Back to Document** returns to Document Detail for the active source page.
|
||||||
|
- **Retranscribe Source** opens Create Processing Job with this Source and its Document locked.
|
||||||
- **Delete Source** opens the guarded delete route.
|
- **Delete Source** opens the guarded delete route.
|
||||||
- Previous and Next navigate only among Sources belonging to the same Document in page order; unavailable boundary actions are disabled.
|
- Previous and Next navigate only among Sources belonging to the same Document in page order; unavailable boundary actions are disabled.
|
||||||
- The media viewer resolves the stored Source path through the configured upload root.
|
- The media viewer resolves the stored Source path through the configured upload root.
|
||||||
- Transcription Text is read-only and prefers the latest JobSource transcription, then the Source projection.
|
- The top layout is adaptive:
|
||||||
- Editable Revision is seeded from an existing revision or the machine transcription.
|
- Standard pages use three columns with a wider Editable Revision column than the image column.
|
||||||
|
- Wide+narrow landscape images switch to a stacked left layout (image above Editable Revision) with metadata on the right.
|
||||||
|
- Editable Revision is seeded from an existing revision or the preferred machine transcription.
|
||||||
- Source Metadata shows upload name, stored filename, page number, Document Name, Document ID, and stored path. Source ID appears in the page-header subtitle.
|
- Source Metadata shows upload name, stored filename, page number, Document Name, Document ID, and stored path. Source ID appears in the page-header subtitle.
|
||||||
- SourceJob Metadata shows latest status, Job ID, execution time, provider, model, prompt, and failure detail.
|
- SourceJob Metadata shows latest status (uppercase display), Job ID, execution time, provider, model, prompt, and failure detail.
|
||||||
- Revision Logistics shows revised state, last-revised time, and upload time.
|
- Revision Logistics shows revised state, last-revised time, and upload time.
|
||||||
|
- Candidate Machine Transcriptions appears below the image/revision area, remains compact until expanded, then compares it with the preferred
|
||||||
|
machine result and requires confirmation before **Use this transcription**.
|
||||||
|
- Candidate promotion does not alter a human revision. Empty states distinguish no machine result from no candidates.
|
||||||
|
- An orientation-normalized artifact appears in evidence only when recognized metadata required a physical rotation.
|
||||||
|
|
||||||
## Provider Evidence
|
## Provider Evidence
|
||||||
|
|
||||||
@@ -47,6 +55,7 @@ The list accepts optional `document_id` and `job_id` query parameters. Document
|
|||||||
- Historical `raw_api_response` values are labeled as OpenRouter SDK response snapshots.
|
- Historical `raw_api_response` values are labeled as OpenRouter SDK response snapshots.
|
||||||
- Missing evidence has an explicit empty state.
|
- Missing evidence has an explicit empty state.
|
||||||
- Historical executions explicitly state that exact transport evidence was not captured.
|
- Historical executions explicitly state that exact transport evidence was not captured.
|
||||||
|
- Quality warning artifacts remain attached to their machine attempt and are not recomputed during page rendering.
|
||||||
- **Export Evidence** downloads a versioned package containing source identity, attempts, artifacts, relationships,
|
- **Export Evidence** downloads a versioned package containing source identity, attempts, artifacts, relationships,
|
||||||
schema versions, and integrity digests without source binaries, credentials, or machine-local source paths.
|
schema versions, and integrity digests without source binaries, credentials, or machine-local source paths.
|
||||||
|
|
||||||
@@ -71,6 +80,7 @@ The list accepts optional `document_id` and `job_id` query parameters. Document
|
|||||||
- List columns and alignments match this contract and omit Stored Filename.
|
- List columns and alignments match this contract and omit Stored Filename.
|
||||||
- Previous/next navigation never crosses Document boundaries.
|
- Previous/next navigation never crosses Document boundaries.
|
||||||
- Detail keeps machine output read-only and human revision separately editable.
|
- Detail keeps machine output read-only and human revision separately editable.
|
||||||
|
- Retranscription, candidate comparison, warnings, and explicit promotion preserve every prior attempt.
|
||||||
- Empty, failed, and missing-evidence states remain explicit.
|
- Empty, failed, and missing-evidence states remain explicit.
|
||||||
- JSON evidence is readable without being mislabeled as native transport evidence.
|
- JSON evidence is readable without being mislabeled as native transport evidence.
|
||||||
- Delete cannot remove a Source with processing-history links.
|
- Delete cannot remove a Source with processing-history links.
|
||||||
@@ -86,4 +96,4 @@ The list accepts optional `document_id` and `job_id` query parameters. Document
|
|||||||
|
|
||||||
## Planned Changes
|
## Planned Changes
|
||||||
|
|
||||||
- Source page reordering is deferred beyond V4.3 and may be reconsidered if a demonstrated workflow need emerges.
|
- Source page reordering remains deferred unless a demonstrated workflow need emerges.
|
||||||
|
|||||||
@@ -1,146 +0,0 @@
|
|||||||
# Implementation Plan (Version 4.1)
|
|
||||||
|
|
||||||
## Goal
|
|
||||||
|
|
||||||
Deliver the V4.1 usability revision as a small, behavior-safe increment over the V4 baseline.
|
|
||||||
|
|
||||||
## Implementation Principles
|
|
||||||
|
|
||||||
- Keep presentation formatting in UI components and route orchestration in pages.
|
|
||||||
- Keep persistence and cross-record queries behind service boundaries.
|
|
||||||
- Reuse shared table and date-label helpers instead of duplicating fallback logic.
|
|
||||||
- Make the FamilySearch schema change additive and nullable.
|
|
||||||
- Add focused tests for changed behavior before broad regression verification.
|
|
||||||
|
|
||||||
## Current Project Impact
|
|
||||||
|
|
||||||
| Area | Expected impact |
|
|
||||||
| --- | --- |
|
|
||||||
| Persistence | Add nullable `Person.family_search_id`; provide the repository's supported schema-upgrade path for existing databases. |
|
|
||||||
| People service | Normalize and validate FamilySearch IDs at the domain/service boundary if model validation does not fully cover writes. |
|
|
||||||
| Documents UI | Add table data, improve relationship labels/links, compact date display, and combine processing navigation. |
|
|
||||||
| People UI | Add table date fields, Person-first Document creation, compact date display, and FamilySearch controls. |
|
|
||||||
| Sources service/UI | Query adjacent document Sources and add bounded navigation; revise list columns and wrapping. |
|
|
||||||
| Jobs UI | Refresh the active detail read model on a timer until terminal status. |
|
|
||||||
| Shared UI | Add reusable constrained/wrapped table presentation and compact date formatting where appropriate. |
|
|
||||||
| Tests | Update model/service and UI coverage for all affected workflows. |
|
|
||||||
|
|
||||||
## Implementation Phases
|
|
||||||
|
|
||||||
### 1. Add Shared Presentation Rules
|
|
||||||
|
|
||||||
- Review `ui/components/table/common.py` and packaged theme CSS for the narrowest reusable table-width solution.
|
|
||||||
- Add reusable styles or column slots for constrained, wrapping, left-aligned text.
|
|
||||||
- Add a shared formatter for exact/approximate/unknown dates if it can be reused without coupling components to persistence.
|
|
||||||
- Preserve sorting and search behavior for rendered display values.
|
|
||||||
|
|
||||||
### 2. Update Archival List Tables
|
|
||||||
|
|
||||||
- Extend the Document table read model with author names and the compact document date.
|
|
||||||
- Build author display from eagerly loaded document-person links using the `author` role.
|
|
||||||
- Apply title/type alignment and constrained title wrapping.
|
|
||||||
- Extend the Person table read model with compact birth and death date values.
|
|
||||||
- Apply Display Name and Maiden Name alignment.
|
|
||||||
- Remove Stored Filename from the Source table read model only if no other list behavior consumes it; always remove its rendered column.
|
|
||||||
- Constrain and left-align the requested Source columns.
|
|
||||||
- Add or update UI component tests for serialized rows, columns, and fallback formatting.
|
|
||||||
|
|
||||||
### 3. Improve Document Relationship Workflows
|
|
||||||
|
|
||||||
- Introduce one person-label formatter that combines preferred Display Name, Full Name context, and known birth year without implying uniqueness.
|
|
||||||
- Use Person UUIDs as selector values.
|
|
||||||
- Apply the formatter to every relationship role selector.
|
|
||||||
- Change Related People rows into actions that navigate to `/people/{person_id}`.
|
|
||||||
- Replace separate exact/approximate rows in view mode with one conditional Document Date row.
|
|
||||||
- Combine Pipeline Jobs and Sources into one related-processing card beneath Related People.
|
|
||||||
- Preserve existing job/source counts and navigation actions.
|
|
||||||
|
|
||||||
### 4. Add the Person-First Document Workflow
|
|
||||||
|
|
||||||
- Add a New Document action on Person Detail.
|
|
||||||
- Pass the Person UUID through a narrowly defined query parameter to `/documents/new`.
|
|
||||||
- Validate the requested UUID against the loaded people list.
|
|
||||||
- Preselect that person in the intended default relationship role. Use `author` unless a different role is explicitly encoded later.
|
|
||||||
- Ignore invalid or unavailable preselection values with the application's normal visible error/notification behavior.
|
|
||||||
- Confirm ordinary `/documents/new` behavior remains unchanged.
|
|
||||||
|
|
||||||
### 5. Add FamilySearch Person References
|
|
||||||
|
|
||||||
- Add nullable, unique `family_search_id` to the `Person` model and schema.
|
|
||||||
- Implement a non-destructive upgrade for existing SQLite and PostgreSQL databases using the repository's established schema-management approach.
|
|
||||||
- Normalize values by trimming and uppercasing.
|
|
||||||
- Validate the `XXXX-XXX` alphanumeric identifier shape and return a clear validation error for malformed input.
|
|
||||||
- Report duplicate identifiers as a deterministic conflict rather than a generic persistence failure.
|
|
||||||
- Add the field to Person create/edit forms and preserve it during updates.
|
|
||||||
- Add a URL builder that safely inserts only a validated identifier into the fixed FamilySearch details URL.
|
|
||||||
- Render a FamilySearch action on Person Detail only when an identifier is present.
|
|
||||||
- Add persistence, normalization, validation, form, and link-generation tests.
|
|
||||||
|
|
||||||
### 6. Add Source Page Navigation
|
|
||||||
|
|
||||||
- Add a Sources service query that returns previous/current/next context for a Source within its Document.
|
|
||||||
- Define ordering by `page_number`, with a stable secondary key such as Source UUID for defensive determinism.
|
|
||||||
- Keep navigation bounded to the current `document_id`.
|
|
||||||
- Render previous and next actions adjacent to the source viewer or detail header.
|
|
||||||
- Disable or omit unavailable boundary actions.
|
|
||||||
- Test first, middle, last, single-page, and cross-document cases.
|
|
||||||
|
|
||||||
### 7. Add Job Detail Auto-Refresh
|
|
||||||
|
|
||||||
- Make Job Detail content refreshable without rebuilding unrelated global navigation.
|
|
||||||
- Start a NiceGUI timer only for queued or processing jobs.
|
|
||||||
- On each tick, re-read the Job through `JobService` and refresh the detail content.
|
|
||||||
- Use a 4-second default interval.
|
|
||||||
- Stop or deactivate the timer when status becomes completed, partial success, failed, or cancelled, according to the model's actual terminal states.
|
|
||||||
- Prevent overlapping refresh callbacks.
|
|
||||||
- Retain existing error presentation if a refresh read fails.
|
|
||||||
- Add UI tests for timer creation, refresh, and terminal-state stopping.
|
|
||||||
|
|
||||||
### 8. Simplify View-Mode Date Rows
|
|
||||||
|
|
||||||
- On Document Detail, show exact date, else approximate date, else one not-set value.
|
|
||||||
- On Person Detail, apply the same independent rule to birth and death.
|
|
||||||
- Do not hide either input in create/edit mode.
|
|
||||||
- Test each exact, approximate, and absent state.
|
|
||||||
|
|
||||||
### 9. Verification and Documentation Alignment
|
|
||||||
|
|
||||||
- Run the focused model/service/UI tests covering changed surfaces.
|
|
||||||
- Run the existing regression suite appropriate to persistence and UI changes.
|
|
||||||
- Confirm SQLite and PostgreSQL model compatibility at the schema-definition level.
|
|
||||||
- Update V4.1 documentation if implementation reveals a necessary boundary change; do not silently expand scope.
|
|
||||||
|
|
||||||
## Recommended Delivery Order
|
|
||||||
|
|
||||||
1. Shared formatters and table presentation.
|
|
||||||
2. Additive Person schema change and FamilySearch validation.
|
|
||||||
3. Document and Person list/detail changes.
|
|
||||||
4. Person-first Document workflow.
|
|
||||||
5. Source navigation.
|
|
||||||
6. Job polling.
|
|
||||||
7. Focused and regression verification.
|
|
||||||
|
|
||||||
## Done When
|
|
||||||
|
|
||||||
- Every V4.1 acceptance criterion is demonstrated or covered by a focused test.
|
|
||||||
- Existing Person rows remain valid after the nullable schema addition.
|
|
||||||
- Duplicate FamilySearch references cannot be assigned to multiple local Person records.
|
|
||||||
- FamilySearch links are generated only from normalized, validated IDs.
|
|
||||||
- Auto-refresh performs no polling after a terminal job state.
|
|
||||||
- Adjacent Source navigation never crosses Document boundaries.
|
|
||||||
- The existing V4 workflows remain operational.
|
|
||||||
|
|
||||||
## Out of Scope
|
|
||||||
|
|
||||||
- Page reordering.
|
|
||||||
- Settings management.
|
|
||||||
- External genealogy API integration.
|
|
||||||
- Raw `.env` editing.
|
|
||||||
- Theme editing.
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [V4.1 Scope Boundary](scope_boundary_v4_1.md)
|
|
||||||
- [V4 Implementation Plan](../ver4/implementation_plan_v4.md)
|
|
||||||
- [V4 Requirements](../ver4/requirements_v4.md)
|
|
||||||
- [V4 Error Handling Policy](../ver4/error_handling_v4.md)
|
|
||||||
@@ -1,142 +0,0 @@
|
|||||||
# V4.1 Scope Boundary
|
|
||||||
|
|
||||||
This document defines the scope of the first incremental revision to Version 4. V4 remains the product and architecture baseline; V4.1 adds focused usability improvements and one additive Person field.
|
|
||||||
|
|
||||||
## Purpose
|
|
||||||
|
|
||||||
- Improve common archival record workflows without redesigning the application.
|
|
||||||
- Resolve table overflow, ambiguous person selection, and unnecessary navigation.
|
|
||||||
- Add a manually maintained FamilySearch person reference without introducing external API integration.
|
|
||||||
|
|
||||||
## In Scope
|
|
||||||
|
|
||||||
### 1. Archival Documents List
|
|
||||||
|
|
||||||
- Keep every table column within the available page width.
|
|
||||||
- Limit and wrap long Document Title values.
|
|
||||||
- Left-align Document Title and Type.
|
|
||||||
- Add Author and Document Date columns.
|
|
||||||
- Display all people linked through the `author` role in the Author column.
|
|
||||||
- Display exact document date when present, otherwise approximate date when present, otherwise `Unknown`.
|
|
||||||
|
|
||||||
### 2. Document Detail and Editing
|
|
||||||
|
|
||||||
- Use an unambiguous label in person selectors. The label should prefer Display Name, retain Full Name for context, and include the birth year when known.
|
|
||||||
- Do not require Display Name to be unique.
|
|
||||||
- Link each Related People entry to its Person Detail page.
|
|
||||||
- In view mode, display only the populated exact or approximate document date row. Display a single unknown/not-set state when neither exists.
|
|
||||||
- Keep both exact and approximate inputs available in create/edit mode.
|
|
||||||
- Move source navigation out from beneath the media viewer.
|
|
||||||
- Present Pipeline Jobs and Sources together in one related-processing card with counts and actions.
|
|
||||||
|
|
||||||
### 3. People List
|
|
||||||
|
|
||||||
- Left-align Display Name and Maiden Name.
|
|
||||||
- Display exact birth date when present, otherwise approximate birth date when present, otherwise `Unknown`.
|
|
||||||
- Add a Death Date column with the same fallback rule.
|
|
||||||
|
|
||||||
### 4. Person Detail and Editing
|
|
||||||
|
|
||||||
- Add a New Document action that opens Document creation with the current person preselected.
|
|
||||||
- Preserve the existing Document-first workflow.
|
|
||||||
- In view mode, display only the populated exact or approximate row for each of birth and death date. Display a single unknown/not-set state when neither value exists.
|
|
||||||
- Keep both exact and approximate inputs available in create/edit mode.
|
|
||||||
|
|
||||||
### 5. FamilySearch Reference
|
|
||||||
|
|
||||||
- Add a nullable, unique `family_search_id` field to `Person`.
|
|
||||||
- Allow the field to be entered and changed in Person create/edit flows.
|
|
||||||
- Trim whitespace, normalize the identifier to uppercase, and validate it against the supported
|
|
||||||
`XXXX-XXX` alphanumeric shape before persistence.
|
|
||||||
- When an identifier exists, show a FamilySearch action on Person Detail linking to:
|
|
||||||
`https://www.familysearch.org/tree/person/details/{family_search_id}`
|
|
||||||
- Construct the URL in application code; do not persist the full URL.
|
|
||||||
|
|
||||||
### 6. Source List and Detail
|
|
||||||
|
|
||||||
- Keep every Source Asset Records table column within the available page width.
|
|
||||||
- Limit and wrap long Document Name, Upload Title, and Error Detail values.
|
|
||||||
- Left-align Document Name, Upload Title, and Error Detail.
|
|
||||||
- Remove Stored Filename only from the Source Asset Records table. Continue storing it and showing it on Source Detail.
|
|
||||||
- On Source Detail, add previous and next navigation for Sources belonging to the same Document, ordered by `page_number`.
|
|
||||||
- Disable or omit the previous/next action at the first/last page.
|
|
||||||
|
|
||||||
### 7. Job Detail
|
|
||||||
|
|
||||||
- Automatically refresh Job Detail while the job is in a non-terminal state.
|
|
||||||
- Use a modest interval in the 3-5 second range.
|
|
||||||
- Stop polling when the job reaches a terminal state or the page is no longer active.
|
|
||||||
- Preserve manual navigation and existing job actions.
|
|
||||||
|
|
||||||
### 8. Homepage Storage Decision
|
|
||||||
|
|
||||||
- Continue treating homepage markdown and images as mutable application data, not prompt artifacts or packaged source assets.
|
|
||||||
- Keep homepage content separate from `prompts`.
|
|
||||||
- Defer relocation to a configurable application-data root unless the existing location prevents normal installed or deployed operation.
|
|
||||||
|
|
||||||
## Out of Scope
|
|
||||||
|
|
||||||
- Source page renumbering or reordering.
|
|
||||||
- A Settings page.
|
|
||||||
- Editing `.env` or secrets through the UI.
|
|
||||||
- Runtime theme editing.
|
|
||||||
- FamilySearch authentication, API calls, search, import, synchronization, or conflict resolution.
|
|
||||||
- Ancestry references or other genealogy providers.
|
|
||||||
- Google Maps links from place fields.
|
|
||||||
- Enforcing unique Display Name values.
|
|
||||||
- Changes to transcription execution or provider behavior.
|
|
||||||
- Changes to the V4 API solely to expose the V4.1 presentation enhancements.
|
|
||||||
|
|
||||||
## Locked Design Decisions
|
|
||||||
|
|
||||||
### A. Person Selector Identity
|
|
||||||
|
|
||||||
- Selection values remain internal Person UUIDs.
|
|
||||||
- Display labels provide disambiguating context but are not identity keys.
|
|
||||||
- Duplicate Full Name and Display Name values remain valid.
|
|
||||||
|
|
||||||
### B. Date Presentation
|
|
||||||
|
|
||||||
- Exact dates take precedence over approximate/raw dates for compact list and view presentation.
|
|
||||||
- Create/edit forms retain both fields so either representation can be maintained.
|
|
||||||
- V4.1 does not introduce a new mutual-exclusion database constraint.
|
|
||||||
|
|
||||||
### C. FamilySearch Storage
|
|
||||||
|
|
||||||
- Store only the FamilySearch person identifier.
|
|
||||||
- Treat a FamilySearch person identifier as unique across local Person records.
|
|
||||||
- Use one dedicated nullable Person field while FamilySearch is the only supported external genealogy reference.
|
|
||||||
- Reconsider a generic external-reference model only when a second provider or multiple references per person are required.
|
|
||||||
|
|
||||||
### D. Stored Filename
|
|
||||||
|
|
||||||
- Stored Filename remains part of the Source model and Source Detail diagnostics.
|
|
||||||
- Only the list-table column is removed.
|
|
||||||
|
|
||||||
## Data and Compatibility Policy
|
|
||||||
|
|
||||||
- The `family_search_id` addition must be nullable and non-destructive for existing Person rows.
|
|
||||||
- Existing records, routes, relationships, jobs, Sources, prompt provenance, and uploaded media remain valid.
|
|
||||||
- UI changes must preserve both Document-first and Person-first workflows.
|
|
||||||
- V4.1 must remain portable across SQLite and PostgreSQL.
|
|
||||||
|
|
||||||
## Acceptance Criteria
|
|
||||||
|
|
||||||
1. Document, Person, and Source tables fit their page containers at supported desktop widths without losing requested columns.
|
|
||||||
2. Long table text wraps or is constrained without forcing important columns outside the table container.
|
|
||||||
3. Duplicate-named people can be distinguished in every document relationship selector.
|
|
||||||
4. Related People entries navigate to the correct Person Detail page.
|
|
||||||
5. Compact date displays consistently use exact, then approximate, then unknown fallback behavior.
|
|
||||||
6. Starting from Person Detail can create a Document with that person preselected without breaking normal Document creation.
|
|
||||||
7. A valid FamilySearch ID is persisted and produces the correct Person Detail hyperlink; absent IDs produce no action.
|
|
||||||
8. Source previous/next actions remain within the same Document and follow `page_number`.
|
|
||||||
9. Active Job Detail pages update without manual refresh and stop polling after terminal status.
|
|
||||||
10. Stored Filename is absent from the Source list table but remains available on Source Detail.
|
|
||||||
11. Focused automated tests pass and unaffected V4 behavior remains intact.
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [V4.1 Implementation Plan](implementation_plan_v4_1.md)
|
|
||||||
- [V4 Scope Boundary](../ver4/scope_boundary_v4.md)
|
|
||||||
- [V4 Architecture](../ver4/architecture_v4.md)
|
|
||||||
- [V4 Schema](../ver4/schema_v4.md)
|
|
||||||
@@ -1,258 +0,0 @@
|
|||||||
# Implementation Plan (Version 4.2)
|
|
||||||
|
|
||||||
## Goal
|
|
||||||
|
|
||||||
Make processing evidence precise, append-only, secret-safe, and exportable while preserving every existing record and creating a provider-neutral home for future OCR/layout artifacts.
|
|
||||||
|
|
||||||
## Implementation Principles
|
|
||||||
|
|
||||||
- Implement the [Digital Evidence and AI Processing Provenance invariant](../invariant/ai_evidence_and_provenance.md), not a provider-specific approximation of it.
|
|
||||||
- Capture transport evidence before SDK parsing.
|
|
||||||
- Keep exact evidence separate from parsed and normalized representations.
|
|
||||||
- Prefer additive schema evolution and explicit compatibility behavior.
|
|
||||||
- Reference source content by digest rather than duplicating it in request JSON.
|
|
||||||
- Use allowlists for safe metadata capture.
|
|
||||||
- Keep persistence and evidence semantics behind service boundaries.
|
|
||||||
- Do not change the default model until a representative benchmark supports that decision.
|
|
||||||
|
|
||||||
## Current-State Gaps
|
|
||||||
|
|
||||||
| Current behavior | Gap to close |
|
|
||||||
| --- | --- |
|
|
||||||
| `Source` stores original file path, digest, and size. | Media type and image/page geometry used for an execution are not frozen with that execution. |
|
|
||||||
| `Job` stores prompt text/hash, requested model after resolution, temperature, and `top_p`. | The complete effective request structure, omitted-versus-explicit parameter state, routing constraints, and software versions are not frozen. |
|
|
||||||
| `JobSource.raw_api_response` stores `model_dump()` output from the OpenRouter SDK. | The exact HTTP body can be normalized by OpenRouter and filtered again by the SDK before persistence. |
|
|
||||||
| `JobSource.ai_metadata` stores finish reason and basic token counts. | Detailed accounting remains only in the SDK snapshot and is not a substitute for exact evidence. |
|
|
||||||
| Provider exceptions become application errors. | Safe HTTP error bodies, statuses, headers, and no-response distinctions are not persisted. |
|
|
||||||
| Worker logs elapsed time. | Execution duration is not stored on `JobSource`. |
|
|
||||||
| Source Detail displays AI metadata and the SDK snapshot. | The UI does not identify evidence layers or expose request/transport/software provenance. |
|
|
||||||
| No generic processing-artifact model exists. | Future OCR geometry would require ad hoc provider fields or an unrelated schema. |
|
|
||||||
|
|
||||||
## Expected Project Impact
|
|
||||||
|
|
||||||
| Area | Expected impact |
|
|
||||||
| --- | --- |
|
|
||||||
| Database models and upgrades | Add execution-specification, transport-evidence, timing, software-context, and generic artifact storage without removing existing columns. |
|
|
||||||
| OpenRouter adapter | Introduce a transport boundary that can capture exact body/status/safe headers before typed SDK parsing, or use supported SDK hooks that expose the unparsed response reliably. |
|
|
||||||
| Provider contract | Return structured evidence for success and failure without leaking provider-specific transport concerns into workflow orchestration. |
|
|
||||||
| Source and workflow services | Persist one append-only execution outcome and its artifacts transactionally; retain compatibility projections. |
|
|
||||||
| UI | Label and inspect evidence layers; export safe evidence packages through service operations. |
|
|
||||||
| Benchmarking | Add a private manifest and repeatable evaluator using the literal-transcription methodology. |
|
|
||||||
| Tests and documentation | Add compatibility, capture, security, integrity, export, and benchmark-scoring coverage; correct overstated V4 evidence language. |
|
|
||||||
|
|
||||||
## Proposed Data Design
|
|
||||||
|
|
||||||
Exact names should be confirmed against existing conventions before migration code is written. The design should provide the following logical records.
|
|
||||||
|
|
||||||
### 1. Execution Evidence
|
|
||||||
|
|
||||||
Extend `JobSource` or associate it one-to-one with a new execution-evidence record containing:
|
|
||||||
|
|
||||||
- Request manifest JSON and manifest schema version.
|
|
||||||
- Transport status, body bytes or exact decoded body plus encoding/content type, and safe headers.
|
|
||||||
- Parsed SDK snapshot retained separately from transport content.
|
|
||||||
- Application, adapter, SDK, and runtime version metadata.
|
|
||||||
- Start, finish, and duration values.
|
|
||||||
- Router/provider request and generation identifiers when available.
|
|
||||||
- Failure phase and whether an HTTP response was received.
|
|
||||||
|
|
||||||
The implementation should evaluate a companion table rather than continuing to widen `JobSource`. A companion record better isolates large/optional evidence and permits clear one-to-one compatibility semantics.
|
|
||||||
|
|
||||||
### 2. Generic Processing Artifact
|
|
||||||
|
|
||||||
Add a one-to-many artifact model associated with a source and, when applicable, a producing execution:
|
|
||||||
|
|
||||||
- Stable artifact UUID.
|
|
||||||
- `source_id` and optional execution/`job_source_id`.
|
|
||||||
- Semantic artifact type.
|
|
||||||
- Media/serialization format.
|
|
||||||
- Schema name and version.
|
|
||||||
- Producer and producer version.
|
|
||||||
- Inline JSON payload or external location.
|
|
||||||
- Payload digest and byte size.
|
|
||||||
- Coordinate-system metadata when relevant.
|
|
||||||
- Creation timestamp.
|
|
||||||
|
|
||||||
Enforce exactly one content location: inline payload or external reference. An external artifact must be written durably and hashed before its database record commits.
|
|
||||||
|
|
||||||
### 3. Compatibility Projections
|
|
||||||
|
|
||||||
- Keep `JobSource.raw_api_response` unchanged for existing and new compatibility reads until a later deprecation decision.
|
|
||||||
- Keep `JobSource.ai_metadata` for indexed/display-ready normalized values.
|
|
||||||
- Keep `Source.raw_transcription` as the latest successful machine-output projection while treating per-execution `JobSource.raw_transcription` as history.
|
|
||||||
- Document that older rows have an SDK snapshot but no exact transport capture.
|
|
||||||
|
|
||||||
## Implementation Phases
|
|
||||||
|
|
||||||
### 1. Correct Terminology and Define Typed Contracts
|
|
||||||
|
|
||||||
- Add typed domain models for request manifests, software context, transport metadata, failure phase, and artifact descriptors.
|
|
||||||
- Version every persisted JSON contract from its first release.
|
|
||||||
- Define the safe response-header allowlist. Begin with correlation, content type/encoding, date, retry/rate-limit, and router-specific generation identifiers only when documented and non-secret.
|
|
||||||
- Define size limits and external-storage thresholds for exact bodies and artifacts.
|
|
||||||
- Correct `docs/ver4/schema_v4.md` under “Page-Level Execution and AI Outputs” so the existing column is described as an SDK-serialized OpenRouter response snapshot, not a complete provider envelope, exact HTTP body, or native upstream-provider response. Apply the same terminology to architecture and UI schema references.
|
|
||||||
- Add serialization and secret-rejection unit tests before provider changes.
|
|
||||||
|
|
||||||
### 2. Add Additive Persistence and Upgrade Behavior
|
|
||||||
|
|
||||||
- Add the selected execution-evidence and artifact models.
|
|
||||||
- Add foreign keys, uniqueness constraints, and indexes for source/execution lookup.
|
|
||||||
- Implement idempotent upgrades following the repository's existing schema-upgrade policy.
|
|
||||||
- Do not populate exact response fields for historical rows.
|
|
||||||
- Do not write a capture-time classification onto historical rows during migration. Compatibility reads may describe a populated legacy `raw_api_response` as an SDK snapshot, but exports must identify that description as a later compatibility interpretation rather than execution-time metadata.
|
|
||||||
- Verify JSON portability and large-payload behavior for SQLite and PostgreSQL.
|
|
||||||
- Add upgrade tests starting from a representative pre-V4.2 schema.
|
|
||||||
|
|
||||||
### 3. Build Secret-Safe Request Manifests
|
|
||||||
|
|
||||||
- Build the manifest from the concrete outgoing request body immediately before transport, not from a narrower typed projection that may discard unrecognized request fields.
|
|
||||||
- Replace each image payload in that concrete representation with a source reference containing source UUID, digest, byte size, media type, dimensions, and transformation identity.
|
|
||||||
- Store exact prompt content and preserve omitted-versus-explicit parameter state.
|
|
||||||
- Include requested model, routing preferences, response-format requirements, and timeout/retry policy.
|
|
||||||
- Record application version/commit when available, adapter contract version, SDK package/version, and request-manifest schema version.
|
|
||||||
- Hash the canonical manifest representation for integrity checks.
|
|
||||||
- Test that credentials and embedded image data cannot enter the persisted manifest.
|
|
||||||
- Test that every field actually sent to the provider, including routing and future provider options, is represented or explicitly excluded by the manifest transform.
|
|
||||||
|
|
||||||
### 4. Capture OpenRouter Transport Evidence
|
|
||||||
|
|
||||||
- Evaluate the installed OpenRouter SDK hooks/client injection first.
|
|
||||||
- If hooks cannot expose an exact stable response before typed parsing, implement the non-streaming OpenRouter call through the existing async HTTP client boundary while retaining typed validation in the adapter.
|
|
||||||
- Read the response body once, preserve it exactly, then parse and normalize it.
|
|
||||||
- Store status, content type/encoding, allowlisted headers, request/generation ID, and timing.
|
|
||||||
- Maintain current authentication, referer/title headers, timeout behavior, and error classification.
|
|
||||||
- Explicitly document that the captured body is the OpenRouter-normalized transport response, not Gemini/Anthropic/OpenAI native upstream JSON.
|
|
||||||
- Add fixture-based tests proving unknown response fields survive transport capture even if a typed parser ignores them.
|
|
||||||
|
|
||||||
### 5. Preserve Failure Evidence
|
|
||||||
|
|
||||||
- Return or raise a typed provider failure that carries safe evidence separately from its user-facing error.
|
|
||||||
- Persist non-success status/body/allowlisted headers before marking an execution failed.
|
|
||||||
- Represent DNS/connect/TLS/local timeout failures as no-response outcomes with a failure phase and safe diagnostic category.
|
|
||||||
- Preserve response-validation failures with both the exact body and validation details.
|
|
||||||
- Keep transcription-quality rejection distinct from provider failure because a valid provider response was received.
|
|
||||||
- Ensure error strings and logs do not contain authorization data or embedded image payloads.
|
|
||||||
- Add tests for 4xx, 5xx, malformed JSON, schema mismatch, timeout, connection failure, and quality rejection.
|
|
||||||
|
|
||||||
### 6. Make Execution History Reliably Append-Only
|
|
||||||
|
|
||||||
- Confirm retry behavior creates a distinct execution attempt rather than reusing and overwriting a completed evidence record.
|
|
||||||
- Separate queue linkage from execution-attempt identity; the current update-in-place behavior cannot serve as append-only execution history.
|
|
||||||
- Assign each attempt a deterministic, monotonically increasing attempt number scoped to its Job and Source, enforced by a database uniqueness constraint.
|
|
||||||
- Update the latest-transcription projection only after a successful attempt.
|
|
||||||
- Never update prior response bodies, manifests, timings, or artifacts during a retry.
|
|
||||||
- Select the latest attempt and latest successful attempt by the persisted attempt number with a stable identifier as a defensive secondary key, never by timestamp alone.
|
|
||||||
- Add service/workflow tests covering retries, partial success, interrupted jobs, and historical projection behavior.
|
|
||||||
|
|
||||||
### 7. Add Generic Artifact Persistence
|
|
||||||
|
|
||||||
- Implement service operations to create, read, list, verify, export, and, only under explicit retention policy, delete artifacts.
|
|
||||||
- Validate semantic type, schema/version, digest, media type, and coordinate metadata.
|
|
||||||
- Support JSON artifacts inline initially when within the agreed size threshold.
|
|
||||||
- Support external artifacts through a constrained application-data root with atomic write, digest verification, and explicit missing-file errors.
|
|
||||||
- Add a provider-neutral example fixture representing OCR words/lines with polygons and confidence values.
|
|
||||||
- Do not integrate a live OCR vendor in this phase.
|
|
||||||
|
|
||||||
### 8. Add Evidence Inspection and Export
|
|
||||||
|
|
||||||
- Rename the current Source Detail label to identify historical values as an OpenRouter SDK Response Snapshot.
|
|
||||||
- Add separate sections for Request Manifest, Transport Response, Normalized Metadata, Software Context, and Derived Artifacts.
|
|
||||||
- Show an explicit “not captured for this historical execution” state instead of an empty object.
|
|
||||||
- Keep large bodies collapsed by default and avoid rendering embedded source data.
|
|
||||||
- Add a service-owned export that packages a versioned manifest, evidence JSON/body files, artifact content or references, and digest inventory.
|
|
||||||
- Exclude secrets and machine-local paths that are not required to interpret the evidence.
|
|
||||||
- Add UI and export tests for new, historical, failed, and large-evidence records.
|
|
||||||
|
|
||||||
### 9. Establish the Private Benchmark
|
|
||||||
|
|
||||||
- Select a small initial corpus, then expand only when it exposes meaningful differences.
|
|
||||||
- Stratify examples by printed/typed text, handwriting style, degradation, layout complexity, language, and editorial anomaly.
|
|
||||||
- Reference existing Source UUIDs and digests in a private manifest; do not copy family documents into public test fixtures.
|
|
||||||
- Create manually reviewed reference transcriptions following the invariant methodology.
|
|
||||||
- Implement or adopt existing project-compatible CER/WER calculations without changing dependencies unless justified.
|
|
||||||
- Score omissions, inventions, silent modernization, uncertainty markup, and layout fidelity separately from CER/WER.
|
|
||||||
- Record cost and latency from preserved execution evidence.
|
|
||||||
- Run the current `google/gemini-2.5-flash` configuration as the baseline before testing alternatives.
|
|
||||||
- Treat results as model-version/route/corpus specific and preserve each comparison run.
|
|
||||||
|
|
||||||
### 10. Verify, Migrate, and Align Documentation
|
|
||||||
|
|
||||||
- Run the smallest focused model, provider, service, workflow, UI, upgrade, and export test groups first.
|
|
||||||
- Run broader regression tests only after focused validation passes.
|
|
||||||
- Execute all destructive tests through `tools/run_destructive_tests.py`.
|
|
||||||
- Verify backup creation and required restoration behavior before any test touching real application data.
|
|
||||||
- Confirm existing Source Detail records remain readable after upgrade.
|
|
||||||
- Update V4 architecture, schema, requirements, and UI schema mappings to point to V4.2 semantics.
|
|
||||||
- Record any deliberate deviation from this plan in the V4.2 scope before release.
|
|
||||||
|
|
||||||
## Recommended Delivery Order
|
|
||||||
|
|
||||||
1. Typed/versioned evidence contracts and terminology.
|
|
||||||
2. Additive execution-evidence persistence.
|
|
||||||
3. Secret-safe request manifests.
|
|
||||||
4. Exact OpenRouter transport capture.
|
|
||||||
5. Failure evidence and append-only retry semantics.
|
|
||||||
6. Generic artifact persistence.
|
|
||||||
7. Inspection and export.
|
|
||||||
8. Private benchmark tooling and baseline run.
|
|
||||||
9. Migration, regression verification, and documentation alignment.
|
|
||||||
|
|
||||||
## Key Implementation Decisions to Resolve
|
|
||||||
|
|
||||||
1. Whether execution evidence is a one-to-one companion to `JobSource` or part of a new execution-attempt model required for append-only retries.
|
|
||||||
2. Whether exact response bodies remain database values at expected sizes or move to hashed external files above a threshold.
|
|
||||||
3. The canonical JSON algorithm used to hash request manifests.
|
|
||||||
4. The safe-header allowlist supported by OpenRouter and future adapters.
|
|
||||||
5. The application version identity available in local, packaged, and uncommitted development builds.
|
|
||||||
6. The initial inline/external artifact size threshold and application-data root.
|
|
||||||
7. Whether evidence exports include original source binaries by default, optionally, or only by reference.
|
|
||||||
8. The minimum private benchmark corpus size and review process before model comparisons influence defaults.
|
|
||||||
|
|
||||||
These decisions must be settled before their corresponding implementation phase; they do not weaken the invariant or expand V4.2 into live OCR integration.
|
|
||||||
|
|
||||||
## Resolved Implementation Decisions
|
|
||||||
|
|
||||||
1. `JobSource` remains queue linkage and a compatibility projection; immutable retries use a one-to-many
|
|
||||||
`ExecutionAttempt` model with a unique `(job_id, source_id, attempt_number)` constraint.
|
|
||||||
2. Exact OpenRouter response bytes remain database values for V4.2. Generic artifacts use inline canonical JSON up
|
|
||||||
to 1 MiB by default and constrained, atomically written external files above that threshold.
|
|
||||||
3. Request manifests use `transcription-canonical-json-v1`: UTF-8 JSON with sorted keys, compact separators,
|
|
||||||
preserved Unicode, and non-finite numbers rejected.
|
|
||||||
4. Safe response headers are explicitly allowlisted in the evidence contract; all others are discarded before
|
|
||||||
persistence.
|
|
||||||
5. Software identity records the package version, optional `TRANSCRIPTION_COMMIT`, adapter contract version,
|
|
||||||
OpenRouter SDK version, and Python version.
|
|
||||||
6. The artifact root defaults to `data/artifacts` and stores source-scoped relative references.
|
|
||||||
7. Evidence exports include source identity and digest by reference, not original source binaries.
|
|
||||||
8. The benchmark manifest is private and digest-referenced. Corpus size remains archive-dependent, but every run
|
|
||||||
uses preserved execution-attempt identity and the fixed literal scoring contract.
|
|
||||||
|
|
||||||
## Done When
|
|
||||||
|
|
||||||
- Every V4.2 acceptance criterion is satisfied by focused tests or an explicit demonstration.
|
|
||||||
- Existing SDK snapshots retain their content and are labeled accurately.
|
|
||||||
- New successful and failed calls preserve secret-safe provider-boundary evidence.
|
|
||||||
- Unknown transport fields survive even when the typed SDK/parser does not recognize them.
|
|
||||||
- Retries cannot overwrite prior execution evidence.
|
|
||||||
- A generic versioned artifact can represent OCR geometry and pass integrity verification.
|
|
||||||
- Evidence can be safely inspected and exported with schema identities and digests.
|
|
||||||
- The current model has a reproducible private benchmark baseline.
|
|
||||||
- No credential or embedded source payload appears in persisted manifests, safe headers, logs, or exports.
|
|
||||||
- Existing V4.1 behavior remains compatible.
|
|
||||||
|
|
||||||
## Out of Scope
|
|
||||||
|
|
||||||
- Live OCR/document-AI provider integration.
|
|
||||||
- Automatic model switching.
|
|
||||||
- Archive-wide reprocessing.
|
|
||||||
- Native upstream-provider response capture through OpenRouter when OpenRouter does not expose it.
|
|
||||||
- Guarantees of deterministic hosted-model output.
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [V4.2 Scope Boundary](scope_boundary_v4_2.md)
|
|
||||||
- [Digital Evidence and AI Processing Provenance](../invariant/ai_evidence_and_provenance.md)
|
|
||||||
- [V4 Architecture](../ver4/architecture_v4.md)
|
|
||||||
- [V4 Schema](../ver4/schema_v4.md)
|
|
||||||
- [V4 Requirements](../ver4/requirements_v4.md)
|
|
||||||
- [Draft V4.3 Implementation Plan](../ver4.3/implementation_plan_v4_3.md)
|
|
||||||
@@ -1,163 +0,0 @@
|
|||||||
# V4.2 Scope Boundary
|
|
||||||
|
|
||||||
This document defines the boundary for the digital-evidence and AI-provenance revision that follows V4.1 and precedes the planned V4.3 settings work. V4 remains the architecture baseline; V4.2 makes the existing evidence claims precise and adds a provider-neutral foundation for future processing artifacts.
|
|
||||||
|
|
||||||
## Purpose
|
|
||||||
|
|
||||||
- Align the application with the [Digital Evidence and AI Processing Provenance invariant](../invariant/ai_evidence_and_provenance.md).
|
|
||||||
- Preserve provider-boundary evidence before SDK parsing can remove unknown fields.
|
|
||||||
- Make successful and failed processing attempts inspectable without storing secrets.
|
|
||||||
- Support future OCR and layout outputs without coupling the database to one vendor.
|
|
||||||
- Establish a repeatable method for comparing transcription models against this archive.
|
|
||||||
|
|
||||||
## In Scope
|
|
||||||
|
|
||||||
### 1. Evidence Terminology and Existing-Data Compatibility
|
|
||||||
|
|
||||||
- Define transport response, router-normalized response, SDK response, normalized metadata, and derived artifact consistently in code, schema documentation, and UI labels.
|
|
||||||
- Treat existing `JobSource.raw_api_response` values as historical SDK response snapshots.
|
|
||||||
- Preserve every existing `Job`, `Source`, and `JobSource` row.
|
|
||||||
- Use additive migrations and compatibility reads; do not reinterpret previously stored values as exact transport captures.
|
|
||||||
- Correct the “Page-Level Execution and AI Outputs” rule in `docs/ver4/schema_v4.md` that currently describes `JOB_SOURCE` as storing a complete provider response envelope. The corrected rule must identify `raw_api_response` as an SDK-serialized OpenRouter response snapshot and state that it is neither the exact HTTP body nor the native upstream-provider response.
|
|
||||||
|
|
||||||
### 2. Secret-Safe Request Manifests
|
|
||||||
|
|
||||||
- Persist the effective request specification for each page execution without storing credentials or duplicate base64 media.
|
|
||||||
- Include requested provider/model, routing constraints, prompt content and hash, explicitly supplied parameters, source digest, media type, dimensions when known, and page identity.
|
|
||||||
- Distinguish an omitted optional parameter from an explicitly supplied null or value.
|
|
||||||
- Record application, provider-adapter, Python client, and relevant schema versions.
|
|
||||||
- Use source or derivative references in place of embedded media bytes.
|
|
||||||
|
|
||||||
### 3. Provider-Boundary Response Capture
|
|
||||||
|
|
||||||
- Capture the exact HTTP response body before OpenRouter SDK parsing for non-streaming transcription calls.
|
|
||||||
- Store HTTP status and an explicit allowlist of safe response headers.
|
|
||||||
- Store router request/generation identifiers and resolved model/provider-routing metadata when exposed.
|
|
||||||
- Preserve the current parsed SDK snapshot and normalized metadata where useful.
|
|
||||||
- Keep exact body, parsed representation, and normalized fields distinguishable.
|
|
||||||
|
|
||||||
### 4. Failure Evidence and Timing
|
|
||||||
|
|
||||||
- Create or update a page execution record for every attempted provider call.
|
|
||||||
- Persist safe response evidence for non-success HTTP responses.
|
|
||||||
- Distinguish HTTP response failures, connection failures, local timeouts, response-validation failures, and transcription-quality failures.
|
|
||||||
- Store execution start/end times or duration using a clearly defined clock policy.
|
|
||||||
- Do not collapse a provider error body into only a generic user-facing message.
|
|
||||||
|
|
||||||
### 5. Generic Processing Artifacts
|
|
||||||
|
|
||||||
- Add a provider-neutral representation for versioned derived artifacts.
|
|
||||||
- Support inline JSON and externally stored payloads with a digest and stable reference.
|
|
||||||
- Record artifact type, format, schema/version, producer/version, source, producing execution, and creation time.
|
|
||||||
- Define coordinate-system metadata sufficient for word, line, block, or page geometry.
|
|
||||||
- Permit future OCR/layout/confidence results without implementing a vendor-specific table for each provider.
|
|
||||||
|
|
||||||
### 6. Evidence Inspection and Export
|
|
||||||
|
|
||||||
- Expand Source Detail and/or Job Detail to identify the evidence layer being displayed.
|
|
||||||
- Provide readable JSON inspection for request manifests, transport metadata, parsed responses, normalized metadata, and derived artifacts.
|
|
||||||
- Provide a safe export containing evidence content or references, relationships, schema versions, and digests.
|
|
||||||
- Clearly label evidence that was not captured for historical records.
|
|
||||||
- Do not display or export credentials, unrestricted headers, or embedded base64 source media.
|
|
||||||
|
|
||||||
### 7. Representative-Corpus Benchmark Protocol
|
|
||||||
|
|
||||||
- Define a private benchmark manifest referencing source digests rather than duplicating archival media.
|
|
||||||
- Include representative printed, typed, handwritten, degraded, tabular, and spatially complex pages.
|
|
||||||
- Pair each benchmark item with a manually reviewed literal transcription.
|
|
||||||
- Score character error rate, word error rate, omissions, inventions, silent normalization, uncertainty handling, layout fidelity, cost, and latency.
|
|
||||||
- Preserve the complete execution provenance for every benchmark run.
|
|
||||||
- Keep the current model as a baseline; do not change the application default solely from vendor benchmarks.
|
|
||||||
|
|
||||||
### 8. Migration, Integrity, and Verification
|
|
||||||
|
|
||||||
- Provide non-destructive upgrade behavior for supported SQLite and PostgreSQL deployments.
|
|
||||||
- Backfill only facts that can be derived reliably from existing records.
|
|
||||||
- Mark unavailable historical evidence as unavailable rather than fabricating it.
|
|
||||||
- Add digest, serialization, header-allowlist, failure-path, compatibility, artifact, export, and UI inspection tests.
|
|
||||||
- Run destructive tests only through the repository's required backup-and-restore wrapper.
|
|
||||||
|
|
||||||
## Out of Scope
|
|
||||||
|
|
||||||
- Selecting or declaring a permanent best transcription model.
|
|
||||||
- Changing the default transcription model without benchmark evidence and a separate decision.
|
|
||||||
- Integrating Azure Document Intelligence, Google Document AI, Transkribus, Mistral OCR, or another OCR provider in V4.2.
|
|
||||||
- Generating bounding boxes retroactively for existing transcriptions.
|
|
||||||
- Bulk reprocessing the archive.
|
|
||||||
- Packet capture, TLS evidence, full unrestricted request/response headers, or credential retention.
|
|
||||||
- Storing duplicate base64 source images in request manifests.
|
|
||||||
- Guaranteeing byte-identical reproduction from nondeterministic or updated hosted models.
|
|
||||||
- Automatic entity extraction, biography generation, or genealogical inference.
|
|
||||||
- Replacing the relational database with an event store or content-addressed object store.
|
|
||||||
- Destructive renaming or removal of `raw_api_response`.
|
|
||||||
|
|
||||||
## Locked Design Decisions
|
|
||||||
|
|
||||||
### A. The Original Source Is Primary Evidence
|
|
||||||
|
|
||||||
- Original uploaded bytes and their digest remain authoritative.
|
|
||||||
- Processing derivatives and outputs are independently identified derived evidence.
|
|
||||||
- Future OCR/layout work reuses the original or a documented derivative.
|
|
||||||
|
|
||||||
### B. Evidence Is Layered
|
|
||||||
|
|
||||||
- Exact transport evidence, SDK-parsed objects, normalized metadata, and transcription text serve different purposes.
|
|
||||||
- One representation must not silently stand in for another.
|
|
||||||
- UI and export labels name the stored evidence layer.
|
|
||||||
|
|
||||||
### C. History Is Append-Only
|
|
||||||
|
|
||||||
- A retry or reprocessing attempt creates new execution evidence.
|
|
||||||
- Convenience caches may change, but historical execution output does not.
|
|
||||||
- Human revisions remain separate from machine output.
|
|
||||||
|
|
||||||
### D. Capture Is Secret-Safe by Construction
|
|
||||||
|
|
||||||
- Safe headers are allowlisted.
|
|
||||||
- Authorization, cookies, API keys, and unrestricted headers are never persisted.
|
|
||||||
- Request manifests reference source digests instead of embedding source bytes.
|
|
||||||
|
|
||||||
### E. Derived Artifacts Are Generic and Versioned
|
|
||||||
|
|
||||||
- Artifact storage is not limited to bounding boxes.
|
|
||||||
- Coordinate metadata declares units, origin, dimensions, and transformations.
|
|
||||||
- Provider-specific payloads may be retained without making provider-specific fields the durable application contract.
|
|
||||||
|
|
||||||
### F. Existing Evidence Keeps Its Original Meaning
|
|
||||||
|
|
||||||
- Existing `raw_api_response` data remains an SDK response snapshot.
|
|
||||||
- A migration may label or classify it but may not claim that missing transport data was captured.
|
|
||||||
- Historical nulls and absent fields remain distinguishable from new explicitly captured values.
|
|
||||||
|
|
||||||
## Data and Compatibility Policy
|
|
||||||
|
|
||||||
- All schema changes are additive in V4.2.
|
|
||||||
- Existing source files, hashes, transcriptions, revisions, prompts, jobs, and relationships remain valid.
|
|
||||||
- Compatibility reads continue to display historical SDK snapshots.
|
|
||||||
- Large derived artifacts may be stored outside the database when the database retains a stable reference, digest, media type, and schema identity.
|
|
||||||
- JSON evidence must remain portable across SQLite and PostgreSQL.
|
|
||||||
- Exports use explicit schema versions so later releases can interpret older packages.
|
|
||||||
|
|
||||||
## Acceptance Criteria
|
|
||||||
|
|
||||||
1. A new execution can be traced from its source digest through its frozen request manifest, transport response, parsed/normalized data, and derived outputs.
|
|
||||||
2. Exact response content is captured before SDK parsing and is clearly distinguished from the existing SDK snapshot.
|
|
||||||
3. Failed HTTP calls retain safe provider evidence; calls with no response record that fact explicitly.
|
|
||||||
4. Omitted parameters remain distinguishable from explicit values.
|
|
||||||
5. No persisted request, header set, UI display, log, or export contains API credentials.
|
|
||||||
6. Retrying or reprocessing does not overwrite prior execution evidence.
|
|
||||||
7. Historical records remain readable and are not mislabeled as exact transport captures.
|
|
||||||
8. A versioned generic artifact can represent OCR/layout JSON and its coordinate system without a provider-specific schema change.
|
|
||||||
9. Evidence exports include relationships, schema identities, and digests sufficient for independent integrity checks.
|
|
||||||
10. The benchmark protocol can compare the current baseline with another model on the same private corpus and scoring rules.
|
|
||||||
11. Additive migrations and focused tests work across the supported persistence model.
|
|
||||||
12. All destructive-test runs comply with the backup-and-restore protocol.
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [V4.2 Implementation Plan](implementation_plan_v4_2.md)
|
|
||||||
- [Digital Evidence and AI Processing Provenance](../invariant/ai_evidence_and_provenance.md)
|
|
||||||
- [V4 Architecture](../ver4/architecture_v4.md)
|
|
||||||
- [V4 Schema](../ver4/schema_v4.md)
|
|
||||||
- [V4 Requirements](../ver4/requirements_v4.md)
|
|
||||||
- [Transcription Methodology](../invariant/transcription_methodology.md)
|
|
||||||
@@ -1,121 +0,0 @@
|
|||||||
# Implementation Plan (Version 4.3)
|
|
||||||
|
|
||||||
## Goal
|
|
||||||
|
|
||||||
Deliver constrained, installation-local application settings while preserving the completed V4.2 behavioral baseline and historical provenance.
|
|
||||||
|
|
||||||
## Planning Constraints
|
|
||||||
|
|
||||||
- V4, V4.1, and V4.2 remain the behavioral baseline.
|
|
||||||
- Settings must use explicit domain operations rather than direct database, environment-file, or arbitrary filesystem access from UI pages.
|
|
||||||
- Prompt changes must preserve historical Job provenance and use a defined safe-write policy.
|
|
||||||
- Source Page Reordering is excluded.
|
|
||||||
- Database, integration, and UI tests must use confirmed isolated test data and must never modify `data/transcription.db`.
|
|
||||||
- Potentially destructive tests must run only through `tools/run_destructive_tests.py`.
|
|
||||||
|
|
||||||
## Expected Project Impact
|
|
||||||
|
|
||||||
| Area | Expected impact |
|
|
||||||
| --- | --- |
|
|
||||||
| Documents service | Expand controlled Document Type maintenance operations. |
|
|
||||||
| People service | Expand controlled Person Role maintenance operations. |
|
|
||||||
| Prompt adapter/service | Add constrained listing, reading, validation, atomic writing, backup, and explicit recovery of existing prompt artifacts. |
|
|
||||||
| UI composition/navigation | Register Settings routes and navigation without moving persistence into UI code. |
|
|
||||||
| Tests | Add isolated registry lifecycle, prompt safety, and UI workflow coverage. |
|
|
||||||
|
|
||||||
## Implementation Phases
|
|
||||||
|
|
||||||
### 1. Define Service Contracts
|
|
||||||
|
|
||||||
- Define Document Type maintenance commands for create, relabel, activate, deactivate, and delete-if-unreferenced.
|
|
||||||
- Define Person Role maintenance commands for create, relabel, activate, deactivate, and delete-if-unreferenced.
|
|
||||||
- Define a Prompt Store interface for constrained list, read, write, backup-status, and explicit recovery behavior.
|
|
||||||
- Map validation, conflict, not-found, dependency, and filesystem failures to existing `AppError` categories.
|
|
||||||
|
|
||||||
### 2. Expand Registry Maintenance Services
|
|
||||||
|
|
||||||
- Reuse existing Document and People service ownership.
|
|
||||||
- Add explicit write methods rather than passing UI-mutated ORM objects directly where practical.
|
|
||||||
- Normalize Document Type labels and reject case-insensitive duplicates deterministically.
|
|
||||||
- Keep Person Role stable-code validation and duplicate rejection.
|
|
||||||
- Permit deletion only after a service-owned reference check proves the entry is unreferenced.
|
|
||||||
- Reject deletion of referenced entries deterministically without partial mutation.
|
|
||||||
- Permit label changes whether or not an entry is referenced.
|
|
||||||
- Preserve inactive entries for historical reads.
|
|
||||||
- Order Document Types alphabetically by normalized label.
|
|
||||||
- Order Person Roles deterministically by label and then code without adding a schema field.
|
|
||||||
- Add service tests for create, relabel, activation, deactivation, duplicates, immutable codes, ordering, allowed deletion, and blocked referenced deletion.
|
|
||||||
|
|
||||||
### 3. Add Constrained Prompt Storage
|
|
||||||
|
|
||||||
- Place filesystem access behind a dedicated Prompt Store/service boundary.
|
|
||||||
- Resolve all filenames directly beneath the configured prompt root and reject traversal.
|
|
||||||
- Permit only existing files with the agreed Markdown extension and reject empty content.
|
|
||||||
- Exclude prompt creation and deletion.
|
|
||||||
- Write new content to a sibling temporary file, flush and sync it, preserve the active file as the sole previous-version backup, and atomically replace the active file.
|
|
||||||
- Expose explicit backup recovery through the same filename validation and safe-write path; never perform automatic rollback.
|
|
||||||
- Clean up temporary files after failed writes while preserving the active prompt and any valid backup.
|
|
||||||
- Preserve file encoding and provide explicit failures for read-only or unavailable storage.
|
|
||||||
- Do not modify any Job row when prompt defaults change.
|
|
||||||
- Add unit tests for valid reads/writes, traversal, invalid names, nonexistent-file creation attempts, empty content, atomic replacement failures, single-backup rotation, explicit recovery, filesystem failures, and unchanged Job provenance.
|
|
||||||
|
|
||||||
### 4. Build the Settings UI
|
|
||||||
|
|
||||||
- Register a Settings landing page and navigation entry.
|
|
||||||
- Add separate pages or panels for Document Types, Person Roles, and Prompts.
|
|
||||||
- Keep pages responsible for orchestration and notifications only.
|
|
||||||
- Use service callbacks for all mutations.
|
|
||||||
- Explain inactive historical entries and future-only prompt effects in the UI.
|
|
||||||
- Present deletion only for unreferenced registry entries and preserve clear conflict feedback if references appear before submission.
|
|
||||||
- Present prompt backup availability and recovery as an explicit operator action.
|
|
||||||
- Do not render raw environment values or secrets.
|
|
||||||
- Add no settings API routes.
|
|
||||||
|
|
||||||
### 5. Verification and Rollout
|
|
||||||
|
|
||||||
- Confirm every database, integration, and UI test is configured for an isolated test database before execution.
|
|
||||||
- Never run those tests against live data and never modify or replace `data/transcription.db`.
|
|
||||||
- Invoke potentially destructive tests only through `tools/run_destructive_tests.py`.
|
|
||||||
- Run focused service tests before UI integration tests.
|
|
||||||
- Verify inactive registry behavior in both historical display and create/edit selectors.
|
|
||||||
- Verify referenced entries can be relabeled or deactivated but not deleted.
|
|
||||||
- Verify unreferenced entries can be deleted.
|
|
||||||
- Verify prompt changes are picked up by newly created Jobs while historical Jobs retain frozen content/hash.
|
|
||||||
- Run the relevant regression suite.
|
|
||||||
|
|
||||||
## Migration and Compatibility Notes
|
|
||||||
|
|
||||||
- Existing registry records remain valid.
|
|
||||||
- Prompt editing changes mutable application files, not database provenance already captured on Jobs.
|
|
||||||
- V4.3 must not require users to recreate existing Sources, Documents, People, roles, or types.
|
|
||||||
- Person Role ordering requires no schema migration.
|
|
||||||
- Registry deletion introduces no cascade behavior; references always block deletion.
|
|
||||||
|
|
||||||
## Delivery Order
|
|
||||||
|
|
||||||
1. Implement registry maintenance service operations.
|
|
||||||
2. Implement the Prompt Store and safety policy.
|
|
||||||
3. Build Settings pages.
|
|
||||||
4. Run isolated integration and regression verification.
|
|
||||||
|
|
||||||
## Done Criteria
|
|
||||||
|
|
||||||
- All V4.3 acceptance criteria are testable and satisfied.
|
|
||||||
- Settings mutations cross explicit service or adapter boundaries.
|
|
||||||
- Document Types use UUID-only identity and unique labels; Person Role codes cannot be accidentally changed.
|
|
||||||
- Referenced registry entries can be relabeled or deactivated but cannot be deleted.
|
|
||||||
- Unreferenced registry entries can be deleted without cascade behavior.
|
|
||||||
- Prompt writes cannot escape the configured directory or rewrite historical provenance.
|
|
||||||
- Prompt writes are atomic, retain one backup, and support explicit recovery.
|
|
||||||
- No secret or raw environment editor exists.
|
|
||||||
- No settings API surface exists.
|
|
||||||
- V4.1 and V4.2 workflows remain intact.
|
|
||||||
- Verification does not touch live data or `data/transcription.db`.
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [V4.3 Scope Boundary](scope_boundary_v4_3.md)
|
|
||||||
- [V4.2 Implementation Plan](../ver4.2/implementation_plan_v4_2.md)
|
|
||||||
- [V4.1 Implementation Plan](../ver4.1/implementation_plan_v4_1.md)
|
|
||||||
- [V4 Implementation Plan](../ver4/implementation_plan_v4.md)
|
|
||||||
- [V4 Error Handling Policy](../ver4/error_handling_v4.md)
|
|
||||||
@@ -1,154 +0,0 @@
|
|||||||
# V4.3 Scope Boundary
|
|
||||||
|
|
||||||
This document defines the frozen boundary for the constrained-settings revision that follows the completed V4.2 evidence-and-provenance work. V4, V4.1, and V4.2 remain the behavioral and architecture baseline.
|
|
||||||
|
|
||||||
## Purpose
|
|
||||||
|
|
||||||
- Provide a constrained Settings area for safe maintenance of selected application-managed configuration.
|
|
||||||
- Avoid exposing secrets, restart-sensitive settings, or unrestricted filesystem editing through the UI.
|
|
||||||
|
|
||||||
## In Scope
|
|
||||||
|
|
||||||
### 1. Settings Navigation
|
|
||||||
|
|
||||||
- Add a Settings entry to application navigation.
|
|
||||||
- Provide separate, clearly described settings areas rather than a raw configuration editor.
|
|
||||||
- Restrict V4.3 settings to application-managed values that can be validated and safely changed at runtime.
|
|
||||||
|
|
||||||
### 2. Document Type Maintenance
|
|
||||||
|
|
||||||
- List active and inactive Document Types.
|
|
||||||
- Add new types with a unique user-facing label.
|
|
||||||
- Edit labels and active state.
|
|
||||||
- Activate or deactivate types without invalidating historical Documents.
|
|
||||||
- Allow deletion only when no Document references the type.
|
|
||||||
- Allow label changes regardless of whether the type is referenced.
|
|
||||||
- Display types alphabetically by label.
|
|
||||||
|
|
||||||
### 3. Person Role Maintenance
|
|
||||||
|
|
||||||
- List active and inactive Person Roles.
|
|
||||||
- Add new roles with a stable unique code and user-facing label.
|
|
||||||
- Edit mutable labels.
|
|
||||||
- Activate or deactivate roles without invalidating historical links.
|
|
||||||
- Do not allow changing a stable code after creation.
|
|
||||||
- Order roles deterministically by label and then code; do not add persisted role sort order.
|
|
||||||
- Allow deletion only when no document-person link references the role.
|
|
||||||
- Allow label changes regardless of whether the role is referenced.
|
|
||||||
|
|
||||||
### 4. Prompt Maintenance
|
|
||||||
|
|
||||||
- List prompt markdown files from the configured prompt directory.
|
|
||||||
- View a prompt with a concise explanation of its purpose and use.
|
|
||||||
- Edit an existing prompt as plain markdown text.
|
|
||||||
- Validate the filename boundary and reject empty prompt content.
|
|
||||||
- Save changes explicitly and report filesystem failures.
|
|
||||||
- Preserve submission-time prompt text and hash already frozen on existing Jobs.
|
|
||||||
- Edit existing prompt files only; prompt creation and deletion are excluded.
|
|
||||||
- Save through a sibling temporary file, flush and sync file content, retain one previous-version backup, and atomically replace the active file.
|
|
||||||
- Provide an explicit recovery operation that restores the retained backup through the same safe-write path; do not silently roll back a failed or unwanted edit.
|
|
||||||
|
|
||||||
### 5. Deployment Boundary
|
|
||||||
|
|
||||||
- Settings changes apply only to the current installation.
|
|
||||||
- V4.3 adds no settings API endpoints.
|
|
||||||
- Service contracts must remain independent of the UI so a separately authorized API can be considered later.
|
|
||||||
|
|
||||||
## Out of Scope
|
|
||||||
|
|
||||||
- Viewing or editing raw `.env` files.
|
|
||||||
- Displaying or changing provider API keys and other secrets.
|
|
||||||
- Editing host, port, database connection, upload paths, or other restart-sensitive runtime settings.
|
|
||||||
- Arbitrary file browsing or arbitrary prompt paths.
|
|
||||||
- Runtime theme/CSS editing.
|
|
||||||
- Installing themes or plugins.
|
|
||||||
- Source page renumbering or reordering.
|
|
||||||
- Source movement between Documents.
|
|
||||||
- Automatic ordering based on filenames, OCR, or image content.
|
|
||||||
- Prompt creation, deletion, and multi-version history.
|
|
||||||
- Persisted sort-order maintenance for Person Roles.
|
|
||||||
- Settings read or write API endpoints.
|
|
||||||
- FamilySearch API synchronization.
|
|
||||||
- A generic external-reference registry.
|
|
||||||
- Ancestry references and Google Maps links.
|
|
||||||
|
|
||||||
## Locked Design Decisions
|
|
||||||
|
|
||||||
### A. Registry Identity and Lifecycle
|
|
||||||
|
|
||||||
- Document Types use UUID identity and case-insensitively unique labels; no separate code is exposed or stored.
|
|
||||||
- Person Role codes remain stable identifiers.
|
|
||||||
- Labels and active state remain mutable.
|
|
||||||
- Historical references remain valid when a registry entry is inactive.
|
|
||||||
- Labels may be updated for referenced and unreferenced entries.
|
|
||||||
- Unreferenced entries may be deleted; referenced entries may only be deactivated.
|
|
||||||
|
|
||||||
### B. No Raw Environment Editor
|
|
||||||
|
|
||||||
- `.env` may contain secrets and values that are not safely reloadable.
|
|
||||||
- V4.3 exposes only purpose-built forms backed by explicit validation and service methods.
|
|
||||||
|
|
||||||
### C. Prompt Editing Is Constrained
|
|
||||||
|
|
||||||
- Prompt maintenance is limited to direct children of the configured prompt directory.
|
|
||||||
- Existing Job provenance is never rewritten when a prompt file changes.
|
|
||||||
- The UI must distinguish editing the default for future submissions from inspecting historical Job prompts.
|
|
||||||
|
|
||||||
### D. Prompt Writes Are Atomic and Recoverable
|
|
||||||
|
|
||||||
- Writes use a sibling temporary file and atomic replacement so readers observe either the old or new complete prompt.
|
|
||||||
- The immediately previous prompt version is retained as the sole backup.
|
|
||||||
- Recovery is an explicit operator action and uses the same validated safe-write path.
|
|
||||||
- Prompt creation and deletion are not available in V4.3.
|
|
||||||
|
|
||||||
### E. Person Role Ordering Is Deterministic, Not Persisted
|
|
||||||
|
|
||||||
- Person Roles are ordered by label and then stable code.
|
|
||||||
- V4.3 does not add a `sort_order` field to Person Roles.
|
|
||||||
- Document Types use alphabetical label ordering and have no persisted sort order.
|
|
||||||
|
|
||||||
### F. Settings Are Installation-Local
|
|
||||||
|
|
||||||
- V4.3 provides Settings through the local application UI and domain services only.
|
|
||||||
- No settings API surface is introduced.
|
|
||||||
|
|
||||||
## Data and Compatibility Policy
|
|
||||||
|
|
||||||
- V4.3 does not rewrite existing Documents, document-person links, Jobs, Sources, execution evidence, or prompt provenance.
|
|
||||||
- Deactivation preserves referenced registry entries for historical display while excluding them from default create selectors.
|
|
||||||
- Deletion checks are performed at the service boundary and must fail deterministically when references exist.
|
|
||||||
- Prompt files are constrained to existing Markdown files that are direct children of the configured prompt root.
|
|
||||||
- Settings UI code performs no direct database, environment-file, or arbitrary filesystem mutations.
|
|
||||||
- Source page numbering and ordering behavior is unchanged.
|
|
||||||
|
|
||||||
## Acceptance Criteria
|
|
||||||
|
|
||||||
1. Document Type UUID identity and Person Role stable codes preserve historical references.
|
|
||||||
2. Inactive registry entries remain visible on historical records but are excluded from default create selectors.
|
|
||||||
3. Labels can be changed for referenced or unreferenced registry entries.
|
|
||||||
4. An unreferenced Document Type or Person Role can be deleted, while deletion of a referenced entry fails without partial mutation.
|
|
||||||
5. Document Types use alphabetical label ordering; Person Roles use deterministic label/code ordering.
|
|
||||||
6. Prompt edits are restricted to existing Markdown files directly beneath the configured prompt directory.
|
|
||||||
7. Prompt saves use atomic replacement, retain exactly one previous-version backup, and support explicit recovery.
|
|
||||||
8. A prompt edit affects future Jobs only and leaves stored Job provenance unchanged.
|
|
||||||
9. No Settings page exposes secrets, unrestricted filesystem access, or a settings API.
|
|
||||||
10. Focused service and UI tests pass without regressing V4.1 or V4.2 workflows.
|
|
||||||
11. Database, integration, and UI tests use confirmed isolated test data and never modify `data/transcription.db`; potentially destructive tests run only through `tools/run_destructive_tests.py`.
|
|
||||||
|
|
||||||
## Scope Freeze Gate
|
|
||||||
|
|
||||||
V4.3 is sufficiently frozen to begin implementation:
|
|
||||||
|
|
||||||
- V4.2 is the completed behavioral baseline.
|
|
||||||
- Registry lifecycle and ordering behavior are resolved.
|
|
||||||
- Prompt lifecycle, atomic-write, backup, and recovery behavior are resolved.
|
|
||||||
- The installation-local deployment boundary is resolved.
|
|
||||||
- The implementation plan is a committed delivery plan.
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [V4.3 Implementation Plan](implementation_plan_v4_3.md)
|
|
||||||
- [V4.2 Scope Boundary](../ver4.2/scope_boundary_v4_2.md)
|
|
||||||
- [V4.1 Scope Boundary](../ver4.1/scope_boundary_v4_1.md)
|
|
||||||
- [V4 Architecture](../ver4/architecture_v4.md)
|
|
||||||
- [V4 Schema](../ver4/schema_v4.md)
|
|
||||||
@@ -1,188 +0,0 @@
|
|||||||
# Implementation Plan (Version 4.4)
|
|
||||||
|
|
||||||
## Goal
|
|
||||||
|
|
||||||
Deliver hidden semantic identity for built-in registries, a single atomic Linked People workflow, and safe browser-native printing of archival Documents and their current transcriptions.
|
|
||||||
|
|
||||||
## Planning Constraints
|
|
||||||
|
|
||||||
- V4.3 is the completed implementation baseline.
|
|
||||||
- V4.4 may replace V4/V4.3 registry and document-person contracts only as specified by the V4.4 scope.
|
|
||||||
- Semantic keys are internal and immutable; UI and public API contracts use UUIDs and labels.
|
|
||||||
- Document and link edits must not partially commit.
|
|
||||||
- Print output must not execute stored text or expose machine-local source paths.
|
|
||||||
- Source page reordering remains excluded.
|
|
||||||
- Database, integration, and UI tests must use confirmed isolated data and never modify `data/transcription.db`.
|
|
||||||
- Potentially destructive tests must run only through `tools/run_destructive_tests.py`.
|
|
||||||
|
|
||||||
## Expected Project Impact
|
|
||||||
|
|
||||||
| Area | Expected impact |
|
|
||||||
| --- | --- |
|
|
||||||
| Models and schema bootstrap | Add nullable unique semantic keys, simplify document-person identity, and seed frozen built-ins. |
|
|
||||||
| Document service | Maintain built-in Document Types, usage summaries, UUID assignment, and atomic Document/link writes. |
|
|
||||||
| People service | Maintain built-in Person Roles, link summaries, UUID-only role assignment, and one-person-per-document enforcement. |
|
|
||||||
| V4 document API | Remove role-code selectors and compatibility role fields; enforce UUID-only relationship writes. |
|
|
||||||
| Settings UI | Use matching table workflows for Document Types and Person Roles. |
|
|
||||||
| Document Create/Edit | Replace role-specific multiselects with one staged Linked People table and inline editor. |
|
|
||||||
| Document Detail/printing | Add format selection, print preview, safe Source media rendering, print CSS, and job metadata. |
|
|
||||||
| Tests and documentation | Replace superseded cardinality/identity assertions and add isolated registry, editor, transaction, and print coverage. |
|
|
||||||
|
|
||||||
## Implementation Phases
|
|
||||||
|
|
||||||
### 1. Align Durable Registry Contracts
|
|
||||||
|
|
||||||
- Add nullable, unique `semantic_key` fields to `DocumentType` and `PersonRole`.
|
|
||||||
- Keep UUIDs as primary and foreign-key identity.
|
|
||||||
- Add normalized-label storage and uniqueness to Person Roles using the same trim and case-normalization policy as Document Types.
|
|
||||||
- Remove the user-created Person Role code contract.
|
|
||||||
- Define built-in detection as `semantic_key is not null`.
|
|
||||||
- Centralize the frozen built-in definitions in one domain-owned location.
|
|
||||||
- Seed six Document Types and three Person Roles idempotently.
|
|
||||||
- Ensure label edits never change semantic keys.
|
|
||||||
- Reject deletion of every built-in before checking references.
|
|
||||||
- Continue blocking deletion of referenced custom entries.
|
|
||||||
- Return deterministic validation, conflict, dependency, and not-found errors through existing error categories.
|
|
||||||
|
|
||||||
### 2. Establish the Clean Schema
|
|
||||||
|
|
||||||
- Remove legacy `DocumentPerson.role` compatibility storage and the fixed `DocumentPersonRole` enum.
|
|
||||||
- Make `DocumentPerson.role_id` required.
|
|
||||||
- Replace role-specific uniqueness with a unique `(document_id, person_id)` constraint.
|
|
||||||
- Remove obsolete Document Type and Person Role migration paths that exist only for disposable development data.
|
|
||||||
- Keep fresh schema creation and built-in seeding portable across SQLite and PostgreSQL.
|
|
||||||
- Make configured development-database recreation a separate operator-confirmed step that displays the resolved target path rather than assuming `app.db` or `data/transcription.db`.
|
|
||||||
- Never invoke recreation from application startup or test setup.
|
|
||||||
- Add isolated schema tests for fresh creation, seed idempotence, semantic-key uniqueness, normalized-label uniqueness, required roles, and link uniqueness.
|
|
||||||
|
|
||||||
### 3. Refine Registry Services and API Contracts
|
|
||||||
|
|
||||||
- Add summary queries for Document counts and Person Role link counts without per-row queries.
|
|
||||||
- Order both registries by normalized label with UUID as a deterministic tie-breaker.
|
|
||||||
- Expose built-in status as a derived read value where the Settings UI needs it.
|
|
||||||
- Keep semantic-key lookup behind service methods for application-owned behavior such as resolving authors.
|
|
||||||
- Ensure create operations always produce custom entries with null semantic keys.
|
|
||||||
- Ensure update operations accept only label and active state.
|
|
||||||
- Remove `role_code` request alternatives and compatibility role responses from the V4 document API.
|
|
||||||
- Require `role_id` for document-person creation and updates.
|
|
||||||
- Add service and API tests for hidden semantic identity, relabeling, activation, built-in protection, custom deletion, counts, ordering, UUID-only writes, and conflicts.
|
|
||||||
|
|
||||||
### 4. Build Matching Settings Tables
|
|
||||||
|
|
||||||
- Retain the existing Document Types table workflow and add the Built-in column.
|
|
||||||
- Replace the current per-row Person Role controls with the same selection-based table pattern.
|
|
||||||
- Render the agreed columns and usage counts.
|
|
||||||
- Keep labels as the only registry text shown in selectors.
|
|
||||||
- Add creates custom entries only.
|
|
||||||
- Edit dialogs expose label and active state only.
|
|
||||||
- Delete reports protected-built-in and referenced-custom conflicts clearly.
|
|
||||||
- Avoid direct persistence queries from the Settings page.
|
|
||||||
- Add component-level UI assertions for columns, actions, label-only selectors, and immutable built-in presentation.
|
|
||||||
|
|
||||||
### 5. Add a Staged Linked People Editor
|
|
||||||
|
|
||||||
- Introduce a small typed UI-state model for staged `(person_id, role_id)` rows rather than storing raw widget values.
|
|
||||||
- Share the editor component between Create Document and Edit Document.
|
|
||||||
- Render a multi-selection table with Person and Role labels.
|
|
||||||
- Add an inline editor whose mode is explicitly Add or Edit.
|
|
||||||
- Disable already-linked People when adding; retain the edited Person as an option during Edit.
|
|
||||||
- Require exactly one row for Edit and allow one or more rows for Delete.
|
|
||||||
- Save and Delete mutate only staged UI state.
|
|
||||||
- Cancel discards only the active inline edit.
|
|
||||||
- Preserve inactive-role historical rows in Edit while restricting new assignments and changes to active roles.
|
|
||||||
- Preserve `person_id` preselection by staging that Person with the active built-in `author` role, with warning behavior for invalid or unavailable selections.
|
|
||||||
- Preserve the `return_to=jobs_new` success path.
|
|
||||||
- Keep navigation to Person creation separate; V4.4 does not add an embedded Person editor.
|
|
||||||
- Add UI tests for staging, duplicate prevention, selection rules, inactive roles, cancel behavior, and both Document forms.
|
|
||||||
|
|
||||||
### 6. Persist Document and Links Atomically
|
|
||||||
|
|
||||||
- Add service commands for Create Document with complete links and Update Document with complete links.
|
|
||||||
- Validate Document Type, every Person, every Person Role, active assignment rules, and duplicate People before mutation.
|
|
||||||
- Compute deterministic add, update, and remove deltas for Edit.
|
|
||||||
- Apply Document and link mutations in one database transaction and commit once.
|
|
||||||
- Roll back the complete operation on any validation, conflict, or persistence failure.
|
|
||||||
- Return the persisted Document detail required by the UI after success.
|
|
||||||
- Reuse these commands from UI orchestration rather than sequencing independent service commits.
|
|
||||||
- Add failure-injection tests proving no partial Document or link mutation survives.
|
|
||||||
|
|
||||||
### 7. Define a Print Projection
|
|
||||||
|
|
||||||
- Add a read-only service projection containing:
|
|
||||||
- Document title and selected archival metadata.
|
|
||||||
- Authors resolved by the `author` semantic key.
|
|
||||||
- Notes.
|
|
||||||
- Ordered Sources with application media URLs and current transcription text.
|
|
||||||
- Ordered Job metadata.
|
|
||||||
- Load the projection with bounded queries and deterministic ordering.
|
|
||||||
- Use non-null `revised_text`, including an intentionally empty revision; otherwise fall back to `raw_transcription`.
|
|
||||||
- Map empty or whitespace-only current text to the explicit unavailable state without falling back past an intentional revision.
|
|
||||||
- Represent unavailable text and optional metadata explicitly.
|
|
||||||
- Do not expose semantic keys, direct file paths, full prompts, provider evidence, or raw API responses.
|
|
||||||
- Keep the projection independent of NiceGUI rendering so formatting tests can use plain typed values.
|
|
||||||
|
|
||||||
### 8. Build Print Preview and Styles
|
|
||||||
|
|
||||||
- Add a Print action to Document Detail.
|
|
||||||
- Open a dedicated persisted-Document print route with a Facsimile/Text-only format choice.
|
|
||||||
- Render the exact content order frozen in the scope.
|
|
||||||
- Render stored Notes and transcription as escaped text.
|
|
||||||
- For Text-only mode, normalize whitespace by joining single line breaks inside paragraphs while preserving blank-line paragraph boundaries.
|
|
||||||
- For Facsimile mode, preserve line breaks and use a two-column Source layout.
|
|
||||||
- Start each Facsimile Source on a new printed sheet with CSS page breaks.
|
|
||||||
- Allow long transcription content to continue rather than clipping it.
|
|
||||||
- Fetch images through an application-controlled Source media route.
|
|
||||||
- Add print-only CSS that hides navigation, controls, and non-document chrome.
|
|
||||||
- Invoke the browser print dialog only from an explicit user action.
|
|
||||||
- Add rendering tests for both modes, missing data, long text, special characters, image URLs, and page ordering.
|
|
||||||
|
|
||||||
### 9. Align Documentation and Verification
|
|
||||||
|
|
||||||
- Update V4 architecture, requirements, schema, and Document UI contracts to reflect:
|
|
||||||
- UUID plus hidden semantic-key registries.
|
|
||||||
- Built-in protection.
|
|
||||||
- One Person per Document.
|
|
||||||
- UUID-only role API writes.
|
|
||||||
- Atomic Document/link synchronization.
|
|
||||||
- Browser-native print projection and formats.
|
|
||||||
- Confirm every database, integration, and UI test target is isolated before execution.
|
|
||||||
- Run focused registry and service tests first.
|
|
||||||
- Run schema tests only through the destructive-test wrapper when they are potentially destructive.
|
|
||||||
- Run Linked People UI and print rendering tests against isolated fixtures.
|
|
||||||
- Run the broader non-external regression suite after focused coverage passes.
|
|
||||||
- Verify that `data/transcription.db` was not changed by test execution.
|
|
||||||
|
|
||||||
## Delivery Order
|
|
||||||
|
|
||||||
1. Registry and clean-schema contracts.
|
|
||||||
2. Registry services, API changes, and Settings tables.
|
|
||||||
3. Atomic Document/link service commands.
|
|
||||||
4. Shared staged Linked People editor.
|
|
||||||
5. Print projection.
|
|
||||||
6. Print preview and styles.
|
|
||||||
7. Documentation alignment and regression verification.
|
|
||||||
|
|
||||||
## Done Criteria
|
|
||||||
|
|
||||||
- All V4.4 acceptance criteria are implemented and testable.
|
|
||||||
- UI and API contracts use UUID identity and never expose semantic keys.
|
|
||||||
- Built-in registries retain meaning after relabeling and cannot be deleted.
|
|
||||||
- Custom registries retain reference-aware deletion.
|
|
||||||
- Both Document forms use one Linked People table.
|
|
||||||
- One Person cannot be linked twice to the same Document.
|
|
||||||
- Main Document saves are atomic across fields and relationships.
|
|
||||||
- Print preview provides both frozen formats and content sections.
|
|
||||||
- Print output uses current human-preferred text, deterministic ordering, escaped content, and application media URLs.
|
|
||||||
- Job metadata lists every Job oldest-to-newest and ends with Status.
|
|
||||||
- Source page reordering and server-generated PDFs are not introduced.
|
|
||||||
- Verification uses isolated data and does not modify `data/transcription.db`.
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [V4.4 Scope Boundary](scope_boundary_v4_4.md)
|
|
||||||
- [V4.3 Scope Boundary](../ver4.3/scope_boundary_v4_3.md)
|
|
||||||
- [V4.3 Implementation Plan](../ver4.3/implementation_plan_v4_3.md)
|
|
||||||
- [V4 Architecture](../ver4/architecture_v4.md)
|
|
||||||
- [V4 Schema](../ver4/schema_v4.md)
|
|
||||||
- [V4 Requirements](../ver4/requirements_v4.md)
|
|
||||||
- [V4 Error Handling Policy](../ver4/error_handling_v4.md)
|
|
||||||
@@ -1,232 +0,0 @@
|
|||||||
# V4.4 Scope Boundary
|
|
||||||
|
|
||||||
This document defines the frozen boundary for the semantic-registry, linked-people, and document-printing revision that follows the completed V4.3 Settings work. V4 through V4.3 remain the architecture and behavioral baseline except where this document explicitly replaces a registry or document-person contract.
|
|
||||||
|
|
||||||
## Purpose
|
|
||||||
|
|
||||||
- Keep registry identifiers stable without exposing duplicate machine codes in Settings tables or selectors.
|
|
||||||
- Replace role-specific person selectors with one coherent Linked People editor.
|
|
||||||
- Provide an archival print view containing document metadata, source pages, current transcription text, and transcription-job metadata.
|
|
||||||
|
|
||||||
## In Scope
|
|
||||||
|
|
||||||
### 1. Semantic Registry Identity
|
|
||||||
|
|
||||||
- `DocumentType` and `PersonRole` use UUIDs as their canonical record and relationship identity.
|
|
||||||
- Both registries may carry a nullable, unique, immutable `semantic_key` used only for application-defined built-ins.
|
|
||||||
- Semantic keys are internal implementation details. Settings tables, selectors, and public API payloads do not display or accept them.
|
|
||||||
- Labels are trimmed, case-insensitively unique, editable, and used for all user-facing display.
|
|
||||||
- Active state remains editable. Inactive entries remain valid for historical records and are excluded from default assignment selectors.
|
|
||||||
- Built-in status is derived from the presence of a semantic key and is displayed as a read-only Yes/No value.
|
|
||||||
- Built-in entries cannot be deleted or converted to custom entries.
|
|
||||||
- Custom entries have no semantic key and may be deleted only when unreferenced.
|
|
||||||
- Users may create custom entries but cannot create, change, or assign semantic keys through the UI or API.
|
|
||||||
|
|
||||||
### 2. Built-In Document Types
|
|
||||||
|
|
||||||
- Seed these built-in semantic keys and initial labels:
|
|
||||||
|
|
||||||
| Semantic key | Initial label |
|
|
||||||
| --- | --- |
|
|
||||||
| `book` | Book |
|
|
||||||
| `letter` | Letter |
|
|
||||||
| `postcard` | Postcard |
|
|
||||||
| `photo` | Photo |
|
|
||||||
| `journal` | Journal |
|
|
||||||
| `form` | Form |
|
|
||||||
|
|
||||||
- The Document Types Settings table contains Select, Label, Documents, Active, and Built-in columns.
|
|
||||||
- Document Types are ordered alphabetically by normalized label.
|
|
||||||
- Add, Edit, and Delete actions operate on table selection.
|
|
||||||
- Add creates a custom type. Edit changes only label and active state.
|
|
||||||
- The Documents count is the number of Documents referencing the type.
|
|
||||||
- Document Type selectors display labels only and submit UUIDs.
|
|
||||||
|
|
||||||
### 3. Built-In Person Roles
|
|
||||||
|
|
||||||
- Seed these built-in semantic keys and initial labels:
|
|
||||||
|
|
||||||
| Semantic key | Initial label |
|
|
||||||
| --- | --- |
|
|
||||||
| `author` | Author |
|
|
||||||
| `recipient` | Recipient |
|
|
||||||
| `mentioned` | Mentioned |
|
|
||||||
|
|
||||||
- Application behavior that requires authorship resolves the built-in `author` semantic key rather than matching a mutable label.
|
|
||||||
- The Person Roles Settings table contains Select, Label, Links, Active, and Built-in columns.
|
|
||||||
- Person Roles are ordered alphabetically by normalized label.
|
|
||||||
- Add, Edit, and Delete actions operate on table selection.
|
|
||||||
- Add creates a custom role. Edit changes only label and active state.
|
|
||||||
- The Links count is the number of document-person relationships referencing the role.
|
|
||||||
- Person Role selectors display labels only and submit UUIDs.
|
|
||||||
|
|
||||||
### 4. Linked People Editor
|
|
||||||
|
|
||||||
- Replace the separate role-specific person selectors on both Create Document and Edit Document with one Linked People table.
|
|
||||||
- The table contains Select, Person, and Role columns.
|
|
||||||
- Add opens an inline editor beneath the table with Person and Person Role selectors.
|
|
||||||
- Edit requires exactly one selected row and loads it into the inline editor.
|
|
||||||
- Save stages the inline addition or edit in the table.
|
|
||||||
- Cancel exits the inline editor without changing the staged link set.
|
|
||||||
- Delete stages removal of one or more selected rows.
|
|
||||||
- A Person may be linked to a Document only once, regardless of role.
|
|
||||||
- Every link has exactly one Person Role.
|
|
||||||
- Already-linked People are unavailable when adding another row.
|
|
||||||
- Existing links using inactive roles remain visible and unchanged unless explicitly edited.
|
|
||||||
- Only active roles are available for new links or role changes.
|
|
||||||
- Create Document preserves the existing `person_id` preselection workflow by staging that Person with the active built-in `author` role. An invalid Person or unavailable Author role produces a warning rather than an invalid link.
|
|
||||||
- Create Document preserves the existing `return_to=jobs_new` success path.
|
|
||||||
- Linked People changes remain staged until the main Create Document or Save Changes action.
|
|
||||||
- The Document and its complete staged link set are persisted atomically. A conflict or validation failure leaves both unchanged.
|
|
||||||
- The API and service contracts identify roles by `role_id`; role-code selectors and compatibility role strings are removed.
|
|
||||||
- Persistence enforces uniqueness on `(document_id, person_id)`.
|
|
||||||
|
|
||||||
### 5. Document Print View
|
|
||||||
|
|
||||||
- Add a Print action to Document Detail.
|
|
||||||
- The action opens a dedicated print-preview page for the persisted Document.
|
|
||||||
- The preview offers two formats:
|
|
||||||
- **Facsimile:** source image on the left and current transcription on the right. Original transcription line breaks are preserved, and each Source begins on a new printed sheet.
|
|
||||||
- **Text only:** no source images. Single line breaks inside a paragraph are reflowed as spaces, while blank-line paragraph boundaries remain.
|
|
||||||
- Both formats use browser printing through a dedicated print stylesheet and the browser print dialog.
|
|
||||||
- Server-generated PDF files are not part of V4.4; users may select the browser's Save as PDF destination.
|
|
||||||
- Sources are ordered by existing `page_number`, with UUID as a deterministic tie-breaker.
|
|
||||||
- The current transcription for each Source is the non-null `revised_text`, including an intentionally empty revision, otherwise the latest successful machine-output projection in `raw_transcription`.
|
|
||||||
- Empty or whitespace-only current text displays the explicit unavailable message rather than falling back past an intentional revision.
|
|
||||||
- A Source with no current transcription displays an explicit unavailable message.
|
|
||||||
- Transcription and Notes content is treated as text and escaped; model output is not executed as arbitrary HTML.
|
|
||||||
- Facsimile images use an application-controlled Source media route. Generated markup does not expose direct machine-local file paths.
|
|
||||||
|
|
||||||
### 6. Printed Content Contract
|
|
||||||
|
|
||||||
The print view contains, in this order:
|
|
||||||
|
|
||||||
1. Document title using the Document name.
|
|
||||||
2. Archival Metadata table:
|
|
||||||
- Author, containing People linked through the built-in `author` role.
|
|
||||||
- Date.
|
|
||||||
- Location Created.
|
|
||||||
- Archival Identifier.
|
|
||||||
3. Notes.
|
|
||||||
4. Document section containing one numbered section per Source.
|
|
||||||
5. Transcription Job Metadata table.
|
|
||||||
|
|
||||||
Empty metadata values remain visible as `Not set`. Empty Notes display `No notes recorded`.
|
|
||||||
|
|
||||||
The job metadata table:
|
|
||||||
|
|
||||||
- Lists field names in the first column and adds one column for every Job associated with the Document.
|
|
||||||
- Orders Job columns from oldest to newest by creation date, then UUID.
|
|
||||||
- Includes every Job status: `queued`, `processing`, `transcribed`, `completed`, `partial_success`, and `failed`.
|
|
||||||
- Contains these rows in order:
|
|
||||||
- Job ID.
|
|
||||||
- Date, using the Job creation/submission timestamp with timezone.
|
|
||||||
- Provider.
|
|
||||||
- Model.
|
|
||||||
- Prompt, using the frozen prompt filename/name rather than full prompt content.
|
|
||||||
- Retry Count.
|
|
||||||
- Status as the final row.
|
|
||||||
- Displays `Not set` for unavailable optional metadata.
|
|
||||||
|
|
||||||
### 7. Clean Development Schema
|
|
||||||
|
|
||||||
- V4.4 does not require preservation or migration of rows in the operator-configured development database.
|
|
||||||
- Implementation may recreate the configured development database, including `data/transcription.db` when it is the explicitly selected target, only through a separate operator-confirmed action that identifies the resolved path. Startup and test execution never delete it automatically.
|
|
||||||
- Fresh schema creation seeds the agreed built-in Document Types and Person Roles idempotently.
|
|
||||||
- No test may use, modify, replace, or restore `data/transcription.db`.
|
|
||||||
- Database, integration, and UI tests use confirmed isolated databases.
|
|
||||||
- Potentially destructive tests run only through `tools/run_destructive_tests.py`.
|
|
||||||
|
|
||||||
## Out of Scope
|
|
||||||
|
|
||||||
- User creation, editing, deletion, or direct display of semantic keys.
|
|
||||||
- Treating custom registry entries as built-ins.
|
|
||||||
- Additional built-in Document Types or Person Roles beyond the frozen lists.
|
|
||||||
- Assigning more than one role to the same Person on the same Document.
|
|
||||||
- Preserving multiple historical links that violate the new one-person-per-document constraint.
|
|
||||||
- Source page renumbering or reordering.
|
|
||||||
- Printing unsaved Create/Edit Document state.
|
|
||||||
- Print actions on Job Detail or other pages.
|
|
||||||
- Batch printing multiple Documents.
|
|
||||||
- Server-side PDF generation or PDF file storage.
|
|
||||||
- Markdown, DOCX, or evidence-package export through the print feature.
|
|
||||||
- User-editable print templates, fonts, margins, headers, or footers.
|
|
||||||
- Full frozen prompt content, prompt hashes, transport evidence, API responses, or execution-attempt details in the print footer.
|
|
||||||
- Rendering transcription text as unrestricted Markdown or HTML.
|
|
||||||
- Pixel-identical pagination across browsers and printer drivers.
|
|
||||||
|
|
||||||
## Locked Design Decisions
|
|
||||||
|
|
||||||
### A. UUID Identifies the Row; Semantic Key Identifies Built-In Meaning
|
|
||||||
|
|
||||||
- UUIDs remain the only relationship and API identity.
|
|
||||||
- A hidden semantic key permits reliable built-in behavior after a label is renamed.
|
|
||||||
- Mutable labels are never used to infer built-in meaning.
|
|
||||||
|
|
||||||
### B. Built-Ins Are Protected but Mutable in Presentation
|
|
||||||
|
|
||||||
- Built-in labels and active state may change.
|
|
||||||
- Built-in semantic identity cannot change, and built-ins cannot be deleted.
|
|
||||||
- Custom entries remain reference-aware and deletable when unreferenced.
|
|
||||||
|
|
||||||
### C. Linked People Is a Single-Role Relationship
|
|
||||||
|
|
||||||
- One `(document_id, person_id)` row represents the complete relationship.
|
|
||||||
- Changing a role updates that row rather than adding another relationship.
|
|
||||||
- The main Document save owns one atomic Document-and-links transaction.
|
|
||||||
|
|
||||||
### D. Printing Uses the Current Human-Preferred Text
|
|
||||||
|
|
||||||
- Human-revised text takes precedence over the latest successful machine-output projection.
|
|
||||||
- Job metadata provides processing context but does not claim that a later human revision is raw output from a listed Job.
|
|
||||||
|
|
||||||
### E. Printing Is Browser-Native
|
|
||||||
|
|
||||||
- A print-specific HTML view and CSS support physical printing and browser Save as PDF.
|
|
||||||
- Source media is served through application-controlled routes, and all textual content is escaped.
|
|
||||||
|
|
||||||
### F. Source Order Is Read-Only in V4.4
|
|
||||||
|
|
||||||
- Print order follows existing page numbers.
|
|
||||||
- Source page reordering remains explicitly excluded.
|
|
||||||
|
|
||||||
## Acceptance Criteria
|
|
||||||
|
|
||||||
1. Registry selectors and Settings forms never display a machine code or semantic key.
|
|
||||||
2. Document Types and Person Roles use UUID relationship identity and case-insensitively unique labels.
|
|
||||||
3. The six Document Type and three Person Role built-ins are seeded with immutable internal semantic keys.
|
|
||||||
4. Built-ins may be relabeled or disabled but cannot be deleted.
|
|
||||||
5. Unreferenced custom entries may be deleted; referenced custom entries may only be relabeled or disabled.
|
|
||||||
6. Settings tables show the agreed columns, alphabetical label order, usage counts, and selection-based actions.
|
|
||||||
7. Create and Edit Document use one Linked People table with inline staged Add/Edit/Save/Cancel and multi-row Delete.
|
|
||||||
8. The same Person cannot be staged or persisted twice for one Document, even under different roles.
|
|
||||||
9. Document fields and Linked People changes commit atomically.
|
|
||||||
10. Historical inactive roles remain displayable, while only active roles are assignable.
|
|
||||||
11. Document Detail opens a print preview with Facsimile and Text-only formats.
|
|
||||||
12. Print pages use current revised text when available and deterministic Source ordering.
|
|
||||||
13. Printed archival metadata resolves authors through the hidden `author` semantic key after any label change.
|
|
||||||
14. The job table contains one oldest-to-newest column per Job and ends with the Status row.
|
|
||||||
15. Print output escapes stored text and does not disclose direct local source paths.
|
|
||||||
16. Source reordering, server PDF generation, and print-template editing are absent.
|
|
||||||
17. Database, integration, and UI verification uses isolated test data and never modifies `data/transcription.db`.
|
|
||||||
|
|
||||||
## Scope Freeze Gate
|
|
||||||
|
|
||||||
V4.4 is sufficiently frozen to begin implementation:
|
|
||||||
|
|
||||||
- Built-in registry identity, membership, lifecycle, display, and selector behavior are resolved.
|
|
||||||
- Linked People selection, editing, uniqueness, inactive-role, staging, and transaction behavior are resolved.
|
|
||||||
- Print entry point, formats, content order, transcription precedence, page order, job metadata, and output mechanism are resolved.
|
|
||||||
- Clean development-schema and destructive-test boundaries are resolved.
|
|
||||||
- Source page reordering remains excluded.
|
|
||||||
|
|
||||||
Any expansion of the built-in catalogs, relationship cardinality, print formats, export formats, or print customization requires an explicit V4.4 scope amendment or a later revision.
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [V4.4 Implementation Plan](implementation_plan_v4_4.md)
|
|
||||||
- [V4.3 Scope Boundary](../ver4.3/scope_boundary_v4_3.md)
|
|
||||||
- [V4.3 Implementation Plan](../ver4.3/implementation_plan_v4_3.md)
|
|
||||||
- [V4 Architecture](../ver4/architecture_v4.md)
|
|
||||||
- [V4 Schema](../ver4/schema_v4.md)
|
|
||||||
- [V4 Requirements](../ver4/requirements_v4.md)
|
|
||||||
@@ -1,197 +0,0 @@
|
|||||||
# System Architecture (Version 4)
|
|
||||||
|
|
||||||
This document describes the production architecture of the document transcription system.
|
|
||||||
|
|
||||||
## Architecture Objectives
|
|
||||||
|
|
||||||
- Preserve original source material, per-execution machine output, and separate human revision.
|
|
||||||
- Support batching one or more images into ordered multi-page documents.
|
|
||||||
- Capture submission-time prompt provenance and a per-page OpenRouter SDK response snapshot.
|
|
||||||
- Execute page transcription concurrently with bounded `asyncio` workers.
|
|
||||||
- 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.
|
|
||||||
|
|
||||||
## Core Capabilities
|
|
||||||
|
|
||||||
- Ingest one or more images into sequential `Source` pages under a `Document`.
|
|
||||||
- Execute asynchronous vision transcription with bounded worker concurrency.
|
|
||||||
- Preserve original source files with SHA-256 digests and byte sizes.
|
|
||||||
- Freeze prompt text, prompt hash, model, and explicitly configured sampling parameters on each `Job`.
|
|
||||||
- Preserve page-level machine output, normalized metadata, and an SDK-serialized OpenRouter response snapshot on `JobSource`.
|
|
||||||
- Organize historical `Person` records through many-to-many Document relationships and extensible roles.
|
|
||||||
- Classify Documents through a UUID-identified registry with unique labels.
|
|
||||||
- Maintain human revision separately from machine-generated text.
|
|
||||||
- Isolate page failures so multi-page jobs can complete with partial success.
|
|
||||||
- Operate across supported platforms through Python-based application and maintenance tooling.
|
|
||||||
|
|
||||||
V4.2 extends this baseline with immutable execution attempts, exact OpenRouter transport evidence, safe
|
|
||||||
versioned exports, and provider-neutral derived-artifact provenance. `JobSource` remains the mutable queue and
|
|
||||||
compatibility projection; `ExecutionAttempt` is the authoritative append-only processing history. See the
|
|
||||||
[V4.2 Scope Boundary](../ver4.2/scope_boundary_v4_2.md).
|
|
||||||
|
|
||||||
## Technical Stack
|
|
||||||
|
|
||||||
- **Runtime:** Python 3.12 or later.
|
|
||||||
- **Web application:** FastAPI and NiceGUI.
|
|
||||||
- **Persistence:** SQLModel and SQLAlchemy, with SQLite and PostgreSQL support.
|
|
||||||
- **Validation and settings:** Pydantic V2 and pydantic-settings.
|
|
||||||
- **Concurrency:** Python `asyncio` workers.
|
|
||||||
- **Vision integration:** OpenRouter through the application's provider adapter.
|
|
||||||
- **Testing and quality:** pytest, pytest-asyncio, Ruff, and ty.
|
|
||||||
|
|
||||||
## Runtime Topology
|
|
||||||
|
|
||||||
The runtime operates as an asynchronous Python application:
|
|
||||||
|
|
||||||
- FastAPI + NiceGUI web application process.
|
|
||||||
- In-process `asyncio` worker 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.py`
|
|
||||||
- `src/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.py`
|
|
||||||
- `src/transcription/services/documents.py`
|
|
||||||
- `src/transcription/services/sources.py`
|
|
||||||
- `src/transcription/services/jobs.py`
|
|
||||||
- `src/transcription/services/people.py`
|
|
||||||
- `src/transcription/services/workflows.py`
|
|
||||||
|
|
||||||
Responsibilities:
|
|
||||||
|
|
||||||
- Keep one primary service boundary per aggregate: Documents, Sources, Jobs, and People.
|
|
||||||
- Documents own document records and the document-type registry.
|
|
||||||
- Sources own source records, revisions, source media formats, MIME resolution, and page execution evidence.
|
|
||||||
- Jobs own job lifecycle state and transitions.
|
|
||||||
- People own person records, relationship roles, document-person links, and portrait media.
|
|
||||||
- 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 by UUID.
|
|
||||||
|
|
||||||
### Source Media Policy
|
|
||||||
|
|
||||||
- `services/sources.py` is the single authority for accepted Source extensions and canonical MIME types.
|
|
||||||
- Storage and provider payload loading must call the same Source validation functions.
|
|
||||||
- Supported Source formats are JPEG, PNG, TIFF, and PDF.
|
|
||||||
- Upload is an interface action, not a domain aggregate. Service names, errors, and workflow variables use
|
|
||||||
`Source` terminology; compatibility aliases may remain temporarily at old import boundaries.
|
|
||||||
|
|
||||||
### 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
|
|
||||||
|
|
||||||
1. User uploads one or more images for a `Document`.
|
|
||||||
2. System stores files, hashes them, creates ordered `Source` rows, and creates a `Job`.
|
|
||||||
3. Worker claims the job, marks it `processing`, and executes page calls concurrently.
|
|
||||||
4. Each provider call appends an `ExecutionAttempt` with its request manifest, transport evidence, SDK snapshot,
|
|
||||||
normalized metadata, timing, and outcome.
|
|
||||||
5. The linked `JobSource` is updated as a compatibility projection, and a successful attempt updates the
|
|
||||||
`Source.raw_transcription` latest-success projection.
|
|
||||||
6. Aggregate status becomes `completed`, `partial_success`, or `failed`.
|
|
||||||
|
|
||||||
### 2. Document-Person Relationship Management
|
|
||||||
|
|
||||||
1. User opens a document or person edit flow.
|
|
||||||
2. UI loads existing links grouped by role.
|
|
||||||
3. User adds or removes people within one or more roles.
|
|
||||||
4. Service computes add/remove deltas rather than replacing all links blindly.
|
|
||||||
5. Conflict checks enforce uniqueness and deterministic write semantics before persistence commits.
|
|
||||||
|
|
||||||
### 3. Document Type Management
|
|
||||||
|
|
||||||
1. User selects a registry-backed document type for a document.
|
|
||||||
2. Service resolves the Document Type UUID.
|
|
||||||
3. Persistence stores the `document_type_id` reference.
|
|
||||||
4. Inactive types remain valid for historical rows but are excluded from default selectors.
|
|
||||||
|
|
||||||
## V4 Domain Rules
|
|
||||||
|
|
||||||
- `JobSource.raw_transcription` preserves page output for its Job execution.
|
|
||||||
- `Source.raw_transcription` is the latest-success machine-output projection for a page.
|
|
||||||
- Human corrections occur only in `Source.revised_text`.
|
|
||||||
- Prompt and parameter provenance is frozen on `Job` at submission time.
|
|
||||||
- The SDK-serialized OpenRouter response snapshot is stored on `JobSource` for each successful page execution.
|
|
||||||
- Every V4.2 provider call appends a distinct `ExecutionAttempt`; retries never rewrite earlier attempts.
|
|
||||||
- Exact response bytes identify the OpenRouter HTTP boundary and are not labeled as native upstream-provider JSON.
|
|
||||||
- Generic `ProcessingArtifact` records use versioned schemas, digests, and one inline or external content location.
|
|
||||||
- `DocumentPerson` links are unique for `(document_id, person_id, role_id)`.
|
|
||||||
- Relationship mutations are deterministic and set-based.
|
|
||||||
- `DocumentType.id` is canonical identity; its unique label may evolve.
|
|
||||||
|
|
||||||
## Data Model Summary
|
|
||||||
|
|
||||||
- `Document` has one `DocumentType`, many `Source` pages, many `Job` runs, and many `Person` records through `DocumentPerson`.
|
|
||||||
- `Source` belongs to one `Document` and may participate in many `JobSource` executions.
|
|
||||||
- `Job` has many `JobSource` rows.
|
|
||||||
- `PersonRole` defines available relationship roles.
|
|
||||||
|
|
||||||
## Test Strategy
|
|
||||||
|
|
||||||
- Unit tests for models, validation, hashing, and registry resolution.
|
|
||||||
- Service tests for CRUD, set-based sync, uniqueness conflicts, and deterministic relationship writes.
|
|
||||||
- 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.
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [System Overview](index_v4.md)
|
|
||||||
- [System Requirements](requirements_v4.md)
|
|
||||||
- [Data Model](schema_v4.md)
|
|
||||||
- [Error Handling Policy](error_handling_v4.md)
|
|
||||||
- [Error Handling Invariant](../invariant/error_handling.md)
|
|
||||||
- [Digital Evidence and AI Processing Provenance](../invariant/ai_evidence_and_provenance.md)
|
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
# Error Handling Policy (Version 4)
|
|
||||||
|
|
||||||
This document defines the Version 4 taxonomy, contracts, and framework behavior used to satisfy the cross-version [Error Handling invariant](../invariant/error_handling.md).
|
|
||||||
|
|
||||||
## Invariant Alignment
|
|
||||||
|
|
||||||
Version 4 implements the invariant through:
|
|
||||||
|
|
||||||
- The shared error taxonomy below.
|
|
||||||
- Structured error envelopes with correlation IDs.
|
|
||||||
- Page-level failure isolation and explicit aggregate job status.
|
|
||||||
- Atomic relationship and classification writes.
|
|
||||||
- Consistent translation across API, UI, service, worker, persistence, and provider boundaries.
|
|
||||||
- Bounded retry guidance based on category and idempotency.
|
|
||||||
|
|
||||||
## Scope and Authority
|
|
||||||
|
|
||||||
This policy governs error behavior across:
|
|
||||||
|
|
||||||
- NiceGUI pages
|
|
||||||
- FastAPI routes
|
|
||||||
- Service-layer orchestration
|
|
||||||
- `asyncio` worker tasks
|
|
||||||
- Database interactions
|
|
||||||
- Provider adapters
|
|
||||||
|
|
||||||
## Error Taxonomy
|
|
||||||
|
|
||||||
| Category | Definition | Retriable |
|
|
||||||
| --- | --- | --- |
|
|
||||||
| `validation_error` | Payload, parameter, or schema validation failure | no |
|
|
||||||
| `user_input_error` | Unacceptable file, invalid selection, or malformed request from the operator | no |
|
|
||||||
| `not_found_error` | Requested `Document`, `Source`, `Person`, `Job`, role, or type does not exist | no |
|
|
||||||
| `conflict_error` | Operation violates uniqueness or relationship-write policy | no |
|
|
||||||
| `external_provider_error` | Provider API failure, rate limit, or execution problem | yes |
|
|
||||||
| `infrastructure_transient_error` | Temporary DB, file-system, or network instability | yes |
|
|
||||||
| `infrastructure_persistent_error` | Persistent configuration, credential, or database availability failure | no |
|
|
||||||
| `internal_unexpected_error` | Uncaught exception or logic defect | no |
|
|
||||||
|
|
||||||
## Async Batch and Page-Level Error Behavior
|
|
||||||
|
|
||||||
In multi-page `asyncio` processing:
|
|
||||||
|
|
||||||
1. Exceptions from individual page calls are trapped within the page task wrapper.
|
|
||||||
2. Failed page detail is written to `JobSource.error_detail` and the page state becomes `failed`.
|
|
||||||
3. Aggregate job status is derived from page outcomes:
|
|
||||||
- all pages succeed -> `completed`
|
|
||||||
- some succeed and some fail -> `partial_success`
|
|
||||||
- all fail -> `failed`
|
|
||||||
4. Successful pages remain valid even when sister pages fail.
|
|
||||||
|
|
||||||
## Relationship and Classification Conflict Behavior
|
|
||||||
|
|
||||||
When relationship or document-type writes fail policy checks:
|
|
||||||
|
|
||||||
1. Reject the full write operation.
|
|
||||||
2. Return structured conflict detail including target identifiers and the violated rule.
|
|
||||||
3. Preserve existing persisted relationships unchanged.
|
|
||||||
|
|
||||||
## API Error Response Contract
|
|
||||||
|
|
||||||
API error responses return a structured envelope:
|
|
||||||
|
|
||||||
^^^json
|
|
||||||
{
|
|
||||||
"error_id": "err_uuid_12345",
|
|
||||||
"category": "conflict_error",
|
|
||||||
"message": "Relationship write conflicts with existing links.",
|
|
||||||
"suggestion": "Adjust the requested relationship links and retry.",
|
|
||||||
"details": {
|
|
||||||
"document_id": "...",
|
|
||||||
"person_id": "...",
|
|
||||||
"attempted_role": "recipient",
|
|
||||||
"operation": "add_link",
|
|
||||||
"conflict_reason": "duplicate document-person-role link"
|
|
||||||
},
|
|
||||||
"timestamp": "2026-08-10T15:00:00Z"
|
|
||||||
}
|
|
||||||
^^^
|
|
||||||
|
|
||||||
HTTP status mappings:
|
|
||||||
|
|
||||||
- `validation_error`, `user_input_error` -> `400`
|
|
||||||
- `not_found_error` -> `404`
|
|
||||||
- `conflict_error` -> `409`
|
|
||||||
- `external_provider_error` -> `502` or `503`
|
|
||||||
- `infrastructure_transient_error` -> `503`
|
|
||||||
- `infrastructure_persistent_error`, `internal_unexpected_error` -> `500`
|
|
||||||
|
|
||||||
## UI Error Presentation Rules
|
|
||||||
|
|
||||||
- Display concise failure summaries with the next action the operator can take.
|
|
||||||
- Keep form state in context when feasible.
|
|
||||||
- Distinguish validation issues, conflict issues, provider failures, and infrastructure failures.
|
|
||||||
- For bulk relationship updates, identify the specific role or person that caused a conflict.
|
|
||||||
|
|
||||||
## Logging and Audit Expectations
|
|
||||||
|
|
||||||
- Log worker failures with correlation IDs and provider context.
|
|
||||||
- Log relationship and classification conflicts with machine-readable detail.
|
|
||||||
- Log persisted provider errors and page-level execution failures.
|
|
||||||
|
|
||||||
## Retry Guidance
|
|
||||||
|
|
||||||
- Do not auto-retry validation or conflict failures.
|
|
||||||
- Permit user-driven retry after the input or selection changes.
|
|
||||||
- Allow bounded retry for transient provider or infrastructure failures when the operation is idempotent.
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [Error Handling Invariant](../invariant/error_handling.md)
|
|
||||||
- [System Overview](index_v4.md)
|
|
||||||
- [System Requirements](requirements_v4.md)
|
|
||||||
- [Data Model](schema_v4.md)
|
|
||||||
- [System Architecture](architecture_v4.md)
|
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
# Implementation Plan (Version 4)
|
|
||||||
|
|
||||||
## Goal
|
|
||||||
|
|
||||||
Implement the Version 4 project definition from the current repository state while preserving existing data by default.
|
|
||||||
|
|
||||||
## Migration Policy
|
|
||||||
|
|
||||||
- Database changes are non-destructive by default.
|
|
||||||
- Exception: the legacy `document_type` text field may be replaced by a `document_type_id` reference without migrating existing text values.
|
|
||||||
- Exception: `document_person` links may be recreated manually.
|
|
||||||
|
|
||||||
## Current Project Impact
|
|
||||||
|
|
||||||
- `src/transcription/db/models.py` requires full schema alignment with the V4 core documents.
|
|
||||||
- `src/transcription/services/documents.py` requires set-based document-person sync and document-type resolution.
|
|
||||||
- API modules require additive role-aware relationship behavior and document-type selection behavior.
|
|
||||||
- UI pages require grouped role displays, multi-role editing, and registry-backed document-type selection.
|
|
||||||
- Existing tests require updates for role enforcement, document-type selection, and regression safety.
|
|
||||||
|
|
||||||
## Implementation Phases
|
|
||||||
|
|
||||||
### 1. Finalize the Transition Documents
|
|
||||||
|
|
||||||
- Confirm the reset scope.
|
|
||||||
- Confirm the database exception policy.
|
|
||||||
- Keep core V4 documents as the only authoritative product definition.
|
|
||||||
|
|
||||||
### 2. Align the Persistence Layer
|
|
||||||
|
|
||||||
- Update SQLModel definitions to match the final V4 schema.
|
|
||||||
- Add `person_role` and `document_type` support.
|
|
||||||
- Replace legacy document-type storage with `document_type_id`.
|
|
||||||
- Apply the accepted manual exception strategy for `document_type` and `document_person` data.
|
|
||||||
- Preserve all other data structures non-destructively.
|
|
||||||
|
|
||||||
### 3. Update Services and Write Semantics
|
|
||||||
|
|
||||||
- Organize service ownership around Documents, Sources, Jobs, and People.
|
|
||||||
- Centralize Source extension and MIME policy in the Sources service.
|
|
||||||
- Treat upload as an interface action and remove it from domain service naming where compatibility permits.
|
|
||||||
- Implement set-based synchronization for document-person updates.
|
|
||||||
- Implement deterministic uniqueness and relationship-write conflict checks.
|
|
||||||
- Remove suggestion-related service behavior.
|
|
||||||
- Add document-type resolution and validation by UUID.
|
|
||||||
|
|
||||||
### 4. Update API Contracts
|
|
||||||
|
|
||||||
- Keep API evolution additive.
|
|
||||||
- Add role-aware relationship retrieval and write behavior.
|
|
||||||
- Add document-type catalog retrieval and UUID-based selection for document writes.
|
|
||||||
- Remove suggestion-related API surfaces from the V4 target state.
|
|
||||||
|
|
||||||
### 5. Update UI Workflows
|
|
||||||
|
|
||||||
- Replace single-person link editing with grouped multi-role editing.
|
|
||||||
- Render grouped role links on document and person detail views.
|
|
||||||
- Replace free-text document type entry with registry-backed selection.
|
|
||||||
- Preserve clear validation and conflict messaging.
|
|
||||||
|
|
||||||
### 6. Verification and Hardening
|
|
||||||
|
|
||||||
- Add or update service tests for many-per-role behavior, uniqueness conflict handling, and set-based sync correctness.
|
|
||||||
- Add API tests for relationship behavior and document-type selection.
|
|
||||||
- Add UI tests or walkthrough coverage for grouped roles and type selection.
|
|
||||||
- Add regression coverage for delete and cleanup semantics.
|
|
||||||
- Enforce backup-first test execution for AI-run unit tests: backup `./data` before tests, then always prompt for restore after successful tests.
|
|
||||||
- Keep restore confirmation-gated by default so code and test outcomes can be reviewed before data is reverted.
|
|
||||||
|
|
||||||
## Done When
|
|
||||||
|
|
||||||
- Core V4 documents and code paths agree on the final project definition.
|
|
||||||
- Relationship-role writes are deterministic and non-destructive.
|
|
||||||
- Relationship-write conflict rules are enforced consistently.
|
|
||||||
- Document type selection is registry-backed.
|
|
||||||
- The accepted manual exceptions for `document_type` and `document_person` are completed.
|
|
||||||
- The focused test coverage passes.
|
|
||||||
|
|
||||||
## Out of Scope
|
|
||||||
|
|
||||||
- Suggested/asserted relationship state.
|
|
||||||
- Suggestion review or extraction workflows.
|
|
||||||
- Global person entity-resolution engine.
|
|
||||||
- Automated semantic document-type classification.
|
|
||||||
|
|
||||||
## Delivery Order Recommendation
|
|
||||||
|
|
||||||
1. Freeze scope boundary and implementation plan.
|
|
||||||
2. Freeze core V4 documents.
|
|
||||||
3. Align persistence models.
|
|
||||||
4. Align services and API behavior.
|
|
||||||
5. Align UI behavior.
|
|
||||||
6. Run focused verification and regression checks.
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [V4 Scope Boundary](scope_boundary_v4.md)
|
|
||||||
- [System Overview](index_v4.md)
|
|
||||||
- [System Requirements](requirements_v4.md)
|
|
||||||
- [Data Model](schema_v4.md)
|
|
||||||
- [System Architecture](architecture_v4.md)
|
|
||||||
- [Error Handling Policy](error_handling_v4.md)
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
# Document Transcription System Overview (Version 4)
|
|
||||||
|
|
||||||
Version 4 is the architecture baseline for the personal-scale application used to transcribe, organize, and preserve historical documents, source images, and related people records.
|
|
||||||
|
|
||||||
## Recommended Reading Order
|
|
||||||
|
|
||||||
1. [System Architecture](architecture_v4.md) for capabilities, technical stack, runtime structure, workflows, and component ownership.
|
|
||||||
2. [System Requirements](requirements_v4.md) for the verifiable V4 contract.
|
|
||||||
3. [Data Model](schema_v4.md) for entities, relationships, constraints, and persistence rules.
|
|
||||||
4. [Error Handling Policy](error_handling_v4.md) for the V4 taxonomy and boundary contracts.
|
|
||||||
|
|
||||||
## Cross-Version Invariants
|
|
||||||
|
|
||||||
- [Historical Document Transcription Design Intent](../invariant/intent.md)
|
|
||||||
- [Transcription Methodology](../invariant/transcription_methodology.md)
|
|
||||||
- [Error Handling](../invariant/error_handling.md)
|
|
||||||
- [Digital Evidence and AI Processing Provenance](../invariant/ai_evidence_and_provenance.md)
|
|
||||||
- [UI Style Guide](../invariant/ui_style_guide.md)
|
|
||||||
|
|
||||||
## V4 Transition Documents
|
|
||||||
|
|
||||||
- [Scope Boundary](scope_boundary_v4.md)
|
|
||||||
- [Implementation Plan](implementation_plan_v4.md)
|
|
||||||
|
|
||||||
## Incremental Revisions
|
|
||||||
|
|
||||||
- [V4.1 Scope](../ver4.1/scope_boundary_v4_1.md) and [Implementation Plan](../ver4.1/implementation_plan_v4_1.md)
|
|
||||||
- [V4.2 Evidence and Provenance Scope](../ver4.2/scope_boundary_v4_2.md) and [Implementation Plan](../ver4.2/implementation_plan_v4_2.md)
|
|
||||||
- [V4.3 Settings Scope](../ver4.3/scope_boundary_v4_3.md) and [Implementation Plan](../ver4.3/implementation_plan_v4_3.md)
|
|
||||||
- [V4.4 Semantic Registries, Linked People, and Printing Scope](../ver4.4/scope_boundary_v4_4.md) and [Implementation Plan](../ver4.4/implementation_plan_v4_4.md)
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
# Document Transcription System Requirements (Version 4)
|
|
||||||
|
|
||||||
This document defines the baseline requirements for the document transcription system.
|
|
||||||
|
|
||||||
## Requirements Model
|
|
||||||
|
|
||||||
| ID | Category | Requirement | Verify Method |
|
|
||||||
| --- | --- | --- | --- |
|
|
||||||
| REQ-0 | System | Provide end-to-end multi-page document transcription with persistent, inspectable async job states. | demonstration |
|
|
||||||
| REQ-1 | Functional | Allow users to upload one or more images as ordered `Source` pages under a `Document`. | test |
|
|
||||||
| REQ-2 | Functional | Process page transcription asynchronously using an `asyncio` worker pool bounded by rate limits. | test |
|
|
||||||
| REQ-3 | Functional | Persist submission-time request provenance and accurately labeled page-level SDK evidence; V4.2 adds exact OpenRouter-boundary transport evidence for new attempts. | test |
|
|
||||||
| REQ-4 | Functional | Support job states `queued`, `processing`, `completed`, `partial_success`, and `failed`, plus page states `pending`, `transcribed`, and `failed`. | inspection |
|
|
||||||
| REQ-5 | Functional | Allow users to manage historical `Person` records and link multiple people per role to a `Document`. | test |
|
|
||||||
| REQ-6 | Functional | Support an extensible role taxonomy for document-person relationships. | inspection |
|
|
||||||
| REQ-7 | Policy Constraint | Enforce deterministic relationship-role writes with uniqueness on `(document_id, person_id, role_id)` and explicit conflict responses for invalid duplicate link attempts. | test |
|
|
||||||
| REQ-8 | Functional | Use set-based synchronization for document-person mutations so updates add and remove only the intended links. | test |
|
|
||||||
| REQ-9 | Functional | Maintain immutable machine output on `Source.raw_transcription` while permitting inline human edits on `Source.revised_text`. | test |
|
|
||||||
| REQ-10 | Functional | Support a UUID-identified `DocumentType` taxonomy with unique user-facing labels and active/inactive lifecycle control. | test |
|
|
||||||
| REQ-11 | Data Constraint | Store `Document` type as a controlled reference to `DocumentType`. | test |
|
|
||||||
| REQ-12 | Interface | Render multi-page transcriptions sequentially by `page_number` with document, people, and document-type metadata. | demonstration |
|
|
||||||
| REQ-13 | Interface | Document create/edit UI must support selecting multiple people per role and selecting an active document type from the registry. | demonstration |
|
|
||||||
| REQ-14 | API Constraint | Expose additive, role-aware retrieval and write behavior for document-person links and UUID-based selection for document types. | test |
|
|
||||||
| REQ-15 | Data Constraint | Calculate and store cryptographic file hashes (SHA-256) and file sizes for uploaded source images. | test |
|
|
||||||
| REQ-16 | Data Constraint | Preserve a portable relational model across supported backends using SQLModel, SQLAlchemy, SQLite, and PostgreSQL. | inspection |
|
|
||||||
| REQ-17 | Reliability | Ensure delete and update flows for documents, people, and relationship links remain deterministic and safe. | test |
|
|
||||||
| REQ-18 | Operations Constraint | Keep canonical development, testing, restore, and recovery workflows OS-independent; for AI-run unit tests, require a pre-test backup of `./data` and an always-shown post-success confirmation prompt before any restore action. | inspection |
|
|
||||||
| REQ-19 | Quality | Provide automated coverage for async transcription workflows, relationship-role enforcement, document-type selection, and regression behavior. | test |
|
|
||||||
|
|
||||||
## Clarifying Constraints
|
|
||||||
|
|
||||||
1. `DocumentType.id` is its sole identity; labels are unique ignoring case and surrounding whitespace.
|
|
||||||
2. `PersonRole.code` is a stable machine identifier; `PersonRole.label` may evolve.
|
|
||||||
3. Relationship-write policy and conflict handling must be consistent across UI, API, services, and persistence.
|
|
||||||
4. Many-per-role behavior is required for document-person links.
|
|
||||||
5. Relationship conflicts must fail deterministically without partial mutation.
|
|
||||||
|
|
||||||
## Element Satisfaction Mapping
|
|
||||||
|
|
||||||
- UI (NiceGUI): Satisfies REQ-0, REQ-1, REQ-5, REQ-9, REQ-12, REQ-13.
|
|
||||||
- API (FastAPI): Satisfies REQ-1, REQ-4, REQ-5, REQ-7, REQ-8, REQ-14.
|
|
||||||
- Worker (`asyncio`): Satisfies REQ-2, REQ-3, REQ-4.
|
|
||||||
- Persistence (SQLModel / SQLAlchemy): Satisfies REQ-3, REQ-9, REQ-10, REQ-11, REQ-15, REQ-16, REQ-17.
|
|
||||||
- Test Suite: Verifies all test-marked requirements and satisfies REQ-19.
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [System Overview](index_v4.md)
|
|
||||||
- [System Architecture](architecture_v4.md)
|
|
||||||
- [Data Model](schema_v4.md)
|
|
||||||
- [Error Handling Policy](error_handling_v4.md)
|
|
||||||
@@ -1,236 +0,0 @@
|
|||||||
# Database Schema (Version 4)
|
|
||||||
|
|
||||||
This document defines the relational schema for the document transcription system.
|
|
||||||
|
|
||||||
## Entity Relationship Diagram
|
|
||||||
|
|
||||||
```mermaid
|
|
||||||
erDiagram
|
|
||||||
DOCUMENT_TYPE {
|
|
||||||
UUID id PK
|
|
||||||
TEXT label
|
|
||||||
TEXT normalized_label
|
|
||||||
BOOLEAN is_active
|
|
||||||
TIMESTAMPTZ created_at
|
|
||||||
TIMESTAMPTZ updated_at
|
|
||||||
}
|
|
||||||
|
|
||||||
PERSON_ROLE {
|
|
||||||
UUID id PK
|
|
||||||
TEXT code
|
|
||||||
TEXT label
|
|
||||||
BOOLEAN is_active
|
|
||||||
TIMESTAMPTZ created_at
|
|
||||||
TIMESTAMPTZ updated_at
|
|
||||||
}
|
|
||||||
|
|
||||||
PERSON {
|
|
||||||
UUID id PK
|
|
||||||
TEXT full_name
|
|
||||||
TEXT display_name
|
|
||||||
TEXT maiden_name
|
|
||||||
DATE birth_date
|
|
||||||
TEXT birth_date_raw
|
|
||||||
TEXT birth_place
|
|
||||||
DATE death_date
|
|
||||||
TEXT death_date_raw
|
|
||||||
TEXT death_place
|
|
||||||
TEXT biography
|
|
||||||
TEXT portrait_path
|
|
||||||
JSONB metadata
|
|
||||||
TIMESTAMPTZ created_at
|
|
||||||
TIMESTAMPTZ updated_at
|
|
||||||
}
|
|
||||||
|
|
||||||
DOCUMENT {
|
|
||||||
UUID id PK
|
|
||||||
UUID document_type_id FK
|
|
||||||
TEXT name
|
|
||||||
DATE document_date
|
|
||||||
TEXT document_date_raw
|
|
||||||
TEXT location_created
|
|
||||||
TEXT notes
|
|
||||||
TEXT archive_identifier
|
|
||||||
TIMESTAMPTZ created_at
|
|
||||||
TIMESTAMPTZ updated_at
|
|
||||||
}
|
|
||||||
|
|
||||||
DOCUMENT_PERSON {
|
|
||||||
UUID id PK
|
|
||||||
UUID document_id FK
|
|
||||||
UUID person_id FK
|
|
||||||
UUID role_id FK
|
|
||||||
TIMESTAMPTZ created_at
|
|
||||||
TIMESTAMPTZ updated_at
|
|
||||||
}
|
|
||||||
|
|
||||||
JOB {
|
|
||||||
UUID id PK
|
|
||||||
UUID document_id FK
|
|
||||||
VARCHAR status
|
|
||||||
INTEGER retry_count
|
|
||||||
TEXT provider
|
|
||||||
TEXT model
|
|
||||||
TEXT prompt_name
|
|
||||||
TEXT prompt_hash
|
|
||||||
TEXT system_prompt
|
|
||||||
TEXT user_prompt
|
|
||||||
FLOAT temperature
|
|
||||||
FLOAT top_p
|
|
||||||
TIMESTAMPTZ date_created
|
|
||||||
TIMESTAMPTZ date_updated
|
|
||||||
}
|
|
||||||
|
|
||||||
SOURCE {
|
|
||||||
UUID id PK
|
|
||||||
UUID document_id FK
|
|
||||||
INTEGER page_number
|
|
||||||
TEXT upload_name
|
|
||||||
TEXT filename
|
|
||||||
TEXT file_path
|
|
||||||
TEXT file_hash
|
|
||||||
BIGINT file_size_bytes
|
|
||||||
TEXT raw_transcription
|
|
||||||
TEXT revised_text
|
|
||||||
TIMESTAMPTZ date_uploaded
|
|
||||||
TIMESTAMPTZ date_revised
|
|
||||||
}
|
|
||||||
|
|
||||||
JOB_SOURCE {
|
|
||||||
UUID id PK
|
|
||||||
UUID job_id FK
|
|
||||||
UUID source_id FK
|
|
||||||
VARCHAR status
|
|
||||||
TEXT raw_transcription
|
|
||||||
JSONB ai_metadata
|
|
||||||
JSONB raw_api_response
|
|
||||||
TEXT error_detail
|
|
||||||
TIMESTAMPTZ executed_at
|
|
||||||
}
|
|
||||||
|
|
||||||
EXECUTION_ATTEMPT {
|
|
||||||
UUID id PK
|
|
||||||
UUID job_source_id FK
|
|
||||||
UUID job_id FK
|
|
||||||
UUID source_id FK
|
|
||||||
INTEGER attempt_number
|
|
||||||
VARCHAR status
|
|
||||||
JSONB request_manifest
|
|
||||||
TEXT request_manifest_sha256
|
|
||||||
INTEGER transport_status_code
|
|
||||||
BINARY transport_body
|
|
||||||
JSONB transport_safe_headers
|
|
||||||
JSONB sdk_response_snapshot
|
|
||||||
JSONB normalized_metadata
|
|
||||||
JSONB software_context
|
|
||||||
TEXT raw_transcription
|
|
||||||
TEXT failure_phase
|
|
||||||
TIMESTAMPTZ started_at
|
|
||||||
TIMESTAMPTZ finished_at
|
|
||||||
INTEGER duration_ms
|
|
||||||
}
|
|
||||||
|
|
||||||
PROCESSING_ARTIFACT {
|
|
||||||
UUID id PK
|
|
||||||
UUID source_id FK
|
|
||||||
UUID execution_attempt_id FK
|
|
||||||
TEXT artifact_type
|
|
||||||
TEXT media_type
|
|
||||||
TEXT schema_name
|
|
||||||
TEXT schema_version
|
|
||||||
TEXT producer
|
|
||||||
TEXT producer_version
|
|
||||||
JSONB inline_payload
|
|
||||||
TEXT external_reference
|
|
||||||
TEXT payload_sha256
|
|
||||||
BIGINT byte_size
|
|
||||||
JSONB coordinate_metadata
|
|
||||||
TIMESTAMPTZ created_at
|
|
||||||
}
|
|
||||||
|
|
||||||
DOCUMENT_TYPE ||--o{ DOCUMENT : classifies
|
|
||||||
DOCUMENT ||--o{ DOCUMENT_PERSON : has_people
|
|
||||||
PERSON ||--o{ DOCUMENT_PERSON : appears_in
|
|
||||||
PERSON_ROLE ||--o{ DOCUMENT_PERSON : labels
|
|
||||||
DOCUMENT ||--o{ JOB : has_jobs
|
|
||||||
DOCUMENT ||--o{ SOURCE : contains_pages
|
|
||||||
JOB ||--o{ JOB_SOURCE : executes
|
|
||||||
SOURCE ||--o{ JOB_SOURCE : processed_in
|
|
||||||
JOB_SOURCE ||--o{ EXECUTION_ATTEMPT : projects
|
|
||||||
SOURCE ||--o{ PROCESSING_ARTIFACT : derives
|
|
||||||
EXECUTION_ATTEMPT ||--o{ PROCESSING_ARTIFACT : produces
|
|
||||||
```
|
|
||||||
|
|
||||||
## Domain Invariants and Provenance Rules
|
|
||||||
|
|
||||||
### Page-Level Execution and AI Outputs
|
|
||||||
|
|
||||||
- Every single page execution by an AI model produces a dedicated `JOB_SOURCE` record.
|
|
||||||
- Every `JOB` stores the frozen prompt identifier, prompt text, and hyperparameters used at submission time.
|
|
||||||
- `JOB_SOURCE.raw_api_response` is a compatibility projection containing an SDK-serialized OpenRouter response
|
|
||||||
snapshot. It is neither the exact HTTP body nor the native upstream-provider response.
|
|
||||||
- Every new provider call creates an immutable `EXECUTION_ATTEMPT` containing the frozen request manifest,
|
|
||||||
exact OpenRouter-boundary response bytes when received, safe transport metadata, SDK snapshot, normalized
|
|
||||||
metadata, timing, and outcome.
|
|
||||||
- `EXECUTION_ATTEMPT(job_id, source_id, attempt_number)` is unique; retries increment the persisted attempt number.
|
|
||||||
- Historical `JOB_SOURCE` rows without an `EXECUTION_ATTEMPT` remain SDK snapshots and are explicitly labeled as
|
|
||||||
lacking transport evidence.
|
|
||||||
- `SOURCE.raw_transcription` caches the latest successful machine output for that page.
|
|
||||||
|
|
||||||
### Generic Processing Artifacts
|
|
||||||
|
|
||||||
- `PROCESSING_ARTIFACT` stores provider-neutral versioned derived outputs.
|
|
||||||
- Exactly one of `inline_payload` and `external_reference` is populated.
|
|
||||||
- Externally stored artifacts use application-managed relative references and are verified by SHA-256 and byte size.
|
|
||||||
- Coordinate metadata declares units, origin, dimensions, and transformations when geometry is present.
|
|
||||||
|
|
||||||
### Image Storage and Integrity
|
|
||||||
|
|
||||||
- Binary images are stored on disk; `SOURCE.file_path` stores the persisted path.
|
|
||||||
- `SOURCE.file_hash` stores a SHA-256 digest.
|
|
||||||
- `SOURCE.file_size_bytes` stores the original file size.
|
|
||||||
|
|
||||||
### Page Ordering and Revisions
|
|
||||||
|
|
||||||
- `SOURCE.page_number` dictates page ordering within a document.
|
|
||||||
- `SOURCE.raw_transcription` remains immutable machine output.
|
|
||||||
- `SOURCE.revised_text` stores human edits and is the preferred display value when present.
|
|
||||||
|
|
||||||
### Document-Person Role Governance
|
|
||||||
|
|
||||||
- Documents support zero, one, or many people per relationship role.
|
|
||||||
- Relationship roles are defined by `PERSON_ROLE` rather than hardcoded columns.
|
|
||||||
- `DOCUMENT_PERSON` must be unique for `(document_id, person_id, role_id)`.
|
|
||||||
- Relationship writes must be deterministic and use explicit add/remove link intent.
|
|
||||||
|
|
||||||
### Document Type Governance
|
|
||||||
|
|
||||||
- Every document type is defined by `DOCUMENT_TYPE`.
|
|
||||||
- `DOCUMENT_TYPE.id` is the sole machine identity.
|
|
||||||
- `DOCUMENT_TYPE.label` is mutable display text and is unique after trimming and case normalization.
|
|
||||||
- `DOCUMENT_TYPE.normalized_label` stores the normalized uniqueness key.
|
|
||||||
- Inactive types remain valid for historical rows but should be excluded from default selection UIs.
|
|
||||||
|
|
||||||
## Constraint Summary
|
|
||||||
|
|
||||||
- `DOCUMENT_TYPE.normalized_label` is unique.
|
|
||||||
- `PERSON_ROLE.code` is unique.
|
|
||||||
- `DOCUMENT_PERSON(document_id, person_id, role_id)` is unique.
|
|
||||||
|
|
||||||
## Indexing Guidance
|
|
||||||
|
|
||||||
- `document(document_type_id)`
|
|
||||||
- `document_person(document_id)`
|
|
||||||
- `document_person(person_id)`
|
|
||||||
- `document_person(role_id)`
|
|
||||||
- `source(document_id, page_number)`
|
|
||||||
- `job(document_id, status)`
|
|
||||||
- `job_source(job_id)`
|
|
||||||
- `job_source(source_id)`
|
|
||||||
|
|
||||||
## Related Local References
|
|
||||||
|
|
||||||
- [System Overview](index_v4.md)
|
|
||||||
- [System Architecture](architecture_v4.md)
|
|
||||||
- [System Requirements](requirements_v4.md)
|
|
||||||
- [Error Handling Policy](error_handling_v4.md)
|
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
# V4 Scope Boundary
|
|
||||||
|
|
||||||
This document defines the scope for the transition from the current repository state to the Version 4 project definition.
|
|
||||||
|
|
||||||
## Purpose
|
|
||||||
|
|
||||||
Define what this revision includes, what it intentionally excludes, and what migration rules govern the transition work.
|
|
||||||
|
|
||||||
## In Scope
|
|
||||||
|
|
||||||
### 1. Relationship Model
|
|
||||||
|
|
||||||
- Extensible role taxonomy for document-person relationships.
|
|
||||||
- Many-to-many document-person links with many people per role.
|
|
||||||
- Set-based add/remove synchronization for document-person updates.
|
|
||||||
|
|
||||||
### 2. Document Type Governance
|
|
||||||
|
|
||||||
- Registry-driven `DocumentType` model with UUID identity, unique labels, and controlled selection.
|
|
||||||
- Minimal rollout for the current corpus with no alias helper table.
|
|
||||||
|
|
||||||
### 3. UI and API Behavior
|
|
||||||
|
|
||||||
- Grouped role links on document and person views.
|
|
||||||
- Multi-role relationship editing on document create/edit flows.
|
|
||||||
- Role-aware API retrieval and write behavior.
|
|
||||||
- Additive API evolution with explicit deprecations.
|
|
||||||
|
|
||||||
### 4. Verification
|
|
||||||
|
|
||||||
- Tests for many-per-role behavior.
|
|
||||||
- Tests for set-based relationship mutation behavior.
|
|
||||||
- Tests for document and person delete/link cleanup regressions.
|
|
||||||
|
|
||||||
## Out of Scope
|
|
||||||
|
|
||||||
- Suggested versus asserted relationship states.
|
|
||||||
- Suggestion storage, review, acceptance, or rejection workflows.
|
|
||||||
- Automatic relationship extraction or recommendation features.
|
|
||||||
- Full entity resolution or identity merge across all people.
|
|
||||||
- Automated semantic document type classification.
|
|
||||||
- Redesign of the core transcription execution model.
|
|
||||||
|
|
||||||
## Locked Design Decisions
|
|
||||||
|
|
||||||
### A. Role Extensibility Mechanism
|
|
||||||
|
|
||||||
- Use registry tables for relationship roles.
|
|
||||||
|
|
||||||
### B. API Compatibility Strategy
|
|
||||||
|
|
||||||
- Use additive API evolution.
|
|
||||||
- In development mode, the current revision is authoritative.
|
|
||||||
- Deprecations should be explicit and short-lived.
|
|
||||||
|
|
||||||
### C. Document Type Rollout Strategy
|
|
||||||
|
|
||||||
- Use a minimal registry rollout for the current corpus.
|
|
||||||
- Do not introduce a `document_type_alias` helper table.
|
|
||||||
|
|
||||||
### D. Database Change Policy
|
|
||||||
|
|
||||||
- Future schema changes are non-destructive by default.
|
|
||||||
- Exception: `document_type` text may be replaced by `document_type_id` without migrating the legacy text values.
|
|
||||||
- Exception: `document_person` links may be recreated manually.
|
|
||||||
|
|
||||||
## Compatibility and Rollout
|
|
||||||
|
|
||||||
- Preserve existing repository behavior where unaffected by the V4 scope.
|
|
||||||
- Treat scope boundary and implementation plan as the only transition documents.
|
|
||||||
- Treat core V4 documents as the authoritative project definition once rewritten.
|
|
||||||
|
|
||||||
## Exit Criteria for Scope Freeze
|
|
||||||
|
|
||||||
V4 scope is considered frozen when:
|
|
||||||
|
|
||||||
- Relationship model and document-type governance are approved.
|
|
||||||
- Relationship model and document-type governance are approved.
|
|
||||||
- Additive API change list and deprecation schedule are approved.
|
|
||||||
- Migration exceptions are explicitly acknowledged.
|
|
||||||
|
|
||||||
## Core V4 Documents
|
|
||||||
|
|
||||||
1. `docs/ver4/index_v4.md`
|
|
||||||
2. `docs/ver4/requirements_v4.md`
|
|
||||||
3. `docs/ver4/schema_v4.md`
|
|
||||||
4. `docs/ver4/architecture_v4.md`
|
|
||||||
5. `docs/ver4/error_handling_v4.md`
|
|
||||||
6. `docs/ver4/implementation_plan_v4.md`
|
|
||||||
@@ -6,9 +6,15 @@ Do not summarize. Do not paraphrase. Do not modernize style.
|
|||||||
|
|
||||||
## Output Contract
|
## Output Contract
|
||||||
- Return only the transcription text.
|
- Return only the transcription text.
|
||||||
|
- Begin with exactly one applicable body marker:
|
||||||
|
- `[document body handwritten]`
|
||||||
|
- `[document body typewritten]`
|
||||||
|
- `[document body typeset]`
|
||||||
|
- `[document body mixed]`
|
||||||
- Preserve original wording, punctuation, and meaningful structure.
|
- Preserve original wording, punctuation, and meaningful structure.
|
||||||
- Keep line/section flow readable while preserving intent and document organization.
|
- Keep line/section flow readable while preserving intent and document organization.
|
||||||
- Never invent missing content.
|
- Never invent missing content.
|
||||||
|
- Use ordinary plain-text characters rather than HTML entities.
|
||||||
|
|
||||||
## Rules for Ambiguous or Damaged Text
|
## Rules for Ambiguous or Damaged Text
|
||||||
|
|
||||||
@@ -46,16 +52,31 @@ Do not summarize. Do not paraphrase. Do not modernize style.
|
|||||||
- Signal location before the note text.
|
- Signal location before the note text.
|
||||||
- Example form: `[written in left margin: ...]`
|
- Example form: `[written in left margin: ...]`
|
||||||
|
|
||||||
### Printed and handwritten text
|
### Document body medium
|
||||||
|
- Use `[document body handwritten]` when the main body is written by hand.
|
||||||
|
- Use `[document body typewritten]` for mechanically typewritten pages. Uneven impressions,
|
||||||
|
monospaced characters, worn type, and other typewriter defects are not handwriting.
|
||||||
|
- Use `[document body typeset]` for printed pages composed with movable type or comparable
|
||||||
|
typesetting.
|
||||||
|
- Use `[document body mixed]` when substantial body content uses more than one medium, such
|
||||||
|
as a completed printed form.
|
||||||
- Preserve printed and handwritten text together in their original reading context.
|
- Preserve printed and handwritten text together in their original reading context.
|
||||||
- On mixed documents such as completed forms, leave printed labels and instructions unmarked.
|
- On mixed documents, leave printed labels and instructions unmarked and wrap only actual
|
||||||
- Wrap handwritten entries in `[handwritten: ...]`.
|
handwritten entries in `[handwritten: ...]`.
|
||||||
- Mark handwritten signatures as `[handwritten signature: ...]`.
|
- Mark handwritten signatures as `[handwritten signature: ...]`.
|
||||||
- If the main body is entirely handwritten, add `[document body handwritten]` once at the beginning rather than marking every line.
|
- If the main body is entirely handwritten, use its one body marker rather than wrapping
|
||||||
|
each line in `[handwritten: ...]`.
|
||||||
- Mark later notes or uncertain additions as `[handwritten annotation: ...]`.
|
- Mark later notes or uncertain additions as `[handwritten annotation: ...]`.
|
||||||
- When authorship is unclear, use `[handwritten annotation, author uncertain: ...]`.
|
- When authorship is unclear, use `[handwritten annotation, author uncertain: ...]`.
|
||||||
- Do not infer authorship, writing date, or whether different handwriting belongs to different people unless explicitly evident.
|
- Do not infer authorship, writing date, or whether different handwriting belongs to different people unless explicitly evident.
|
||||||
|
|
||||||
|
### Structured layouts
|
||||||
|
- Preserve tables of contents as associated title, dotted-leader, and page-reference rows.
|
||||||
|
- Preserve tables and forms in reading order while keeping labels associated with their values.
|
||||||
|
- Preserve columns in their evident reading order; do not interleave unrelated rows.
|
||||||
|
- Preserve captions with the visual element they describe.
|
||||||
|
- Preserve marginalia with its location marker and page numbers in their evident position.
|
||||||
|
|
||||||
### Line-break hyphenation
|
### Line-break hyphenation
|
||||||
- Rejoin words split across line breaks when they are clearly one word.
|
- Rejoin words split across line breaks when they are clearly one word.
|
||||||
- Remove only line-break hyphens used for wrapping.
|
- Remove only line-break hyphens used for wrapping.
|
||||||
@@ -80,4 +101,5 @@ Before finalizing, ensure:
|
|||||||
2. Uncertain/illegible areas are explicitly marked.
|
2. Uncertain/illegible areas are explicitly marked.
|
||||||
3. Crossed-out and inserted text are preserved with required tags.
|
3. Crossed-out and inserted text are preserved with required tags.
|
||||||
4. Structure/ordering is preserved as faithfully as possible.
|
4. Structure/ordering is preserved as faithfully as possible.
|
||||||
5. Handwriting is identified using the mixed-text conventions without separating it from its printed context.
|
5. Exactly one document-body marker appears, and localized handwriting markers are used only where applicable.
|
||||||
|
6. Tables, forms, columns, captions, marginalia, dotted leaders, and page references retain their associations.
|
||||||
|
|||||||
@@ -15,11 +15,17 @@ dependencies = [
|
|||||||
"aiosqlite>=0.21.0",
|
"aiosqlite>=0.21.0",
|
||||||
"asyncpg>=0.31.0",
|
"asyncpg>=0.31.0",
|
||||||
"fastapi>=0.138.0",
|
"fastapi>=0.138.0",
|
||||||
|
# Exact pin, deliberate. NiceGUI 3.x minor releases ship Quasar/Vue changes that
|
||||||
|
# break component props and styling, and tests/ui/ cannot detect visual regressions.
|
||||||
|
# Hold through the current release stabilization; revisit as a scheduled upgrade.
|
||||||
|
# See docs/production-runbook.md, "Dependency upgrade policy".
|
||||||
"nicegui==3.13.0",
|
"nicegui==3.13.0",
|
||||||
"openrouter>=0.7.0",
|
"openrouter>=0.7.0",
|
||||||
|
"pillow>=10.0.0",
|
||||||
"psycopg2-binary>=2.9.12",
|
"psycopg2-binary>=2.9.12",
|
||||||
"pydantic>=2.13.4",
|
"pydantic>=2.13.4",
|
||||||
"pydantic-settings>=2.9.1",
|
"pydantic-settings>=2.9.1",
|
||||||
|
"python-gedcom>=1.1.0",
|
||||||
"sqlmodel>=0.0.25",
|
"sqlmodel>=0.0.25",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -40,6 +46,7 @@ dev = [
|
|||||||
[tool.pytest.ini_options]
|
[tool.pytest.ini_options]
|
||||||
addopts = "--strict-markers -q"
|
addopts = "--strict-markers -q"
|
||||||
asyncio_mode = "strict"
|
asyncio_mode = "strict"
|
||||||
|
asyncio_default_fixture_loop_scope = "function"
|
||||||
filterwarnings = [
|
filterwarnings = [
|
||||||
"error:coroutine .* was never awaited:RuntimeWarning",
|
"error:coroutine .* was never awaited:RuntimeWarning",
|
||||||
]
|
]
|
||||||
|
|||||||
Binary file not shown.
@@ -26,6 +26,7 @@ extend-select = [
|
|||||||
"E", "W", # https://docs.astral.sh/ruff/rules/#pycodestyle-e-w
|
"E", "W", # https://docs.astral.sh/ruff/rules/#pycodestyle-e-w
|
||||||
"F", # https://docs.astral.sh/ruff/rules/#pyflakes-f
|
"F", # https://docs.astral.sh/ruff/rules/#pyflakes-f
|
||||||
"FURB", # https://docs.astral.sh/ruff/rules/#refurb-furb
|
"FURB", # https://docs.astral.sh/ruff/rules/#refurb-furb
|
||||||
|
"G", # https://docs.astral.sh/ruff/rules/#flake8-logging-format-g
|
||||||
"I", # https://docs.astral.sh/ruff/rules/#isort-i
|
"I", # https://docs.astral.sh/ruff/rules/#isort-i
|
||||||
"N", # https://docs.astral.sh/ruff/rules/#pep8-naming-n
|
"N", # https://docs.astral.sh/ruff/rules/#pep8-naming-n
|
||||||
"PD", # https://docs.astral.sh/ruff/rules/#pandas-vet-pd
|
"PD", # https://docs.astral.sh/ruff/rules/#pandas-vet-pd
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""Additive V4 API routes for relationship and classification registries."""
|
"""API routes for relationship and classification registries."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -12,7 +12,6 @@ from fastapi import Response
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from pydantic import ConfigDict
|
from pydantic import ConfigDict
|
||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
from pydantic import model_validator
|
|
||||||
|
|
||||||
from transcription.db.models import Document
|
from transcription.db.models import Document
|
||||||
from transcription.db.models import DocumentPerson
|
from transcription.db.models import DocumentPerson
|
||||||
@@ -21,34 +20,13 @@ from transcription.db.models import PersonRole
|
|||||||
from transcription.services import DocumentService
|
from transcription.services import DocumentService
|
||||||
from transcription.services import PeopleService
|
from transcription.services import PeopleService
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/v4", tags=["v4-documents"])
|
router = APIRouter(prefix="/api", tags=["documents"])
|
||||||
|
|
||||||
|
|
||||||
class ApiModel(BaseModel):
|
class ApiModel(BaseModel):
|
||||||
model_config = ConfigDict(extra="forbid", frozen=True, str_strip_whitespace=True)
|
model_config = ConfigDict(extra="forbid", frozen=True, str_strip_whitespace=True)
|
||||||
|
|
||||||
|
|
||||||
class SelectorRequest(ApiModel):
|
|
||||||
@model_validator(mode="after")
|
|
||||||
def require_exactly_one_selector(self):
|
|
||||||
values = (self.selector_id, self.selector_code)
|
|
||||||
if sum(value is not None for value in values) != 1:
|
|
||||||
raise ValueError(f"Provide exactly one of {self.selector_names[0]} or {self.selector_names[1]}")
|
|
||||||
return self
|
|
||||||
|
|
||||||
@property
|
|
||||||
def selector_id(self) -> UUID | None:
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
@property
|
|
||||||
def selector_code(self) -> str | None:
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
@property
|
|
||||||
def selector_names(self) -> tuple[str, str]:
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
|
|
||||||
class DocumentTypeRead(ApiModel):
|
class DocumentTypeRead(ApiModel):
|
||||||
id: UUID
|
id: UUID
|
||||||
label: str
|
label: str
|
||||||
@@ -57,7 +35,6 @@ class DocumentTypeRead(ApiModel):
|
|||||||
|
|
||||||
class PersonRoleRead(ApiModel):
|
class PersonRoleRead(ApiModel):
|
||||||
id: UUID
|
id: UUID
|
||||||
code: str
|
|
||||||
label: str
|
label: str
|
||||||
is_active: bool
|
is_active: bool
|
||||||
|
|
||||||
@@ -73,39 +50,19 @@ class DocumentTypeWriteResponse(ApiModel):
|
|||||||
|
|
||||||
class DocumentPersonWriteRequest(ApiModel):
|
class DocumentPersonWriteRequest(ApiModel):
|
||||||
person_id: UUID
|
person_id: UUID
|
||||||
role_id: UUID | None = None
|
role_id: UUID
|
||||||
role_code: str | None = Field(default=None, min_length=1, pattern=r"^[a-z0-9_]+$")
|
|
||||||
|
|
||||||
@model_validator(mode="after")
|
|
||||||
def reject_conflicting_role_selectors(self):
|
|
||||||
if self.role_id is not None and self.role_code is not None:
|
|
||||||
raise ValueError("Provide role_id or role_code, not both")
|
|
||||||
return self
|
|
||||||
|
|
||||||
|
|
||||||
class DocumentPersonRoleUpdateRequest(SelectorRequest):
|
class DocumentPersonRoleUpdateRequest(ApiModel):
|
||||||
role_id: UUID | None = None
|
role_id: UUID
|
||||||
role_code: str | None = Field(default=None, min_length=1, pattern=r"^[a-z0-9_]+$")
|
|
||||||
|
|
||||||
@property
|
|
||||||
def selector_id(self) -> UUID | None:
|
|
||||||
return self.role_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def selector_code(self) -> str | None:
|
|
||||||
return self.role_code
|
|
||||||
|
|
||||||
@property
|
|
||||||
def selector_names(self) -> tuple[str, str]:
|
|
||||||
return "role_id", "role_code"
|
|
||||||
|
|
||||||
|
|
||||||
class DocumentPersonRead(ApiModel):
|
class DocumentPersonRead(ApiModel):
|
||||||
id: UUID
|
id: UUID
|
||||||
document_id: UUID
|
document_id: UUID
|
||||||
person_id: UUID
|
person_id: UUID
|
||||||
role_id: UUID | None
|
role_id: UUID
|
||||||
role_code: str
|
role_label: str | None = None
|
||||||
person_name: str | None = None
|
person_name: str | None = None
|
||||||
|
|
||||||
|
|
||||||
@@ -115,32 +72,27 @@ class DocumentPeopleResponse(ApiModel):
|
|||||||
|
|
||||||
|
|
||||||
def _document_type_to_read(item: DocumentType) -> DocumentTypeRead:
|
def _document_type_to_read(item: DocumentType) -> DocumentTypeRead:
|
||||||
return DocumentTypeRead(
|
item_id, label, is_active = _registry_read_values(item)
|
||||||
id=item.id,
|
return DocumentTypeRead(id=item_id, label=label, is_active=is_active)
|
||||||
label=item.label,
|
|
||||||
is_active=item.is_active,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _person_role_to_read(item: PersonRole) -> PersonRoleRead:
|
def _person_role_to_read(item: PersonRole) -> PersonRoleRead:
|
||||||
return PersonRoleRead(
|
item_id, label, is_active = _registry_read_values(item)
|
||||||
id=item.id,
|
return PersonRoleRead(id=item_id, label=label, is_active=is_active)
|
||||||
code=item.code,
|
|
||||||
label=item.label,
|
|
||||||
is_active=item.is_active,
|
def _registry_read_values(item: DocumentType | PersonRole) -> tuple[UUID, str, bool]:
|
||||||
)
|
return item.id, item.label, item.is_active
|
||||||
|
|
||||||
|
|
||||||
def _document_person_to_read(item: DocumentPerson) -> DocumentPersonRead:
|
def _document_person_to_read(item: DocumentPerson) -> DocumentPersonRead:
|
||||||
role_code = item.role_ref.code if item.role_ref is not None else str(item.role)
|
|
||||||
|
|
||||||
person_name = item.person.full_name if item.person is not None else None
|
person_name = item.person.full_name if item.person is not None else None
|
||||||
return DocumentPersonRead(
|
return DocumentPersonRead(
|
||||||
id=item.id,
|
id=item.id,
|
||||||
document_id=item.document_id,
|
document_id=item.document_id,
|
||||||
person_id=item.person_id,
|
person_id=item.person_id,
|
||||||
role_id=item.role_id,
|
role_id=item.role_id,
|
||||||
role_code=role_code,
|
role_label=item.role_ref.label if item.role_ref is not None else None,
|
||||||
person_name=person_name,
|
person_name=person_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -224,7 +176,6 @@ async def add_document_person_link(
|
|||||||
document_id=document_id,
|
document_id=document_id,
|
||||||
person_id=payload.person_id,
|
person_id=payload.person_id,
|
||||||
role_id=payload.role_id,
|
role_id=payload.role_id,
|
||||||
role_code=payload.role_code,
|
|
||||||
)
|
)
|
||||||
return _document_person_to_read(link)
|
return _document_person_to_read(link)
|
||||||
|
|
||||||
@@ -238,7 +189,6 @@ async def set_document_person_role(
|
|||||||
link = await service.set_document_person_role(
|
link = await service.set_document_person_role(
|
||||||
document_person_id=document_person_id,
|
document_person_id=document_person_id,
|
||||||
role_id=payload.role_id,
|
role_id=payload.role_id,
|
||||||
role_code=payload.role_code,
|
|
||||||
)
|
)
|
||||||
return _document_person_to_read(link)
|
return _document_person_to_read(link)
|
||||||
|
|
||||||
@@ -21,7 +21,9 @@ _STATUS_BY_CATEGORY: dict[ErrorCategory, int] = {
|
|||||||
ErrorCategory.NOT_FOUND: 404,
|
ErrorCategory.NOT_FOUND: 404,
|
||||||
ErrorCategory.CONFLICT: 409,
|
ErrorCategory.CONFLICT: 409,
|
||||||
ErrorCategory.EXTERNAL_PROVIDER: 503,
|
ErrorCategory.EXTERNAL_PROVIDER: 503,
|
||||||
|
ErrorCategory.EXTERNAL_TIMEOUT: 503,
|
||||||
ErrorCategory.INFRA_TRANSIENT: 503,
|
ErrorCategory.INFRA_TRANSIENT: 503,
|
||||||
|
ErrorCategory.PROCESSING: 500,
|
||||||
ErrorCategory.INFRA_PERSISTENT: 500,
|
ErrorCategory.INFRA_PERSISTENT: 500,
|
||||||
ErrorCategory.INTERNAL_UNEXPECTED: 500,
|
ErrorCategory.INTERNAL_UNEXPECTED: 500,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,44 @@
|
|||||||
"""Health endpoint routes."""
|
"""Health endpoint routes."""
|
||||||
|
|
||||||
|
from typing import NotRequired
|
||||||
|
from typing import TypedDict
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
from fastapi import Request
|
||||||
|
|
||||||
|
from transcription.worker import resolve_worker_health
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
def healthz() -> dict[str, str]:
|
class WorkerHealthPayload(TypedDict):
|
||||||
"""Return a simple health status payload."""
|
state: str
|
||||||
return {"status": "ok"}
|
error_id: NotRequired[str]
|
||||||
|
error_category: NotRequired[str]
|
||||||
|
|
||||||
|
|
||||||
|
class HealthPayload(TypedDict):
|
||||||
|
status: str
|
||||||
|
worker: WorkerHealthPayload
|
||||||
|
|
||||||
|
|
||||||
|
def healthz(request: Request) -> HealthPayload:
|
||||||
|
"""Return health status with worker-liveness signal."""
|
||||||
|
worker = resolve_worker_health(request.app.state)
|
||||||
|
payload: HealthPayload = {
|
||||||
|
"status": "ok",
|
||||||
|
"worker": {
|
||||||
|
"state": worker.state,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if worker.error_id is not None:
|
||||||
|
payload["worker"]["error_id"] = worker.error_id
|
||||||
|
if worker.error_category is not None:
|
||||||
|
payload["worker"]["error_category"] = worker.error_category
|
||||||
|
return payload
|
||||||
|
|
||||||
|
|
||||||
@router.get("/healthz")
|
@router.get("/healthz")
|
||||||
def healthz_route() -> dict[str, str]:
|
def healthz_route(request: Request) -> HealthPayload:
|
||||||
"""Route wrapper for health status payload."""
|
"""Route wrapper for health status payload."""
|
||||||
return healthz()
|
return healthz(request)
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
"""Safe media route for Document print previews."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Annotated
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from fastapi import APIRouter
|
||||||
|
from fastapi import Depends
|
||||||
|
from fastapi import HTTPException
|
||||||
|
from fastapi import Request
|
||||||
|
from fastapi.responses import FileResponse
|
||||||
|
|
||||||
|
from transcription.services.source_media import SOURCE_MIME_TYPES
|
||||||
|
from transcription.services.sources import SourceService
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api", tags=["print"])
|
||||||
|
|
||||||
|
|
||||||
|
def get_source_service(request: Request) -> SourceService:
|
||||||
|
services = getattr(request.app.state, "services", None)
|
||||||
|
if services is not None:
|
||||||
|
return services.sources
|
||||||
|
return SourceService()
|
||||||
|
|
||||||
|
|
||||||
|
SourceServiceDependency = Annotated[SourceService, Depends(get_source_service)]
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/documents/{document_id}/sources/{source_id}/media", response_class=FileResponse)
|
||||||
|
async def read_document_source_media(
|
||||||
|
document_id: UUID,
|
||||||
|
source_id: UUID,
|
||||||
|
service: SourceServiceDependency,
|
||||||
|
) -> FileResponse:
|
||||||
|
"""Serve one validated Source through record identifiers, never a supplied path."""
|
||||||
|
source = await service.read_source(source_id)
|
||||||
|
if source.document_id != document_id:
|
||||||
|
raise HTTPException(status_code=404, detail="Source not found for Document")
|
||||||
|
|
||||||
|
upload_root = service.settings.upload_dir.resolve()
|
||||||
|
path = (upload_root / Path(source.file_path)).resolve()
|
||||||
|
try:
|
||||||
|
path.relative_to(upload_root)
|
||||||
|
except ValueError as exc:
|
||||||
|
raise HTTPException(status_code=404, detail="Source media is outside managed storage") from exc
|
||||||
|
if not path.is_file():
|
||||||
|
raise HTTPException(status_code=404, detail="Source media is unavailable")
|
||||||
|
|
||||||
|
media_type = SOURCE_MIME_TYPES.get(path.suffix.lower())
|
||||||
|
if media_type is None:
|
||||||
|
raise HTTPException(status_code=415, detail="Unsupported Source media type")
|
||||||
|
return FileResponse(path, media_type=media_type)
|
||||||
+21
-19
@@ -14,20 +14,19 @@ from fastapi import status
|
|||||||
from fastapi.responses import RedirectResponse
|
from fastapi.responses import RedirectResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
||||||
|
from .api.documents_api import router as documents_router
|
||||||
from .api.errors import register_error_handlers
|
from .api.errors import register_error_handlers
|
||||||
from .api.health import router as health_router
|
from .api.health import router as health_router
|
||||||
from .api.v4_documents import router as v4_documents_router
|
from .api.print_api import router as print_router
|
||||||
from .config import Settings
|
from .config import Settings
|
||||||
from .config import configure_logging
|
from .config import configure_logging
|
||||||
from .config import get_settings
|
from .config import get_settings
|
||||||
from .db import create_all
|
from .db import create_all
|
||||||
from .db import dispose_database_runtime
|
from .db import dispose_database_runtime
|
||||||
from .db import initialize_database_runtime
|
from .db import initialize_database_runtime
|
||||||
|
from .db import reconcile_canonical_media_paths
|
||||||
|
from .db import reconcile_legacy_job_source_columns
|
||||||
from .services import ServiceBundle
|
from .services import ServiceBundle
|
||||||
from .services.documents import DocumentService
|
|
||||||
from .services.jobs import JobService
|
|
||||||
from .services.people import PeopleService
|
|
||||||
from .services.sources import SourceService
|
|
||||||
from .ui import register_pages
|
from .ui import register_pages
|
||||||
from .worker import worker_consumer_lifespan
|
from .worker import worker_consumer_lifespan
|
||||||
|
|
||||||
@@ -41,44 +40,46 @@ async def _lifespan(app: FastAPI):
|
|||||||
app.state.settings = settings
|
app.state.settings = settings
|
||||||
app.state.runtime = initialize_database_runtime(settings=settings)
|
app.state.runtime = initialize_database_runtime(settings=settings)
|
||||||
session_factory = app.state.runtime.session_factory
|
session_factory = app.state.runtime.session_factory
|
||||||
app.state.services = ServiceBundle(
|
app.state.services = ServiceBundle.from_session_factory(session_factory, settings=settings)
|
||||||
documents=DocumentService(session_factory=session_factory, settings=settings),
|
|
||||||
sources=SourceService(session_factory=session_factory, settings=settings),
|
|
||||||
jobs=JobService(session_factory=session_factory, settings=settings),
|
|
||||||
people=PeopleService(session_factory=session_factory, settings=settings),
|
|
||||||
)
|
|
||||||
|
|
||||||
if settings.should_bootstrap_schema:
|
if settings.should_bootstrap_schema:
|
||||||
await create_all(engine=app.state.runtime.engine)
|
await create_all(engine=app.state.runtime.engine)
|
||||||
|
await reconcile_legacy_job_source_columns(engine=app.state.runtime.engine)
|
||||||
|
await reconcile_canonical_media_paths(engine=app.state.runtime.engine)
|
||||||
|
|
||||||
settings.upload_dir.mkdir(parents=True, exist_ok=True)
|
settings.upload_dir.mkdir(parents=True, exist_ok=True)
|
||||||
settings.prompt_dir.mkdir(parents=True, exist_ok=True)
|
settings.prompt_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
settings.log_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
await _recover_stale_processing_jobs(app)
|
await _recover_stale_processing_jobs(app)
|
||||||
|
|
||||||
async with AsyncExitStack() as stack:
|
async with AsyncExitStack() as stack:
|
||||||
stack.push_async_callback(dispose_database_runtime)
|
stack.push_async_callback(dispose_database_runtime)
|
||||||
stop_event, worker_notifier = await stack.enter_async_context(
|
if settings.run_embedded_worker:
|
||||||
|
stop_event, worker_notifier, worker_health = await stack.enter_async_context(
|
||||||
worker_consumer_lifespan(
|
worker_consumer_lifespan(
|
||||||
session_factory=app.state.runtime.session_factory,
|
session_factory=app.state.runtime.session_factory,
|
||||||
poll_interval_seconds=1.0,
|
poll_interval_seconds=settings.worker_poll_interval_seconds,
|
||||||
|
shutdown_timeout_seconds=(
|
||||||
|
settings.worker_provider_timeout_seconds + settings.worker_shutdown_grace_seconds
|
||||||
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
app.state.worker_stop_event = stop_event
|
app.state.worker_stop_event = stop_event
|
||||||
app.state.worker_notifier = worker_notifier
|
app.state.worker_notifier = worker_notifier
|
||||||
|
app.state.worker_health = worker_health
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
async def _recover_stale_processing_jobs(app: FastAPI) -> None:
|
async def _recover_stale_processing_jobs(app: FastAPI) -> None:
|
||||||
"""Re-queue stale processing jobs at startup.
|
"""Re-queue stale processing jobs at startup.
|
||||||
|
|
||||||
Any job left in PROCESSING longer than the configured provider timeout is
|
Any job left in PROCESSING longer than the stale-job threshold is assumed
|
||||||
assumed orphaned and moved back to QUEUED before the worker starts.
|
orphaned and moved back to QUEUED before the worker starts.
|
||||||
"""
|
"""
|
||||||
settings = app.state.settings
|
settings = app.state.settings
|
||||||
stale_before = datetime.now(UTC) - timedelta(seconds=settings.worker_provider_timeout_seconds)
|
stale_before = datetime.now(UTC) - timedelta(seconds=settings.worker_stale_job_seconds)
|
||||||
job_service = JobService(session_factory=app.state.runtime.session_factory)
|
recovered = await app.state.services.jobs.requeue_stale_processing_jobs(stale_before=stale_before)
|
||||||
recovered = await job_service.requeue_stale_processing_jobs(stale_before=stale_before)
|
|
||||||
if recovered > 0:
|
if recovered > 0:
|
||||||
logger.warning("Recovered %s stale processing job(s) at startup", recovered)
|
logger.warning("Recovered %s stale processing job(s) at startup", recovered)
|
||||||
|
|
||||||
@@ -104,6 +105,7 @@ def create_app(settings: Settings | None = None) -> FastAPI:
|
|||||||
|
|
||||||
register_error_handlers(app)
|
register_error_handlers(app)
|
||||||
app.include_router(health_router)
|
app.include_router(health_router)
|
||||||
app.include_router(v4_documents_router)
|
app.include_router(documents_router)
|
||||||
|
app.include_router(print_router)
|
||||||
register_pages(app)
|
register_pages(app)
|
||||||
return app
|
return app
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
"""Helpers for accessing lifespan-owned application state resources."""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from fastapi import FastAPI
|
|
||||||
from sqlalchemy.ext.asyncio import async_sessionmaker
|
|
||||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
|
||||||
|
|
||||||
from transcription.db.runtime import DatabaseRuntime
|
|
||||||
from transcription.db.session import get_session_factory
|
|
||||||
from transcription.worker import WorkerNotifier
|
|
||||||
from transcription.worker import resolve_worker_notifier
|
|
||||||
|
|
||||||
|
|
||||||
def resolve_database_runtime(state: object) -> DatabaseRuntime | None:
|
|
||||||
"""Return database runtime from app-like state objects when available."""
|
|
||||||
runtime = getattr(state, "runtime", None)
|
|
||||||
return runtime if isinstance(runtime, DatabaseRuntime) else None
|
|
||||||
|
|
||||||
|
|
||||||
def require_database_runtime(state: object) -> DatabaseRuntime:
|
|
||||||
"""Return database runtime or raise when app lifespan has not initialized it."""
|
|
||||||
runtime = resolve_database_runtime(state)
|
|
||||||
if runtime is None:
|
|
||||||
raise RuntimeError("Database runtime is not initialized on application state")
|
|
||||||
return runtime
|
|
||||||
|
|
||||||
|
|
||||||
def resolve_session_factory(state: object) -> async_sessionmaker[AsyncSession]:
|
|
||||||
"""Return DB session factory from state when available, otherwise shared runtime."""
|
|
||||||
runtime = resolve_database_runtime(state)
|
|
||||||
if runtime is not None:
|
|
||||||
return runtime.session_factory
|
|
||||||
return get_session_factory()
|
|
||||||
|
|
||||||
|
|
||||||
def get_worker_notifier(app: FastAPI) -> WorkerNotifier:
|
|
||||||
"""Return app worker notifier, or a no-op fallback when unavailable."""
|
|
||||||
return resolve_worker_notifier(app.state)
|
|
||||||
@@ -1,4 +1,11 @@
|
|||||||
"""Private-corpus benchmark contracts and deterministic text scoring."""
|
"""Private-corpus benchmark contracts and deterministic text scoring.
|
||||||
|
|
||||||
|
This module is retained as the implementation of the evaluation policy in
|
||||||
|
`docs/invariant/ai_evidence_and_provenance.md` §5. Application runtime paths do
|
||||||
|
not call it directly, but preserved execution attempts and manually reviewed
|
||||||
|
references need a deterministic scorer that remains importable for tests and
|
||||||
|
operator tooling.
|
||||||
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -13,24 +20,6 @@ class BenchmarkModel(BaseModel):
|
|||||||
model_config = ConfigDict(extra="forbid", frozen=True)
|
model_config = ConfigDict(extra="forbid", frozen=True)
|
||||||
|
|
||||||
|
|
||||||
class BenchmarkItem(BenchmarkModel):
|
|
||||||
"""One private benchmark item referenced by archival identity."""
|
|
||||||
|
|
||||||
source_id: UUID
|
|
||||||
source_digest_sha256: str = Field(pattern=r"^[0-9a-f]{64}$")
|
|
||||||
categories: frozenset[str] = Field(min_length=1)
|
|
||||||
reference_transcription: str = Field(min_length=1)
|
|
||||||
|
|
||||||
|
|
||||||
class BenchmarkManifest(BenchmarkModel):
|
|
||||||
"""Versioned private benchmark definition without copied source media."""
|
|
||||||
|
|
||||||
schema_name: str = "transcription.private-benchmark"
|
|
||||||
schema_version: str = "1"
|
|
||||||
name: str = Field(min_length=1)
|
|
||||||
items: tuple[BenchmarkItem, ...] = Field(min_length=1)
|
|
||||||
|
|
||||||
|
|
||||||
class EditorialAssessment(BenchmarkModel):
|
class EditorialAssessment(BenchmarkModel):
|
||||||
"""Manually reviewed errors not represented adequately by CER or WER."""
|
"""Manually reviewed errors not represented adequately by CER or WER."""
|
||||||
|
|
||||||
|
|||||||
+134
-15
@@ -1,11 +1,13 @@
|
|||||||
"""Centralized application configuration.
|
"""Centralized application configuration.
|
||||||
|
|
||||||
All settings are loaded from environment variables (or a .env file)
|
All settings are loaded from environment variables (or an env file)
|
||||||
once at startup. Provider-specific defaults (model names, base URLs)
|
once at startup. Provider-specific defaults (model names, base URLs)
|
||||||
are resolved by the provider adapters, not here.
|
are resolved by the provider adapters, not here.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import copy
|
||||||
import logging.config
|
import logging.config
|
||||||
|
import os
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
from functools import cache
|
from functools import cache
|
||||||
@@ -19,10 +21,22 @@ from pydantic import ConfigDict
|
|||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
from pydantic import SecretStr
|
from pydantic import SecretStr
|
||||||
from pydantic import StringConstraints
|
from pydantic import StringConstraints
|
||||||
|
from pydantic import field_validator
|
||||||
|
from pydantic import model_validator
|
||||||
from pydantic_settings import BaseSettings
|
from pydantic_settings import BaseSettings
|
||||||
from pydantic_settings import SettingsConfigDict
|
from pydantic_settings import SettingsConfigDict
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
||||||
|
DEFAULT_ENV_FILE_NAME = ".env.production"
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_settings_env_file_path() -> Path:
|
||||||
|
"""Resolve the runtime env file independent of the process working directory."""
|
||||||
|
override = os.getenv("ENV_FILE", "").strip()
|
||||||
|
if override:
|
||||||
|
return Path(override)
|
||||||
|
return PROJECT_ROOT / DEFAULT_ENV_FILE_NAME
|
||||||
|
|
||||||
|
|
||||||
class Provider(StrEnum):
|
class Provider(StrEnum):
|
||||||
@@ -33,13 +47,15 @@ NonEmptyStr = Annotated[str, StringConstraints(strip_whitespace=True, min_length
|
|||||||
PromptFilename = Annotated[str, StringConstraints(strip_whitespace=True, min_length=1, pattern=r"^[^/\\]+$")]
|
PromptFilename = Annotated[str, StringConstraints(strip_whitespace=True, min_length=1, pattern=r"^[^/\\]+$")]
|
||||||
Probability = Annotated[float, Field(ge=0.0, le=1.0)]
|
Probability = Annotated[float, Field(ge=0.0, le=1.0)]
|
||||||
Temperature = Annotated[float, Field(ge=0.0, le=2.0)]
|
Temperature = Annotated[float, Field(ge=0.0, le=2.0)]
|
||||||
|
DEFAULT_PROVIDER_MODEL = "google/gemini-2.5-flash"
|
||||||
|
WORKER_STALE_TIMEOUT_MULTIPLIER = 3.0
|
||||||
|
|
||||||
|
|
||||||
class SqliteSettings(BaseModel):
|
class SqliteSettings(BaseModel):
|
||||||
model_config = ConfigDict(extra="forbid", frozen=True)
|
model_config = ConfigDict(extra="forbid", frozen=True)
|
||||||
|
|
||||||
driver: Literal["sqlite"] = "sqlite"
|
driver: Literal["sqlite"] = "sqlite"
|
||||||
path: NonEmptyStr = "app.db"
|
path: NonEmptyStr = "./data/transcription.db"
|
||||||
|
|
||||||
|
|
||||||
class PostgresSettings(BaseModel):
|
class PostgresSettings(BaseModel):
|
||||||
@@ -61,7 +77,7 @@ DatabaseSettings = Annotated[
|
|||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
model_config = SettingsConfigDict(
|
model_config = SettingsConfigDict(
|
||||||
env_file=".env",
|
env_file=None,
|
||||||
env_file_encoding="utf-8",
|
env_file_encoding="utf-8",
|
||||||
extra="ignore",
|
extra="ignore",
|
||||||
env_nested_delimiter="__",
|
env_nested_delimiter="__",
|
||||||
@@ -70,16 +86,26 @@ class Settings(BaseSettings):
|
|||||||
frozen=True,
|
frozen=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __init__(self, /, **values: Any) -> None:
|
||||||
|
if "_env_file" not in values:
|
||||||
|
values["_env_file"] = resolve_settings_env_file_path()
|
||||||
|
super().__init__(**values)
|
||||||
|
|
||||||
# --- NiceGUI Server ---
|
# --- NiceGUI Server ---
|
||||||
host: str = "0.0.0.0"
|
host: str = "0.0.0.0"
|
||||||
port: int = 8000
|
port: int = 8000
|
||||||
log_level: Literal["critical", "error", "warning", "info", "debug", "trace"] = "info"
|
log_level: Literal["critical", "error", "warning", "info", "debug", "trace"] = "info"
|
||||||
reload: bool = False
|
reload: bool = False
|
||||||
|
log_dir: Path = Path("./data/logs")
|
||||||
|
log_file_name: NonEmptyStr = "transcription.log"
|
||||||
|
log_file_max_bytes: int = Field(default=10 * 1024 * 1024, gt=0)
|
||||||
|
log_file_backup_count: int = Field(default=5, ge=1)
|
||||||
|
|
||||||
# --- AI provider ---
|
# --- AI provider ---
|
||||||
provider: Provider = Provider.OPENROUTER
|
provider: Provider = Provider.OPENROUTER
|
||||||
openrouter_api_key: SecretStr
|
openrouter_api_key: SecretStr
|
||||||
provider_model: NonEmptyStr | None = None
|
provider_model: NonEmptyStr | None = DEFAULT_PROVIDER_MODEL
|
||||||
|
provider_models: tuple[NonEmptyStr, ...] = ()
|
||||||
openrouter_http_referer: NonEmptyStr | None = None
|
openrouter_http_referer: NonEmptyStr | None = None
|
||||||
openrouter_app_title: NonEmptyStr | None = None
|
openrouter_app_title: NonEmptyStr | None = None
|
||||||
default_prompt_name: PromptFilename = "transcribe_document.md"
|
default_prompt_name: PromptFilename = "transcribe_document.md"
|
||||||
@@ -88,6 +114,8 @@ class Settings(BaseSettings):
|
|||||||
|
|
||||||
# --- runtime environment ---
|
# --- runtime environment ---
|
||||||
environment: Literal["development", "test", "production"] = "development"
|
environment: Literal["development", "test", "production"] = "development"
|
||||||
|
transcription_commit: NonEmptyStr | None = None
|
||||||
|
run_embedded_worker: bool = True
|
||||||
|
|
||||||
# --- persistence ---
|
# --- persistence ---
|
||||||
database: DatabaseSettings = Field(default_factory=SqliteSettings)
|
database: DatabaseSettings = Field(default_factory=SqliteSettings)
|
||||||
@@ -95,19 +123,97 @@ class Settings(BaseSettings):
|
|||||||
sqlite_check_same_thread: bool = False
|
sqlite_check_same_thread: bool = False
|
||||||
|
|
||||||
# --- filesystem paths ---
|
# --- filesystem paths ---
|
||||||
upload_dir: Path = Path("./uploads")
|
upload_dir: Path = Path("./data")
|
||||||
prompt_dir: Path = Path("./prompts")
|
prompt_dir: Path = Path("./prompts")
|
||||||
artifact_dir: Path = Path("./data/artifacts")
|
|
||||||
artifact_inline_threshold_bytes: int = Field(default=1_048_576, ge=1)
|
|
||||||
|
|
||||||
# --- worker reliability ---
|
# --- worker reliability ---
|
||||||
worker_max_retries: int = Field(default=0, ge=0)
|
worker_max_retries: int = Field(default=0, ge=0)
|
||||||
worker_retry_backoff_seconds: float = Field(default=0.0, ge=0.0)
|
# Bounded only from below. Vision transcription of a dense page routinely runs
|
||||||
worker_provider_timeout_seconds: float = Field(default=20.0, gt=0.0, le=20.0)
|
# well past twenty seconds, so an upper cap here would silently fail real work.
|
||||||
|
worker_provider_timeout_seconds: float = Field(default=30.0, gt=0.0)
|
||||||
|
worker_stale_job_seconds: float = Field(default=90.0, gt=0.0)
|
||||||
|
worker_retry_backoff_seconds: float = Field(default=1.0, ge=0.0)
|
||||||
|
worker_shutdown_grace_seconds: float = Field(default=5.0, ge=0.0)
|
||||||
|
worker_poll_interval_seconds: float = Field(default=1.0, gt=0.0)
|
||||||
worker_min_transcription_chars: int = Field(default=0, ge=0)
|
worker_min_transcription_chars: int = Field(default=0, ge=0)
|
||||||
worker_min_transcription_lines: int = Field(default=0, ge=0)
|
worker_min_transcription_lines: int = Field(default=0, ge=0)
|
||||||
worker_fail_on_finish_reason_length: bool = False
|
worker_fail_on_finish_reason_length: bool = False
|
||||||
|
|
||||||
|
@field_validator("provider_models", mode="before")
|
||||||
|
@classmethod
|
||||||
|
def validate_provider_models_input(cls, value: object) -> object:
|
||||||
|
if value is None:
|
||||||
|
return ()
|
||||||
|
if isinstance(value, (list, tuple)) and not value:
|
||||||
|
raise ValueError("PROVIDER_MODELS must contain at least one model")
|
||||||
|
return value
|
||||||
|
|
||||||
|
@model_validator(mode="before")
|
||||||
|
@classmethod
|
||||||
|
def normalize_provider_models(cls, data: object) -> object:
|
||||||
|
"""Build the immutable model selector with the configured default first.
|
||||||
|
|
||||||
|
This runs before field validation so the derived value is produced by
|
||||||
|
normal construction rather than by mutating a frozen instance.
|
||||||
|
"""
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
|
|
||||||
|
default_model = data.get("provider_model") or DEFAULT_PROVIDER_MODEL
|
||||||
|
if not isinstance(default_model, str):
|
||||||
|
return data
|
||||||
|
default_model = default_model.strip()
|
||||||
|
|
||||||
|
configured = data.get("provider_models")
|
||||||
|
if configured is None:
|
||||||
|
configured = ()
|
||||||
|
elif isinstance(configured, str):
|
||||||
|
# Left as-is so the field validator can report the malformed value.
|
||||||
|
return {**data, "provider_model": default_model}
|
||||||
|
elif not isinstance(configured, (list, tuple)):
|
||||||
|
return {**data, "provider_model": default_model}
|
||||||
|
elif not configured:
|
||||||
|
# Preserved so validate_provider_models_input can reject it.
|
||||||
|
return {**data, "provider_model": default_model}
|
||||||
|
|
||||||
|
deduplicated: list[str] = []
|
||||||
|
for model in (default_model, *configured):
|
||||||
|
if not isinstance(model, str):
|
||||||
|
return {**data, "provider_model": default_model}
|
||||||
|
normalized = model.strip()
|
||||||
|
if normalized not in deduplicated:
|
||||||
|
deduplicated.append(normalized)
|
||||||
|
|
||||||
|
return {**data, "provider_model": default_model, "provider_models": tuple(deduplicated)}
|
||||||
|
|
||||||
|
@model_validator(mode="before")
|
||||||
|
@classmethod
|
||||||
|
def _derive_worker_stale_job_seconds(cls, data: object) -> object:
|
||||||
|
"""Default stale-job recovery with margin over one provider timeout."""
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return data
|
||||||
|
if data.get("worker_stale_job_seconds") is not None:
|
||||||
|
return data
|
||||||
|
|
||||||
|
timeout = data.get("worker_provider_timeout_seconds", 30.0)
|
||||||
|
if not isinstance(timeout, (str, int, float)):
|
||||||
|
return data
|
||||||
|
try:
|
||||||
|
timeout_seconds = float(timeout)
|
||||||
|
except ValueError:
|
||||||
|
return data
|
||||||
|
return {
|
||||||
|
**data,
|
||||||
|
"worker_stale_job_seconds": timeout_seconds * WORKER_STALE_TIMEOUT_MULTIPLIER,
|
||||||
|
}
|
||||||
|
|
||||||
|
@model_validator(mode="after")
|
||||||
|
def _validate_worker_stale_job_seconds(self) -> "Settings":
|
||||||
|
"""Reject stale recovery that can fire before one provider timeout expires."""
|
||||||
|
if self.worker_stale_job_seconds <= self.worker_provider_timeout_seconds:
|
||||||
|
raise ValueError("WORKER_STALE_JOB_SECONDS must exceed WORKER_PROVIDER_TIMEOUT_SECONDS")
|
||||||
|
return self
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_bootstrap_schema(self) -> bool:
|
def should_bootstrap_schema(self) -> bool:
|
||||||
"""Return whether startup should auto-create schema for this environment."""
|
"""Return whether startup should auto-create schema for this environment."""
|
||||||
@@ -119,13 +225,13 @@ class Settings(BaseSettings):
|
|||||||
@cache
|
@cache
|
||||||
def get_settings(**kwargs: Any) -> Settings:
|
def get_settings(**kwargs: Any) -> Settings:
|
||||||
"""Load cached settings without reading process CLI arguments."""
|
"""Load cached settings without reading process CLI arguments."""
|
||||||
return Settings(_cli_parse_args=False, **kwargs) # pyright: ignore[reportCallIssue]
|
return Settings(_cli_parse_args=False, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def parse_cli_settings(args: Sequence[str] | None = None) -> Settings:
|
def parse_cli_settings(args: Sequence[str] | None = None) -> Settings:
|
||||||
"""Load settings with CLI arguments at the executable boundary."""
|
"""Load settings with CLI arguments at the executable boundary."""
|
||||||
cli_args = True if args is None else list(args)
|
cli_args = True if args is None else list(args)
|
||||||
return Settings(_cli_parse_args=cli_args) # pyright: ignore[reportCallIssue]
|
return Settings(_cli_parse_args=cli_args)
|
||||||
|
|
||||||
|
|
||||||
LOGGING_CONFIG: dict[str, Any] = {
|
LOGGING_CONFIG: dict[str, Any] = {
|
||||||
@@ -142,16 +248,24 @@ LOGGING_CONFIG: dict[str, Any] = {
|
|||||||
"class": "logging.StreamHandler",
|
"class": "logging.StreamHandler",
|
||||||
"formatter": "standard",
|
"formatter": "standard",
|
||||||
"stream": "ext://sys.stdout",
|
"stream": "ext://sys.stdout",
|
||||||
}
|
},
|
||||||
|
"file": {
|
||||||
|
"class": "logging.handlers.RotatingFileHandler",
|
||||||
|
"formatter": "standard",
|
||||||
|
"filename": str(Path("./data/logs") / "transcription.log"),
|
||||||
|
"maxBytes": 10 * 1024 * 1024,
|
||||||
|
"backupCount": 5,
|
||||||
|
"encoding": "utf-8",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"root": {
|
"root": {
|
||||||
"level": "INFO",
|
"level": "INFO",
|
||||||
"handlers": ["console"],
|
"handlers": ["console", "file"],
|
||||||
},
|
},
|
||||||
"loggers": {
|
"loggers": {
|
||||||
"transcription": {
|
"transcription": {
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"handlers": ["console"],
|
"handlers": ["console", "file"],
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -160,8 +274,13 @@ LOGGING_CONFIG: dict[str, Any] = {
|
|||||||
|
|
||||||
def configure_logging(settings: Settings | None = None) -> None:
|
def configure_logging(settings: Settings | None = None) -> None:
|
||||||
"""Configure root logging once at startup."""
|
"""Configure root logging once at startup."""
|
||||||
cfg = LOGGING_CONFIG.copy()
|
cfg = copy.deepcopy(LOGGING_CONFIG)
|
||||||
active_settings = settings or get_settings()
|
active_settings = settings or get_settings()
|
||||||
|
active_settings.log_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
file_handler = cfg["handlers"]["file"]
|
||||||
|
file_handler["filename"] = str(active_settings.log_dir / active_settings.log_file_name)
|
||||||
|
file_handler["maxBytes"] = active_settings.log_file_max_bytes
|
||||||
|
file_handler["backupCount"] = active_settings.log_file_backup_count
|
||||||
cfg["loggers"]["transcription"]["level"] = active_settings.log_level.upper()
|
cfg["loggers"]["transcription"]["level"] = active_settings.log_level.upper()
|
||||||
logging.config.dictConfig(cfg)
|
logging.config.dictConfig(cfg)
|
||||||
logger.debug("Logging configured")
|
logger.debug("Logging configured")
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
from .operations import create_all
|
from .operations import create_all
|
||||||
from .operations import upgrade_schema
|
from .operations import reconcile_canonical_media_paths
|
||||||
|
from .operations import reconcile_legacy_job_source_columns
|
||||||
|
from .operations import reconcile_person_name_columns
|
||||||
from .runtime import dispose_database_runtime
|
from .runtime import dispose_database_runtime
|
||||||
from .runtime import initialize_database_runtime
|
from .runtime import initialize_database_runtime
|
||||||
from .session import session_scope
|
from .session import session_scope
|
||||||
@@ -9,7 +11,9 @@ __all__ = [
|
|||||||
"create_all",
|
"create_all",
|
||||||
"dispose_database_runtime",
|
"dispose_database_runtime",
|
||||||
"initialize_database_runtime",
|
"initialize_database_runtime",
|
||||||
|
"reconcile_canonical_media_paths",
|
||||||
|
"reconcile_legacy_job_source_columns",
|
||||||
|
"reconcile_person_name_columns",
|
||||||
"session_scope",
|
"session_scope",
|
||||||
"transaction_scope",
|
"transaction_scope",
|
||||||
"upgrade_schema",
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
from functools import cache
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from sqlalchemy import URL
|
from sqlalchemy import URL
|
||||||
@@ -33,28 +32,44 @@ def get_database_url(settings: Settings) -> str:
|
|||||||
|
|
||||||
def resolve_engine(settings: Settings | None = None) -> AsyncEngine:
|
def resolve_engine(settings: Settings | None = None) -> AsyncEngine:
|
||||||
active_settings = settings or get_settings()
|
active_settings = settings or get_settings()
|
||||||
return get_engine(get_database_url(active_settings))
|
return get_engine(
|
||||||
|
get_database_url(active_settings),
|
||||||
|
sqlite_check_same_thread=active_settings.sqlite_check_same_thread,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@cache
|
_ENGINES: dict[str, AsyncEngine] = {}
|
||||||
def get_engine(database_url: str) -> AsyncEngine:
|
|
||||||
|
|
||||||
|
def _create_engine(database_url: str, *, sqlite_check_same_thread: bool) -> AsyncEngine:
|
||||||
kwargs: dict[str, Any] = {"echo": False, "pool_pre_ping": True}
|
kwargs: dict[str, Any] = {"echo": False, "pool_pre_ping": True}
|
||||||
if database_url.startswith("sqlite"):
|
if database_url.startswith("sqlite"):
|
||||||
kwargs["connect_args"] = {"check_same_thread": False}
|
kwargs["connect_args"] = {"check_same_thread": sqlite_check_same_thread}
|
||||||
if ":memory:" in database_url:
|
if ":memory:" in database_url:
|
||||||
kwargs["poolclass"] = StaticPool
|
kwargs["poolclass"] = StaticPool
|
||||||
|
|
||||||
return create_async_engine(database_url, **kwargs)
|
return create_async_engine(database_url, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def get_engine(database_url: str, *, sqlite_check_same_thread: bool = False) -> AsyncEngine:
|
||||||
|
"""Return the process-wide engine for ``database_url``, creating it on first use.
|
||||||
|
|
||||||
|
Engines are registered per URL so that disposing one leaves every other
|
||||||
|
database untouched.
|
||||||
|
"""
|
||||||
|
engine = _ENGINES.get(database_url)
|
||||||
|
if engine is None:
|
||||||
|
engine = _create_engine(database_url, sqlite_check_same_thread=sqlite_check_same_thread)
|
||||||
|
_ENGINES[database_url] = engine
|
||||||
|
return engine
|
||||||
|
|
||||||
|
|
||||||
async def dispose_engine(database_url: str) -> None:
|
async def dispose_engine(database_url: str) -> None:
|
||||||
engine = get_engine(database_url)
|
"""Dispose and unregister the engine for ``database_url`` only.
|
||||||
try:
|
|
||||||
|
Unknown URLs are a no-op rather than provoking the creation of an engine
|
||||||
|
purely so that it can be thrown away.
|
||||||
|
"""
|
||||||
|
engine = _ENGINES.pop(database_url, None)
|
||||||
|
if engine is not None:
|
||||||
await engine.dispose()
|
await engine.dispose()
|
||||||
finally:
|
|
||||||
get_engine.cache_clear()
|
|
||||||
|
|
||||||
|
|
||||||
async def refresh_engine(database_url: str) -> AsyncEngine:
|
|
||||||
await dispose_engine(database_url)
|
|
||||||
return get_engine(database_url)
|
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
"""Typed loader-option wrappers for SQLModel relationship attributes.
|
||||||
|
|
||||||
|
SQLModel declares relationships with their runtime Python type, so
|
||||||
|
``Document.jobs`` is annotated ``list[Job]`` even though at runtime it is an
|
||||||
|
``InstrumentedAttribute``. SQLAlchemy's loader options are typed against
|
||||||
|
``QueryableAttribute``, so every eager-load call site reads as a type error to a
|
||||||
|
static checker even though the code is correct.
|
||||||
|
|
||||||
|
These wrappers put that reinterpretation in one documented place instead of
|
||||||
|
scattering a suppression comment across every eager-load call. Import
|
||||||
|
``selectinload`` and ``defer`` from here rather than from ``sqlalchemy.orm``.
|
||||||
|
|
||||||
|
Multi-level eager loads must keep using the chained form --
|
||||||
|
``selectinload(A.b).selectinload(orm_attribute(B.c))`` -- and not the varargs
|
||||||
|
form ``selectinload(A.b, B.c)``. The two produce the same loader path, but
|
||||||
|
varargs applies the selectin strategy only to the last element while the
|
||||||
|
intermediate falls back to its default strategy. Every relationship here
|
||||||
|
declares ``lazy="raise"``, so the varargs form raises at render time.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
from typing import cast
|
||||||
|
|
||||||
|
from sqlalchemy.orm import defer as _defer
|
||||||
|
from sqlalchemy.orm import selectinload as _selectinload
|
||||||
|
from sqlalchemy.orm.attributes import QueryableAttribute
|
||||||
|
from sqlalchemy.orm.strategy_options import _AbstractLoad
|
||||||
|
|
||||||
|
|
||||||
|
def orm_attribute(attribute: object) -> QueryableAttribute[Any]:
|
||||||
|
"""Reinterpret a SQLModel relationship or field as its ORM descriptor."""
|
||||||
|
return cast("QueryableAttribute[Any]", attribute)
|
||||||
|
|
||||||
|
|
||||||
|
def selectinload(*keys: object) -> _AbstractLoad:
|
||||||
|
"""``sqlalchemy.orm.selectinload`` accepting SQLModel-annotated attributes."""
|
||||||
|
return _selectinload(*(orm_attribute(key) for key in keys))
|
||||||
|
|
||||||
|
|
||||||
|
def defer(*keys: object, raiseload: bool = False) -> _AbstractLoad:
|
||||||
|
"""``sqlalchemy.orm.defer`` accepting SQLModel-annotated attributes."""
|
||||||
|
first, *rest = (orm_attribute(key) for key in keys)
|
||||||
|
return _defer(first, *rest, raiseload=raiseload)
|
||||||
@@ -0,0 +1,610 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import json
|
||||||
|
import shutil
|
||||||
|
from collections.abc import Sequence
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from datetime import UTC
|
||||||
|
from datetime import date
|
||||||
|
from datetime import datetime
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
from uuid import UUID
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from sqlalchemy import URL
|
||||||
|
from sqlalchemy import MetaData
|
||||||
|
from sqlalchemy import Table
|
||||||
|
from sqlalchemy import bindparam
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy import func
|
||||||
|
from sqlalchemy import inspect as sqlalchemy_inspect
|
||||||
|
from sqlalchemy import select
|
||||||
|
from sqlalchemy import text
|
||||||
|
from sqlalchemy.engine import RowMapping
|
||||||
|
from sqlalchemy.engine import make_url
|
||||||
|
from sqlmodel import SQLModel
|
||||||
|
|
||||||
|
from transcription.config import Settings
|
||||||
|
from transcription.config import get_settings
|
||||||
|
|
||||||
|
# Register table metadata.
|
||||||
|
from transcription.db import models as _models # noqa: F401
|
||||||
|
from transcription.db.engine import get_database_url
|
||||||
|
|
||||||
|
EXPORT_TABLE_ORDER = (
|
||||||
|
"document_type",
|
||||||
|
"person_role",
|
||||||
|
"tag",
|
||||||
|
"document",
|
||||||
|
"person",
|
||||||
|
"genealogy_person",
|
||||||
|
"genealogy_family",
|
||||||
|
"photo",
|
||||||
|
"document_person",
|
||||||
|
"document_tag",
|
||||||
|
"person_tag",
|
||||||
|
"job",
|
||||||
|
"source",
|
||||||
|
"job_source",
|
||||||
|
"execution_attempt",
|
||||||
|
"genealogy_family_child",
|
||||||
|
"genealogy_citation",
|
||||||
|
)
|
||||||
|
|
||||||
|
BYTES_FIELDS = {"transport_body"}
|
||||||
|
VERIFICATION_TABLES = EXPORT_TABLE_ORDER
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class MigrationPaths:
|
||||||
|
source_db_url: str
|
||||||
|
target_db_url: str
|
||||||
|
source_upload_dir: Path
|
||||||
|
target_upload_dir: Path
|
||||||
|
bundle_dir: Path
|
||||||
|
|
||||||
|
|
||||||
|
def export_bundle(*, source_db_url: str, source_upload_dir: Path, bundle_dir: Path) -> None:
|
||||||
|
bundle_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
export_json = bundle_dir / "database.json"
|
||||||
|
uploads_bundle_dir = bundle_dir / "uploads"
|
||||||
|
|
||||||
|
payload: dict[str, Any] = {
|
||||||
|
"schema_name": "transcription.export-import",
|
||||||
|
"schema_version": "1",
|
||||||
|
"created_at": datetime.now(UTC).isoformat(),
|
||||||
|
"tables": {},
|
||||||
|
}
|
||||||
|
|
||||||
|
engine = create_engine(source_db_url)
|
||||||
|
legacy_portrait_rows: Sequence[RowMapping] = ()
|
||||||
|
try: # noqa: PLR1702
|
||||||
|
inspector = sqlalchemy_inspect(engine)
|
||||||
|
source_tables = set(inspector.get_table_names())
|
||||||
|
metadata = MetaData()
|
||||||
|
metadata.reflect(bind=engine)
|
||||||
|
current_metadata = SQLModel.metadata
|
||||||
|
|
||||||
|
with engine.connect() as connection:
|
||||||
|
for table_name in EXPORT_TABLE_ORDER:
|
||||||
|
if table_name not in source_tables:
|
||||||
|
payload["tables"][table_name] = []
|
||||||
|
continue
|
||||||
|
|
||||||
|
source_table = metadata.tables[table_name]
|
||||||
|
target_table = current_metadata.tables[table_name]
|
||||||
|
export_columns = [column.name for column in target_table.columns if column.name in source_table.columns]
|
||||||
|
if table_name == "person" and "full_name" in source_table.columns:
|
||||||
|
for legacy_column in ("full_name",):
|
||||||
|
if legacy_column not in export_columns:
|
||||||
|
export_columns.append(legacy_column)
|
||||||
|
if table_name == "person" and "portrait_path" in source_table.columns:
|
||||||
|
legacy_portrait_rows = (
|
||||||
|
connection.execute(
|
||||||
|
select(source_table.c["id"], source_table.c["portrait_path"]).where(
|
||||||
|
source_table.c["portrait_path"].is_not(None)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.mappings()
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
rows = connection.execute(select(*(source_table.c[name] for name in export_columns))).mappings().all()
|
||||||
|
payload["tables"][table_name] = [
|
||||||
|
_serialize_row(row, table_name=table_name, source_upload_dir=source_upload_dir) for row in rows
|
||||||
|
]
|
||||||
|
finally:
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
|
if uploads_bundle_dir.exists():
|
||||||
|
shutil.rmtree(uploads_bundle_dir)
|
||||||
|
if source_upload_dir.exists():
|
||||||
|
shutil.copytree(source_upload_dir, uploads_bundle_dir)
|
||||||
|
else:
|
||||||
|
uploads_bundle_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
_prepare_photo_payload_and_uploads(
|
||||||
|
payload=payload,
|
||||||
|
uploads_bundle_dir=uploads_bundle_dir,
|
||||||
|
legacy_portrait_rows=legacy_portrait_rows,
|
||||||
|
)
|
||||||
|
_relocate_homepage_markdown(uploads_bundle_dir=uploads_bundle_dir)
|
||||||
|
|
||||||
|
export_json.write_text(json.dumps(payload, indent=2), encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def import_bundle(*, target_db_url: str, target_upload_dir: Path, bundle_dir: Path) -> None:
|
||||||
|
export_json = bundle_dir / "database.json"
|
||||||
|
uploads_bundle_dir = bundle_dir / "uploads"
|
||||||
|
payload = json.loads(export_json.read_text(encoding="utf-8"))
|
||||||
|
|
||||||
|
if target_upload_dir.exists():
|
||||||
|
shutil.rmtree(target_upload_dir)
|
||||||
|
target_upload_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
if uploads_bundle_dir.exists():
|
||||||
|
shutil.copytree(uploads_bundle_dir, target_upload_dir, dirs_exist_ok=True)
|
||||||
|
|
||||||
|
_reset_sqlite_target_file(target_db_url)
|
||||||
|
_ensure_sqlite_target_parent_exists(target_db_url)
|
||||||
|
engine = create_engine(target_db_url)
|
||||||
|
try:
|
||||||
|
SQLModel.metadata.create_all(engine)
|
||||||
|
execution_attempt_ids = _collect_execution_attempt_ids(payload)
|
||||||
|
with engine.begin() as connection:
|
||||||
|
for table_name in reversed(EXPORT_TABLE_ORDER):
|
||||||
|
table = SQLModel.metadata.tables[table_name]
|
||||||
|
connection.execute(table.delete())
|
||||||
|
|
||||||
|
deferred_source_preferred_attempt_updates: list[dict[str, Any]] = []
|
||||||
|
for table_name in EXPORT_TABLE_ORDER:
|
||||||
|
rows = payload.get("tables", {}).get(table_name, [])
|
||||||
|
if not rows:
|
||||||
|
continue
|
||||||
|
table = SQLModel.metadata.tables[table_name]
|
||||||
|
if table_name == "source":
|
||||||
|
prepared_source_rows, updates = _prepare_source_rows_for_import(
|
||||||
|
rows=rows,
|
||||||
|
source_table=table,
|
||||||
|
execution_attempt_ids=execution_attempt_ids,
|
||||||
|
)
|
||||||
|
deferred_source_preferred_attempt_updates.extend(updates)
|
||||||
|
connection.execute(table.insert(), prepared_source_rows)
|
||||||
|
continue
|
||||||
|
connection.execute(table.insert(), [_deserialize_row(row, table) for row in rows])
|
||||||
|
|
||||||
|
if deferred_source_preferred_attempt_updates:
|
||||||
|
source_table = SQLModel.metadata.tables["source"]
|
||||||
|
connection.execute(
|
||||||
|
source_table.update()
|
||||||
|
.where(source_table.c.id == bindparam("source_id"))
|
||||||
|
.values(preferred_execution_attempt_id=bindparam("preferred_execution_attempt_id")),
|
||||||
|
deferred_source_preferred_attempt_updates,
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
def _collect_execution_attempt_ids(payload: dict[str, Any]) -> set[str]:
|
||||||
|
execution_attempt_rows = payload.get("tables", {}).get("execution_attempt", [])
|
||||||
|
return {_normalize_uuid_like(row.get("id")) for row in execution_attempt_rows if row.get("id") is not None}
|
||||||
|
|
||||||
|
|
||||||
|
def _prepare_source_rows_for_import(
|
||||||
|
*,
|
||||||
|
rows: list[dict[str, Any]],
|
||||||
|
source_table: Table,
|
||||||
|
execution_attempt_ids: set[str],
|
||||||
|
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
|
||||||
|
prepared_source_rows: list[dict[str, Any]] = []
|
||||||
|
updates: list[dict[str, Any]] = []
|
||||||
|
for row in rows:
|
||||||
|
source_row = _deserialize_row(row, source_table)
|
||||||
|
source_id = source_row.get("id")
|
||||||
|
preferred_attempt_id = source_row.get("preferred_execution_attempt_id")
|
||||||
|
if (
|
||||||
|
source_id is not None
|
||||||
|
and preferred_attempt_id is not None
|
||||||
|
and _normalize_uuid_like(preferred_attempt_id) in execution_attempt_ids
|
||||||
|
):
|
||||||
|
updates.append(
|
||||||
|
{
|
||||||
|
"source_id": source_id,
|
||||||
|
"preferred_execution_attempt_id": preferred_attempt_id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
source_row["preferred_execution_attempt_id"] = None
|
||||||
|
prepared_source_rows.append(source_row)
|
||||||
|
return prepared_source_rows, updates
|
||||||
|
|
||||||
|
|
||||||
|
def _ensure_sqlite_target_parent_exists(target_db_url: str) -> None:
|
||||||
|
parsed = make_url(target_db_url)
|
||||||
|
if not parsed.drivername.startswith("sqlite"):
|
||||||
|
return
|
||||||
|
database = parsed.database
|
||||||
|
if not database or database == ":memory:":
|
||||||
|
return
|
||||||
|
Path(database).parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
|
def _reset_sqlite_target_file(target_db_url: str) -> None:
|
||||||
|
parsed = make_url(target_db_url)
|
||||||
|
if not parsed.drivername.startswith("sqlite"):
|
||||||
|
return
|
||||||
|
database = parsed.database
|
||||||
|
if not database or database == ":memory:":
|
||||||
|
return
|
||||||
|
target = Path(database)
|
||||||
|
if target.exists():
|
||||||
|
target.unlink()
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_via_bundle(paths: MigrationPaths) -> None:
|
||||||
|
export_bundle(
|
||||||
|
source_db_url=paths.source_db_url,
|
||||||
|
source_upload_dir=paths.source_upload_dir,
|
||||||
|
bundle_dir=paths.bundle_dir,
|
||||||
|
)
|
||||||
|
import_bundle(
|
||||||
|
target_db_url=paths.target_db_url,
|
||||||
|
target_upload_dir=paths.target_upload_dir,
|
||||||
|
bundle_dir=paths.bundle_dir,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class MigrationVerificationReport:
|
||||||
|
source_counts: dict[str, int]
|
||||||
|
target_counts: dict[str, int]
|
||||||
|
mismatched_tables: dict[str, dict[str, int]]
|
||||||
|
integrity_violations: dict[str, int]
|
||||||
|
success: bool
|
||||||
|
|
||||||
|
def to_dict(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"success": self.success,
|
||||||
|
"source_counts": self.source_counts,
|
||||||
|
"target_counts": self.target_counts,
|
||||||
|
"mismatched_tables": self.mismatched_tables,
|
||||||
|
"integrity_violations": self.integrity_violations,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def sqlite_url_from_path(path: Path) -> str:
|
||||||
|
return URL.create(drivername="sqlite", database=str(path)).render_as_string(hide_password=False)
|
||||||
|
|
||||||
|
|
||||||
|
def default_sync_db_url(settings: Settings | None = None) -> str:
|
||||||
|
runtime_settings = settings or get_settings()
|
||||||
|
return get_database_url(runtime_settings).replace("+aiosqlite", "").replace("+asyncpg", "").replace("+psycopg", "")
|
||||||
|
|
||||||
|
|
||||||
|
def verify_migration(*, source_db_url: str, target_db_url: str) -> MigrationVerificationReport:
|
||||||
|
source_counts = _table_counts(source_db_url)
|
||||||
|
target_counts = _table_counts(target_db_url)
|
||||||
|
mismatched_tables = {
|
||||||
|
table_name: {"source": source_counts[table_name], "target": target_counts[table_name]}
|
||||||
|
for table_name in VERIFICATION_TABLES
|
||||||
|
if source_counts[table_name] != target_counts[table_name]
|
||||||
|
}
|
||||||
|
integrity_violations = _integrity_violations(target_db_url)
|
||||||
|
success = not mismatched_tables and all(count == 0 for count in integrity_violations.values())
|
||||||
|
return MigrationVerificationReport(
|
||||||
|
source_counts=source_counts,
|
||||||
|
target_counts=target_counts,
|
||||||
|
mismatched_tables=mismatched_tables,
|
||||||
|
integrity_violations=integrity_violations,
|
||||||
|
success=success,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _table_counts(db_url: str) -> dict[str, int]:
|
||||||
|
engine = create_engine(db_url)
|
||||||
|
try:
|
||||||
|
metadata = MetaData()
|
||||||
|
metadata.reflect(bind=engine)
|
||||||
|
counts: dict[str, int] = {}
|
||||||
|
with engine.connect() as connection:
|
||||||
|
for table_name in VERIFICATION_TABLES:
|
||||||
|
table = metadata.tables.get(table_name)
|
||||||
|
if table is None:
|
||||||
|
counts[table_name] = 0
|
||||||
|
continue
|
||||||
|
counts[table_name] = int(connection.execute(select(func.count()).select_from(table)).scalar_one())
|
||||||
|
return counts
|
||||||
|
finally:
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
def _integrity_violations(db_url: str) -> dict[str, int]:
|
||||||
|
checks = {
|
||||||
|
"orphan_source_document": (
|
||||||
|
"select count(*) from source s left join document d on d.id = s.document_id where d.id is null"
|
||||||
|
),
|
||||||
|
"orphan_job_document": (
|
||||||
|
"select count(*) from job j left join document d on d.id = j.document_id where d.id is null"
|
||||||
|
),
|
||||||
|
"orphan_job_source_job": (
|
||||||
|
"select count(*) from job_source js left join job j on j.id = js.job_id where j.id is null"
|
||||||
|
),
|
||||||
|
"orphan_job_source_source": (
|
||||||
|
"select count(*) from job_source js left join source s on s.id = js.source_id where s.id is null"
|
||||||
|
),
|
||||||
|
"orphan_attempt_job_source": (
|
||||||
|
"select count(*) from execution_attempt ea "
|
||||||
|
"left join job_source js on js.id = ea.job_source_id "
|
||||||
|
"where js.id is null"
|
||||||
|
),
|
||||||
|
"orphan_attempt_job": (
|
||||||
|
"select count(*) from execution_attempt ea left join job j on j.id = ea.job_id where j.id is null"
|
||||||
|
),
|
||||||
|
"orphan_attempt_source": (
|
||||||
|
"select count(*) from execution_attempt ea left join source s on s.id = ea.source_id where s.id is null"
|
||||||
|
),
|
||||||
|
"duplicate_attempt_numbers": (
|
||||||
|
"select count(*) from ("
|
||||||
|
" select job_id, source_id, attempt_number, count(*) as c"
|
||||||
|
" from execution_attempt"
|
||||||
|
" group by job_id, source_id, attempt_number"
|
||||||
|
" having count(*) > 1"
|
||||||
|
") x"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
engine = create_engine(db_url)
|
||||||
|
try:
|
||||||
|
with engine.connect() as connection:
|
||||||
|
return {
|
||||||
|
check_name: int(connection.execute(text(query)).scalar_one()) for check_name, query in checks.items()
|
||||||
|
}
|
||||||
|
finally:
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
|
|
||||||
|
def _serialize_row(row: RowMapping, *, table_name: str, source_upload_dir: Path) -> dict[str, Any]:
|
||||||
|
serialized: dict[str, Any] = {}
|
||||||
|
for raw_key, value in row.items():
|
||||||
|
key = str(raw_key)
|
||||||
|
serialized_value = _serialize_value(value)
|
||||||
|
if table_name == "source" and key == "file_path" and isinstance(serialized_value, str):
|
||||||
|
serialized[key] = _canonical_media_relative_path(
|
||||||
|
serialized_value,
|
||||||
|
source_upload_dir=source_upload_dir,
|
||||||
|
preferred_prefix="documents/",
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
if table_name == "photo" and key == "path" and isinstance(serialized_value, str):
|
||||||
|
serialized[key] = _canonical_media_relative_path(
|
||||||
|
serialized_value,
|
||||||
|
source_upload_dir=source_upload_dir,
|
||||||
|
preferred_prefix="photos/",
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
if table_name == "person" and key == "full_name" and isinstance(serialized_value, str):
|
||||||
|
given_names, last_name = _split_legacy_full_name(serialized_value)
|
||||||
|
serialized["given_names"] = given_names
|
||||||
|
serialized["last_name"] = last_name
|
||||||
|
continue
|
||||||
|
serialized[key] = serialized_value
|
||||||
|
if table_name == "person":
|
||||||
|
serialized["given_names"] = str(serialized.get("given_names") or "").strip()
|
||||||
|
serialized["last_name"] = str(serialized.get("last_name") or "").strip()
|
||||||
|
return serialized
|
||||||
|
|
||||||
|
|
||||||
|
def _split_legacy_full_name(full_name: str) -> tuple[str, str]:
|
||||||
|
tokens = [token for token in full_name.strip().split() if token]
|
||||||
|
if len(tokens) >= 2:
|
||||||
|
return (" ".join(tokens[:-1]), tokens[-1])
|
||||||
|
if len(tokens) == 1:
|
||||||
|
return (tokens[0], tokens[0])
|
||||||
|
return ("Unknown", "Unknown")
|
||||||
|
|
||||||
|
|
||||||
|
def _serialize_value(value: Any) -> Any:
|
||||||
|
if isinstance(value, UUID):
|
||||||
|
return str(value)
|
||||||
|
if isinstance(value, (datetime, date)):
|
||||||
|
return value.isoformat()
|
||||||
|
if isinstance(value, bytes):
|
||||||
|
return {"encoding": "base64", "data": base64.b64encode(value).decode("ascii")}
|
||||||
|
if isinstance(value, dict):
|
||||||
|
return {str(k): _serialize_value(v) for k, v in value.items()}
|
||||||
|
if isinstance(value, list):
|
||||||
|
return [_serialize_value(item) for item in value]
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _deserialize_row(row: dict[str, Any], table: Table) -> dict[str, Any]:
|
||||||
|
deserialized: dict[str, Any] = {}
|
||||||
|
for key, value in row.items():
|
||||||
|
if key in BYTES_FIELDS and isinstance(value, dict) and value.get("encoding") == "base64":
|
||||||
|
deserialized[key] = base64.b64decode(value["data"])
|
||||||
|
continue
|
||||||
|
if key in table.columns:
|
||||||
|
try:
|
||||||
|
python_type: type[Any] = table.columns[key].type.python_type
|
||||||
|
except NotImplementedError:
|
||||||
|
deserialized[key] = value
|
||||||
|
continue
|
||||||
|
deserialized[key] = _deserialize_value(python_type, value)
|
||||||
|
return deserialized
|
||||||
|
|
||||||
|
|
||||||
|
def _deserialize_value(python_type: type[Any], value: Any) -> Any:
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
if python_type is UUID and isinstance(value, str):
|
||||||
|
return UUID(value)
|
||||||
|
if python_type is datetime and isinstance(value, str):
|
||||||
|
return datetime.fromisoformat(value)
|
||||||
|
if python_type is date and isinstance(value, str):
|
||||||
|
return date.fromisoformat(value)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_uuid_like(value: Any) -> str:
|
||||||
|
if isinstance(value, UUID):
|
||||||
|
return str(value)
|
||||||
|
if isinstance(value, str):
|
||||||
|
text_value = value.strip()
|
||||||
|
try:
|
||||||
|
return str(UUID(text_value))
|
||||||
|
except ValueError:
|
||||||
|
return text_value
|
||||||
|
return str(value)
|
||||||
|
|
||||||
|
|
||||||
|
def _canonical_media_relative_path(value: str, *, source_upload_dir: Path, preferred_prefix: str) -> str:
|
||||||
|
normalized = value.strip().replace("\\", "/")
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
upload_root = source_upload_dir.resolve().as_posix().casefold().rstrip("/")
|
||||||
|
if lowered.startswith(upload_root + "/"):
|
||||||
|
normalized = normalized[len(source_upload_dir.resolve().as_posix()) + 1 :]
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
|
||||||
|
if lowered.startswith("/uploads/"):
|
||||||
|
normalized = normalized[len("/uploads/") :]
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
elif lowered.startswith("uploads/"):
|
||||||
|
normalized = normalized[len("uploads/") :]
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
elif lowered.startswith("data/"):
|
||||||
|
normalized = normalized[len("data/") :]
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
|
||||||
|
if preferred_prefix == "persons/" and lowered.startswith("portraits/"):
|
||||||
|
normalized = "persons/" + normalized[len("portraits/") :]
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
|
||||||
|
for prefix in ("documents/", "photos/", "persons/"):
|
||||||
|
marker = f"/{prefix}"
|
||||||
|
index = lowered.find(marker)
|
||||||
|
if index >= 0:
|
||||||
|
normalized = normalized[index + 1 :]
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
break
|
||||||
|
|
||||||
|
if not lowered.startswith(preferred_prefix):
|
||||||
|
return normalized
|
||||||
|
return Path(normalized).as_posix()
|
||||||
|
|
||||||
|
|
||||||
|
def _prepare_photo_payload_and_uploads( # noqa: PLR0915
|
||||||
|
*,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
uploads_bundle_dir: Path,
|
||||||
|
legacy_portrait_rows: Sequence[RowMapping],
|
||||||
|
) -> None:
|
||||||
|
photo_rows = payload.setdefault("tables", {}).setdefault("photo", [])
|
||||||
|
photos_dir = uploads_bundle_dir / "photos"
|
||||||
|
photos_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
# Keep only photo rows whose referenced media exists inside the uploads tree.
|
||||||
|
# This prevents stale/injected rows from blocking legacy backfill.
|
||||||
|
retained_rows: list[dict[str, Any]] = []
|
||||||
|
for row in photo_rows:
|
||||||
|
path_value = row.get("path")
|
||||||
|
if not isinstance(path_value, str) or not path_value.strip():
|
||||||
|
continue
|
||||||
|
canonical_path = _canonical_media_relative_path(
|
||||||
|
path_value,
|
||||||
|
source_upload_dir=uploads_bundle_dir,
|
||||||
|
preferred_prefix="photos/",
|
||||||
|
)
|
||||||
|
candidate = uploads_bundle_dir / canonical_path
|
||||||
|
if not candidate.exists():
|
||||||
|
continue
|
||||||
|
row["path"] = canonical_path
|
||||||
|
retained_rows.append(row)
|
||||||
|
photo_rows[:] = retained_rows
|
||||||
|
|
||||||
|
existing_homepage_rows = [row for row in photo_rows if row.get("person_id") is None]
|
||||||
|
existing_person_ids = {str(row["person_id"]) for row in photo_rows if row.get("person_id") is not None}
|
||||||
|
existing_primary_person_ids = {
|
||||||
|
str(row["person_id"]) for row in photo_rows if row.get("person_id") is not None and bool(row.get("is_primary"))
|
||||||
|
}
|
||||||
|
has_homepage_primary = any(bool(row.get("is_primary")) for row in existing_homepage_rows)
|
||||||
|
|
||||||
|
now_iso = datetime.now(UTC).isoformat()
|
||||||
|
for row in legacy_portrait_rows:
|
||||||
|
portrait_path = row.get("portrait_path")
|
||||||
|
person_id = row.get("id")
|
||||||
|
if not isinstance(portrait_path, str) or not portrait_path.strip():
|
||||||
|
continue
|
||||||
|
if person_id is None:
|
||||||
|
continue
|
||||||
|
canonical = _canonical_media_relative_path(
|
||||||
|
portrait_path,
|
||||||
|
source_upload_dir=uploads_bundle_dir,
|
||||||
|
preferred_prefix="persons/",
|
||||||
|
)
|
||||||
|
source_file = uploads_bundle_dir / canonical
|
||||||
|
if not source_file.exists():
|
||||||
|
continue
|
||||||
|
person_key = str(person_id)
|
||||||
|
if person_key in existing_person_ids:
|
||||||
|
continue
|
||||||
|
suffix = Path(canonical).suffix.lower() or ".jpg"
|
||||||
|
photo_id = str(uuid4())
|
||||||
|
relative_path = f"photos/{photo_id}{suffix}"
|
||||||
|
target_file = uploads_bundle_dir / relative_path
|
||||||
|
target_file.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
shutil.copy2(source_file, target_file)
|
||||||
|
is_primary = person_key not in existing_primary_person_ids
|
||||||
|
photo_rows.append(
|
||||||
|
{
|
||||||
|
"id": photo_id,
|
||||||
|
"person_id": person_key,
|
||||||
|
"path": relative_path,
|
||||||
|
"description": None,
|
||||||
|
"is_primary": is_primary,
|
||||||
|
"created_at": now_iso,
|
||||||
|
"updated_at": now_iso,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
existing_person_ids.add(person_key)
|
||||||
|
if is_primary:
|
||||||
|
existing_primary_person_ids.add(person_key)
|
||||||
|
|
||||||
|
legacy_homepage_dir = uploads_bundle_dir / "homepage"
|
||||||
|
if not legacy_homepage_dir.exists():
|
||||||
|
return
|
||||||
|
|
||||||
|
homepage_images = sorted(
|
||||||
|
[
|
||||||
|
path
|
||||||
|
for path in legacy_homepage_dir.iterdir()
|
||||||
|
if path.is_file()
|
||||||
|
and path.suffix.lower() in {".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".tif", ".tiff"}
|
||||||
|
],
|
||||||
|
key=lambda path: (path.stat().st_mtime, path.name),
|
||||||
|
)
|
||||||
|
if existing_homepage_rows:
|
||||||
|
return
|
||||||
|
for index, image_path in enumerate(homepage_images):
|
||||||
|
photo_id = str(uuid4())
|
||||||
|
relative_path = f"photos/{photo_id}{image_path.suffix.lower()}"
|
||||||
|
target_file = uploads_bundle_dir / relative_path
|
||||||
|
target_file.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
shutil.copy2(image_path, target_file)
|
||||||
|
photo_rows.append(
|
||||||
|
{
|
||||||
|
"id": photo_id,
|
||||||
|
"person_id": None,
|
||||||
|
"path": relative_path,
|
||||||
|
"description": None,
|
||||||
|
"is_primary": (not has_homepage_primary) and index == 0,
|
||||||
|
"created_at": now_iso,
|
||||||
|
"updated_at": now_iso,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _relocate_homepage_markdown(*, uploads_bundle_dir: Path) -> None:
|
||||||
|
legacy_markdown = uploads_bundle_dir / "homepage" / "homepage.md"
|
||||||
|
target_markdown = uploads_bundle_dir / "homepage.md"
|
||||||
|
if not legacy_markdown.exists() or target_markdown.exists():
|
||||||
|
return
|
||||||
|
target_markdown.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
shutil.copy2(legacy_markdown, target_markdown)
|
||||||
+495
-132
@@ -4,6 +4,7 @@ from datetime import UTC
|
|||||||
from datetime import date
|
from datetime import date
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
|
from typing import Any
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
@@ -11,19 +12,45 @@ from uuid import uuid4
|
|||||||
from pydantic import JsonValue
|
from pydantic import JsonValue
|
||||||
from sqlalchemy import JSON
|
from sqlalchemy import JSON
|
||||||
from sqlalchemy import BigInteger
|
from sqlalchemy import BigInteger
|
||||||
from sqlalchemy import CheckConstraint
|
|
||||||
from sqlalchemy import Column
|
from sqlalchemy import Column
|
||||||
from sqlalchemy import Enum as SAEnum
|
from sqlalchemy import Enum as SAEnum
|
||||||
|
from sqlalchemy import ForeignKey
|
||||||
|
from sqlalchemy import Index
|
||||||
from sqlalchemy import LargeBinary
|
from sqlalchemy import LargeBinary
|
||||||
from sqlalchemy import UniqueConstraint
|
from sqlalchemy import UniqueConstraint
|
||||||
|
from sqlalchemy import Uuid
|
||||||
|
from sqlalchemy import inspect as sqlalchemy_inspect
|
||||||
from sqlalchemy.dialects.postgresql import JSONB
|
from sqlalchemy.dialects.postgresql import JSONB
|
||||||
from sqlalchemy.orm.exc import DetachedInstanceError
|
from sqlalchemy.exc import NoInspectionAvailable
|
||||||
|
from sqlalchemy.orm.state import InstanceState
|
||||||
from sqlalchemy.types import TypeDecorator
|
from sqlalchemy.types import TypeDecorator
|
||||||
from sqlmodel import Field
|
from sqlmodel import Field
|
||||||
from sqlmodel import Relationship
|
from sqlmodel import Relationship
|
||||||
from sqlmodel import SQLModel
|
from sqlmodel import SQLModel
|
||||||
|
|
||||||
|
|
||||||
|
def _loaded_attribute(instance: object, attribute: str) -> Any | None:
|
||||||
|
"""Return ``attribute`` only when it is already loaded on ``instance``.
|
||||||
|
|
||||||
|
Relationships in this module declare ``lazy="raise"``, so reading an
|
||||||
|
unloaded attribute is an error rather than a silent query. Callers that
|
||||||
|
render optional detail use this to distinguish "not loaded" from "absent"
|
||||||
|
without catching exceptions indiscriminately.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
state: InstanceState[Any] = sqlalchemy_inspect(instance, raiseerr=True)
|
||||||
|
except NoInspectionAvailable:
|
||||||
|
return None
|
||||||
|
if attribute in state.unloaded:
|
||||||
|
return None
|
||||||
|
return state.dict.get(attribute)
|
||||||
|
|
||||||
|
|
||||||
|
def _utc_now_naive() -> datetime:
|
||||||
|
"""Return a UTC timestamp stored as a naive datetime."""
|
||||||
|
return datetime.now(UTC).replace(tzinfo=None)
|
||||||
|
|
||||||
|
|
||||||
class JSONBCompat(TypeDecorator):
|
class JSONBCompat(TypeDecorator):
|
||||||
"""JSONB for PostgreSQL and JSON for SQLite/testing backends."""
|
"""JSONB for PostgreSQL and JSON for SQLite/testing backends."""
|
||||||
|
|
||||||
@@ -39,21 +66,45 @@ class JobStatus(StrEnum):
|
|||||||
QUEUED = "queued"
|
QUEUED = "queued"
|
||||||
PROCESSING = "processing"
|
PROCESSING = "processing"
|
||||||
TRANSCRIBED = "transcribed"
|
TRANSCRIBED = "transcribed"
|
||||||
COMPLETED = "completed"
|
|
||||||
PARTIAL_SUCCESS = "partial_success"
|
PARTIAL_SUCCESS = "partial_success"
|
||||||
FAILED = "failed"
|
FAILED = "failed"
|
||||||
|
|
||||||
|
|
||||||
class DocumentPersonRole(StrEnum):
|
|
||||||
AUTHOR = "author"
|
|
||||||
RECIPIENT = "recipient"
|
|
||||||
MENTIONED = "mentioned"
|
|
||||||
|
|
||||||
|
|
||||||
class JobSourceStatus(StrEnum):
|
class JobSourceStatus(StrEnum):
|
||||||
PENDING = "pending"
|
PENDING = "pending"
|
||||||
TRANSCRIBED = "transcribed"
|
TRANSCRIBED = "transcribed"
|
||||||
FAILED = "failed"
|
FAILED = "failed"
|
||||||
|
CANCELLED = "cancelled"
|
||||||
|
|
||||||
|
|
||||||
|
class JobPurpose(StrEnum):
|
||||||
|
TRANSCRIPTION = "transcription"
|
||||||
|
RETRANSCRIPTION = "retranscription"
|
||||||
|
|
||||||
|
|
||||||
|
class MaintenanceJobType(StrEnum):
|
||||||
|
BACKUP = "backup"
|
||||||
|
STORAGE_RECONCILIATION = "storage_reconciliation"
|
||||||
|
GEDCOM_IMPORT = "gedcom_import"
|
||||||
|
|
||||||
|
|
||||||
|
class MaintenanceRunStatus(StrEnum):
|
||||||
|
QUEUED = "queued"
|
||||||
|
PROCESSING = "processing"
|
||||||
|
SUCCEEDED = "succeeded"
|
||||||
|
FAILED = "failed"
|
||||||
|
|
||||||
|
|
||||||
|
class GenealogyCitationFactType(StrEnum):
|
||||||
|
BIRTH = "birth"
|
||||||
|
DEATH = "death"
|
||||||
|
MARRIAGE = "marriage"
|
||||||
|
OTHER = "other"
|
||||||
|
|
||||||
|
|
||||||
|
class GenealogyCitationSourceKind(StrEnum):
|
||||||
|
FAMILYSEARCH_IMPORTED = "familysearch_imported"
|
||||||
|
TRANSCRIPTION_EVIDENCE = "transcription_evidence"
|
||||||
|
|
||||||
|
|
||||||
class DocumentType(SQLModel, table=True):
|
class DocumentType(SQLModel, table=True):
|
||||||
@@ -62,14 +113,18 @@ class DocumentType(SQLModel, table=True):
|
|||||||
__tablename__ = "document_type"
|
__tablename__ = "document_type"
|
||||||
|
|
||||||
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
semantic_key: str | None = Field(default=None, index=True, unique=True)
|
||||||
label: str
|
label: str
|
||||||
normalized_label: str = Field(index=True, unique=True)
|
normalized_label: str = Field(index=True, unique=True)
|
||||||
is_active: bool = True
|
is_active: bool = True
|
||||||
created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
updated_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
updated_at: datetime = Field(
|
||||||
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
|
)
|
||||||
|
|
||||||
documents: list["Document"] = Relationship(
|
documents: list["Document"] = Relationship(
|
||||||
back_populates="document_type_ref", sa_relationship_kwargs={"lazy": "selectin"}
|
back_populates="document_type_ref", sa_relationship_kwargs={"lazy": "raise"}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -79,14 +134,44 @@ class PersonRole(SQLModel, table=True):
|
|||||||
__tablename__ = "person_role"
|
__tablename__ = "person_role"
|
||||||
|
|
||||||
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
code: str = Field(index=True, unique=True)
|
semantic_key: str | None = Field(default=None, index=True, unique=True)
|
||||||
label: str
|
label: str
|
||||||
|
normalized_label: str = Field(index=True, unique=True)
|
||||||
is_active: bool = True
|
is_active: bool = True
|
||||||
created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
updated_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
updated_at: datetime = Field(
|
||||||
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
|
)
|
||||||
|
|
||||||
document_people: list["DocumentPerson"] = Relationship(
|
document_people: list["DocumentPerson"] = Relationship(
|
||||||
back_populates="role_ref", sa_relationship_kwargs={"lazy": "selectin"}
|
back_populates="role_ref", sa_relationship_kwargs={"lazy": "raise"}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Tag(SQLModel, table=True):
|
||||||
|
"""Registry of labels that can be attached to Documents."""
|
||||||
|
|
||||||
|
__tablename__ = "tag"
|
||||||
|
|
||||||
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
semantic_key: str | None = Field(default=None, index=True, unique=True)
|
||||||
|
label: str
|
||||||
|
normalized_label: str = Field(index=True, unique=True)
|
||||||
|
is_active: bool = True
|
||||||
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
|
updated_at: datetime = Field(
|
||||||
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
|
)
|
||||||
|
|
||||||
|
document_tags: list["DocumentTag"] = Relationship(
|
||||||
|
back_populates="tag_ref",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
person_tags: list["PersonTag"] = Relationship(
|
||||||
|
back_populates="tag_ref",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -95,22 +180,29 @@ class Document(SQLModel, table=True):
|
|||||||
|
|
||||||
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
name: str
|
name: str
|
||||||
document_type_id: UUID | None = Field(default=None, foreign_key="document_type.id")
|
document_type_id: UUID | None = Field(default=None, foreign_key="document_type.id", index=True)
|
||||||
document_date: date | None = None
|
document_date: date | None = None
|
||||||
document_date_raw: str | None = None
|
document_date_raw: str | None = None
|
||||||
location_created: str | None = None
|
location_created: str | None = None
|
||||||
notes: str | None = None
|
notes: str | None = None
|
||||||
archive_identifier: str | None = None
|
archive_identifier: str | None = None
|
||||||
created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
updated_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
updated_at: datetime = Field(
|
||||||
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
|
)
|
||||||
|
|
||||||
jobs: list["Job"] = Relationship(back_populates="document", sa_relationship_kwargs={"lazy": "selectin"})
|
jobs: list["Job"] = Relationship(back_populates="document", sa_relationship_kwargs={"lazy": "raise"})
|
||||||
sources: list["Source"] = Relationship(back_populates="document", sa_relationship_kwargs={"lazy": "selectin"})
|
sources: list["Source"] = Relationship(back_populates="document", sa_relationship_kwargs={"lazy": "raise"})
|
||||||
document_people: list["DocumentPerson"] = Relationship(
|
document_people: list["DocumentPerson"] = Relationship(
|
||||||
back_populates="document", sa_relationship_kwargs={"lazy": "selectin"}
|
back_populates="document", sa_relationship_kwargs={"lazy": "raise"}
|
||||||
|
)
|
||||||
|
document_tags: list["DocumentTag"] = Relationship(
|
||||||
|
back_populates="document",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
)
|
)
|
||||||
document_type_ref: Optional["DocumentType"] = Relationship(
|
document_type_ref: Optional["DocumentType"] = Relationship(
|
||||||
back_populates="documents", sa_relationship_kwargs={"lazy": "selectin"}
|
back_populates="documents", sa_relationship_kwargs={"lazy": "raise"}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -118,9 +210,8 @@ class Person(SQLModel, table=True):
|
|||||||
"""A historical person linked to one or more documents."""
|
"""A historical person linked to one or more documents."""
|
||||||
|
|
||||||
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
full_name: str
|
last_name: str
|
||||||
display_name: str | None = None
|
given_names: str
|
||||||
maiden_name: str | None = None
|
|
||||||
birth_date: date | None = None
|
birth_date: date | None = None
|
||||||
birth_date_raw: str | None = None
|
birth_date_raw: str | None = None
|
||||||
birth_place: str | None = None
|
birth_place: str | None = None
|
||||||
@@ -128,17 +219,194 @@ class Person(SQLModel, table=True):
|
|||||||
death_date_raw: str | None = None
|
death_date_raw: str | None = None
|
||||||
death_place: str | None = None
|
death_place: str | None = None
|
||||||
biography: str | None = None
|
biography: str | None = None
|
||||||
portrait_path: str | None = None
|
|
||||||
family_search_id: str | None = Field(default=None, unique=True)
|
family_search_id: str | None = Field(default=None, unique=True)
|
||||||
metadata_: dict[str, JsonValue] | None = Field(
|
metadata_: dict[str, JsonValue] | None = Field(
|
||||||
default=None,
|
default=None,
|
||||||
sa_column=Column("metadata", JSONBCompat(), nullable=True),
|
sa_column=Column("metadata", JSONBCompat(), nullable=True),
|
||||||
)
|
)
|
||||||
created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
updated_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
updated_at: datetime = Field(
|
||||||
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
|
)
|
||||||
|
|
||||||
document_people: list["DocumentPerson"] = Relationship(
|
document_people: list["DocumentPerson"] = Relationship(
|
||||||
back_populates="person", sa_relationship_kwargs={"lazy": "selectin"}
|
back_populates="person", sa_relationship_kwargs={"lazy": "raise"}
|
||||||
|
)
|
||||||
|
person_tags: list["PersonTag"] = Relationship(
|
||||||
|
back_populates="person",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
photos: list["Photo"] = Relationship(
|
||||||
|
back_populates="person",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def full_name(self) -> str:
|
||||||
|
"""Presentation-friendly combined name."""
|
||||||
|
return f"{self.given_names} {self.last_name}".strip()
|
||||||
|
|
||||||
|
|
||||||
|
class GenealogyPerson(SQLModel, table=True):
|
||||||
|
"""An individual imported from a GEDCOM export."""
|
||||||
|
|
||||||
|
__tablename__ = "genealogy_person"
|
||||||
|
|
||||||
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
fs_id: str = Field(index=True, unique=True)
|
||||||
|
full_name: str
|
||||||
|
birth_date: date | None = None
|
||||||
|
birth_date_raw: str | None = None
|
||||||
|
birth_place: str | None = None
|
||||||
|
death_date: date | None = None
|
||||||
|
death_date_raw: str | None = None
|
||||||
|
death_place: str | None = None
|
||||||
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
|
updated_at: datetime = Field(
|
||||||
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
|
)
|
||||||
|
|
||||||
|
husband_families: list["GenealogyFamily"] = Relationship(
|
||||||
|
back_populates="husband",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise", "foreign_keys": "[GenealogyFamily.husband_id]"},
|
||||||
|
)
|
||||||
|
wife_families: list["GenealogyFamily"] = Relationship(
|
||||||
|
back_populates="wife",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise", "foreign_keys": "[GenealogyFamily.wife_id]"},
|
||||||
|
)
|
||||||
|
child_family_memberships: list["GenealogyFamilyChild"] = Relationship(
|
||||||
|
back_populates="child",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
citations: list["GenealogyCitation"] = Relationship(
|
||||||
|
back_populates="genealogy_person",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class GenealogyFamily(SQLModel, table=True):
|
||||||
|
"""A family linking two GenealogyPerson records."""
|
||||||
|
|
||||||
|
__tablename__ = "genealogy_family"
|
||||||
|
|
||||||
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
fs_family_id: str = Field(index=True, unique=True)
|
||||||
|
husband_id: UUID | None = Field(default=None, foreign_key="genealogy_person.id", index=True)
|
||||||
|
wife_id: UUID | None = Field(default=None, foreign_key="genealogy_person.id", index=True)
|
||||||
|
marriage_date: date | None = None
|
||||||
|
marriage_date_raw: str | None = None
|
||||||
|
marriage_place: str | None = None
|
||||||
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
|
updated_at: datetime = Field(
|
||||||
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
|
)
|
||||||
|
|
||||||
|
husband: Optional["GenealogyPerson"] = Relationship(
|
||||||
|
back_populates="husband_families",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise", "foreign_keys": "[GenealogyFamily.husband_id]"},
|
||||||
|
)
|
||||||
|
wife: Optional["GenealogyPerson"] = Relationship(
|
||||||
|
back_populates="wife_families",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise", "foreign_keys": "[GenealogyFamily.wife_id]"},
|
||||||
|
)
|
||||||
|
children: list["GenealogyFamilyChild"] = Relationship(
|
||||||
|
back_populates="family",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
citations: list["GenealogyCitation"] = Relationship(
|
||||||
|
back_populates="genealogy_family",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class GenealogyFamilyChild(SQLModel, table=True):
|
||||||
|
"""Junction table for child membership within a genealogy family."""
|
||||||
|
|
||||||
|
__tablename__ = "genealogy_family_child"
|
||||||
|
|
||||||
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
family_id: UUID = Field(foreign_key="genealogy_family.id", index=True)
|
||||||
|
child_id: UUID = Field(foreign_key="genealogy_person.id", index=True)
|
||||||
|
relationship_type: str | None = None
|
||||||
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
|
|
||||||
|
__table_args__ = (UniqueConstraint("family_id", "child_id", name="uq_genealogy_family_child"),)
|
||||||
|
|
||||||
|
family: Optional["GenealogyFamily"] = Relationship(
|
||||||
|
back_populates="children",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
child: Optional["GenealogyPerson"] = Relationship(
|
||||||
|
back_populates="child_family_memberships",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class GenealogyCitation(SQLModel, table=True):
|
||||||
|
"""A source citation attached to a genealogical fact."""
|
||||||
|
|
||||||
|
__tablename__ = "genealogy_citation"
|
||||||
|
|
||||||
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
genealogy_person_id: UUID | None = Field(default=None, foreign_key="genealogy_person.id", index=True)
|
||||||
|
genealogy_family_id: UUID | None = Field(default=None, foreign_key="genealogy_family.id", index=True)
|
||||||
|
fact_type: GenealogyCitationFactType = Field(
|
||||||
|
sa_column=Column(
|
||||||
|
SAEnum(
|
||||||
|
GenealogyCitationFactType,
|
||||||
|
values_callable=lambda enum_cls: [item.value for item in enum_cls],
|
||||||
|
native_enum=False,
|
||||||
|
),
|
||||||
|
nullable=False,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
raw_citation_text: str
|
||||||
|
source_kind: GenealogyCitationSourceKind = Field(
|
||||||
|
sa_column=Column(
|
||||||
|
SAEnum(
|
||||||
|
GenealogyCitationSourceKind,
|
||||||
|
values_callable=lambda enum_cls: [item.value for item in enum_cls],
|
||||||
|
native_enum=False,
|
||||||
|
),
|
||||||
|
nullable=False,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
document_id: UUID | None = Field(default=None, foreign_key="document.id", index=True)
|
||||||
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
|
|
||||||
|
genealogy_person: Optional["GenealogyPerson"] = Relationship(
|
||||||
|
back_populates="citations",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
genealogy_family: Optional["GenealogyFamily"] = Relationship(
|
||||||
|
back_populates="citations",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
document: Optional["Document"] = Relationship(sa_relationship_kwargs={"lazy": "raise"})
|
||||||
|
|
||||||
|
|
||||||
|
class Photo(SQLModel, table=True):
|
||||||
|
"""A reusable image record for Person and homepage galleries."""
|
||||||
|
|
||||||
|
__tablename__ = "photo"
|
||||||
|
|
||||||
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
person_id: UUID | None = Field(default=None, foreign_key="person.id", index=True)
|
||||||
|
path: str
|
||||||
|
description: str | None = None
|
||||||
|
is_primary: bool = False
|
||||||
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
|
updated_at: datetime = Field(
|
||||||
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
|
)
|
||||||
|
|
||||||
|
person: Optional["Person"] = Relationship(
|
||||||
|
back_populates="photos",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -148,34 +416,87 @@ class DocumentPerson(SQLModel, table=True):
|
|||||||
__tablename__ = "document_person"
|
__tablename__ = "document_person"
|
||||||
|
|
||||||
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
document_id: UUID = Field(foreign_key="document.id")
|
document_id: UUID = Field(foreign_key="document.id", index=True)
|
||||||
person_id: UUID = Field(foreign_key="person.id")
|
person_id: UUID = Field(foreign_key="person.id", index=True)
|
||||||
role_id: UUID | None = Field(default=None, foreign_key="person_role.id")
|
role_id: UUID = Field(foreign_key="person_role.id", index=True)
|
||||||
role: str = Field(default=DocumentPersonRole.AUTHOR.value, nullable=False)
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
updated_at: datetime = Field(
|
||||||
updated_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
__table_args__ = (
|
|
||||||
UniqueConstraint("document_id", "person_id", "role_id", name="uq_document_person_role_id"),
|
|
||||||
UniqueConstraint("document_id", "person_id", "role", name="uq_document_person_role"),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
__table_args__ = (UniqueConstraint("document_id", "person_id", name="uq_document_person"),)
|
||||||
|
|
||||||
document: Optional["Document"] = Relationship(
|
document: Optional["Document"] = Relationship(
|
||||||
back_populates="document_people", sa_relationship_kwargs={"lazy": "selectin"}
|
back_populates="document_people", sa_relationship_kwargs={"lazy": "raise"}
|
||||||
)
|
)
|
||||||
person: Optional["Person"] = Relationship(
|
person: Optional["Person"] = Relationship(
|
||||||
back_populates="document_people", sa_relationship_kwargs={"lazy": "selectin"}
|
back_populates="document_people", sa_relationship_kwargs={"lazy": "raise"}
|
||||||
)
|
)
|
||||||
role_ref: Optional["PersonRole"] = Relationship(
|
role_ref: Optional["PersonRole"] = Relationship(
|
||||||
back_populates="document_people", sa_relationship_kwargs={"lazy": "selectin"}
|
back_populates="document_people", sa_relationship_kwargs={"lazy": "raise"}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class DocumentTag(SQLModel, table=True):
|
||||||
|
"""Associates Documents with Tags."""
|
||||||
|
|
||||||
|
__tablename__ = "document_tag"
|
||||||
|
|
||||||
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
document_id: UUID = Field(foreign_key="document.id", index=True)
|
||||||
|
tag_id: UUID = Field(foreign_key="tag.id", index=True)
|
||||||
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
|
updated_at: datetime = Field(
|
||||||
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
|
)
|
||||||
|
|
||||||
|
__table_args__ = (UniqueConstraint("document_id", "tag_id", name="uq_document_tag"),)
|
||||||
|
|
||||||
|
document: Optional["Document"] = Relationship(
|
||||||
|
back_populates="document_tags",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
tag_ref: Optional["Tag"] = Relationship(
|
||||||
|
back_populates="document_tags",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class PersonTag(SQLModel, table=True):
|
||||||
|
"""Associates People with Tags."""
|
||||||
|
|
||||||
|
__tablename__ = "person_tag"
|
||||||
|
|
||||||
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
person_id: UUID = Field(foreign_key="person.id", index=True)
|
||||||
|
tag_id: UUID = Field(foreign_key="tag.id", index=True)
|
||||||
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
|
updated_at: datetime = Field(
|
||||||
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
|
)
|
||||||
|
|
||||||
|
__table_args__ = (UniqueConstraint("person_id", "tag_id", name="uq_person_tag"),)
|
||||||
|
|
||||||
|
person: Optional["Person"] = Relationship(
|
||||||
|
back_populates="person_tags",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
|
)
|
||||||
|
tag_ref: Optional["Tag"] = Relationship(
|
||||||
|
back_populates="person_tags",
|
||||||
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Job(SQLModel, table=True):
|
class Job(SQLModel, table=True):
|
||||||
"""A transcription job tied to a single document."""
|
"""A transcription job tied to a single document."""
|
||||||
|
|
||||||
|
__table_args__ = (Index("ix_job_status_date_created", "status", "date_created"),)
|
||||||
|
|
||||||
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
document_id: UUID = Field(foreign_key="document.id")
|
document_id: UUID = Field(foreign_key="document.id", index=True)
|
||||||
status: JobStatus = Field(
|
status: JobStatus = Field(
|
||||||
default=JobStatus.QUEUED,
|
default=JobStatus.QUEUED,
|
||||||
sa_column=Column(
|
sa_column=Column(
|
||||||
@@ -188,8 +509,23 @@ class Job(SQLModel, table=True):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
retry_count: int = Field(default=0, ge=0)
|
retry_count: int = Field(default=0, ge=0)
|
||||||
date_created: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
purpose: JobPurpose = Field(
|
||||||
date_updated: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
default=JobPurpose.TRANSCRIPTION,
|
||||||
|
sa_column=Column(
|
||||||
|
SAEnum(
|
||||||
|
JobPurpose,
|
||||||
|
values_callable=lambda enum_cls: [item.value for item in enum_cls],
|
||||||
|
native_enum=False,
|
||||||
|
),
|
||||||
|
nullable=False,
|
||||||
|
default=JobPurpose.TRANSCRIPTION.value,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
date_created: datetime = Field(default_factory=_utc_now_naive)
|
||||||
|
date_updated: datetime = Field(
|
||||||
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
|
)
|
||||||
provider: str | None = None
|
provider: str | None = None
|
||||||
model: str | None = None
|
model: str | None = None
|
||||||
prompt_name: str | None = None
|
prompt_name: str | None = None
|
||||||
@@ -199,48 +535,72 @@ class Job(SQLModel, table=True):
|
|||||||
temperature: float | None = None
|
temperature: float | None = None
|
||||||
top_p: float | None = None
|
top_p: float | None = None
|
||||||
|
|
||||||
document: Optional["Document"] = Relationship(back_populates="jobs", sa_relationship_kwargs={"lazy": "selectin"})
|
document: Optional["Document"] = Relationship(back_populates="jobs", sa_relationship_kwargs={"lazy": "raise"})
|
||||||
job_sources: list["JobSource"] = Relationship(back_populates="job", sa_relationship_kwargs={"lazy": "selectin"})
|
job_sources: list["JobSource"] = Relationship(back_populates="job", sa_relationship_kwargs={"lazy": "raise"})
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def filename(self) -> str:
|
def filename(self) -> str:
|
||||||
"""Return the filename of the associated source, when available."""
|
"""Return the filename of the first loaded source, when available.
|
||||||
if not self.job_sources:
|
|
||||||
return "unknown"
|
|
||||||
|
|
||||||
for job_source in self.job_sources:
|
|
||||||
source = job_source.__dict__.get("source")
|
|
||||||
if source is None:
|
|
||||||
try:
|
|
||||||
source = job_source.source
|
|
||||||
except DetachedInstanceError:
|
|
||||||
source = None
|
|
||||||
except Exception: # noqa: BLE001
|
|
||||||
source = None
|
|
||||||
|
|
||||||
|
Relationships on this model use ``lazy="raise"``, so this deliberately
|
||||||
|
inspects load state rather than triggering (or swallowing) a lazy load:
|
||||||
|
a read model that did not eager-load its sources gets "unknown" instead
|
||||||
|
of an unhandled error, and genuine errors are no longer hidden.
|
||||||
|
"""
|
||||||
|
for job_source in _loaded_attribute(self, "job_sources") or ():
|
||||||
|
source = _loaded_attribute(job_source, "source")
|
||||||
if source is not None:
|
if source is not None:
|
||||||
return source.filename
|
return source.filename
|
||||||
|
|
||||||
return "unknown"
|
return "unknown"
|
||||||
|
|
||||||
@property
|
|
||||||
def error_detail(self) -> str | None:
|
|
||||||
"""Return the first available source-level error detail for the job."""
|
|
||||||
if not self.job_sources:
|
|
||||||
return None
|
|
||||||
|
|
||||||
for job_source in self.job_sources:
|
class MaintenanceRun(SQLModel, table=True):
|
||||||
if job_source.error_detail:
|
"""A queued/processed maintenance task execution record."""
|
||||||
return job_source.error_detail
|
|
||||||
|
|
||||||
return None
|
__tablename__ = "maintenance_run"
|
||||||
|
|
||||||
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
job_type: MaintenanceJobType = Field(
|
||||||
|
sa_column=Column(
|
||||||
|
SAEnum(
|
||||||
|
MaintenanceJobType,
|
||||||
|
values_callable=lambda enum_cls: [item.value for item in enum_cls],
|
||||||
|
native_enum=False,
|
||||||
|
),
|
||||||
|
nullable=False,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
status: MaintenanceRunStatus = Field(
|
||||||
|
default=MaintenanceRunStatus.QUEUED,
|
||||||
|
sa_column=Column(
|
||||||
|
SAEnum(
|
||||||
|
MaintenanceRunStatus,
|
||||||
|
values_callable=lambda enum_cls: [item.value for item in enum_cls],
|
||||||
|
native_enum=False,
|
||||||
|
),
|
||||||
|
nullable=False,
|
||||||
|
default=MaintenanceRunStatus.QUEUED.value,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
started_at: datetime | None = None
|
||||||
|
finished_at: datetime | None = None
|
||||||
|
triggered_by: str | None = None
|
||||||
|
summary: str | None = None
|
||||||
|
log_path: str | None = None
|
||||||
|
error_detail: str | None = None
|
||||||
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
|
updated_at: datetime = Field(
|
||||||
|
default_factory=_utc_now_naive,
|
||||||
|
sa_column_kwargs={"onupdate": _utc_now_naive},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Source(SQLModel, table=True):
|
class Source(SQLModel, table=True):
|
||||||
"""A document source image or PDF page."""
|
"""A document source image or PDF page."""
|
||||||
|
|
||||||
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
document_id: UUID = Field(foreign_key="document.id")
|
document_id: UUID = Field(foreign_key="document.id", index=True)
|
||||||
page_number: int = Field(default=1, ge=1)
|
page_number: int = Field(default=1, ge=1)
|
||||||
upload_name: str
|
upload_name: str
|
||||||
filename: str
|
filename: str
|
||||||
@@ -248,29 +608,49 @@ class Source(SQLModel, table=True):
|
|||||||
file_hash: str
|
file_hash: str
|
||||||
file_size_bytes: int = Field(sa_column=Column(BigInteger(), nullable=False))
|
file_size_bytes: int = Field(sa_column=Column(BigInteger(), nullable=False))
|
||||||
raw_transcription: str | None = None
|
raw_transcription: str | None = None
|
||||||
|
preferred_execution_attempt_id: UUID | None = Field(
|
||||||
|
default=None,
|
||||||
|
sa_column=Column(
|
||||||
|
Uuid(),
|
||||||
|
# use_alter breaks the source / job_source / execution_attempt cycle so
|
||||||
|
# metadata.create_all can order table creation on every dialect.
|
||||||
|
ForeignKey(
|
||||||
|
"execution_attempt.id",
|
||||||
|
use_alter=True,
|
||||||
|
name="fk_source_preferred_execution_attempt_id",
|
||||||
|
),
|
||||||
|
nullable=True,
|
||||||
|
index=True,
|
||||||
|
),
|
||||||
|
)
|
||||||
revised_text: str | None = None
|
revised_text: str | None = None
|
||||||
date_uploaded: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
date_uploaded: datetime = Field(default_factory=_utc_now_naive)
|
||||||
date_revised: datetime | None = None
|
date_revised: datetime | None = None
|
||||||
|
|
||||||
document: Optional["Document"] = Relationship(
|
document: Optional["Document"] = Relationship(
|
||||||
back_populates="sources",
|
back_populates="sources",
|
||||||
sa_relationship_kwargs={"lazy": "selectin"},
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
)
|
)
|
||||||
job_sources: list["JobSource"] = Relationship(
|
job_sources: list["JobSource"] = Relationship(
|
||||||
back_populates="source",
|
back_populates="source",
|
||||||
sa_relationship_kwargs={"lazy": "selectin"},
|
sa_relationship_kwargs={"lazy": "raise"},
|
||||||
)
|
|
||||||
processing_artifacts: list["ProcessingArtifact"] = Relationship(
|
|
||||||
back_populates="source",
|
|
||||||
sa_relationship_kwargs={"lazy": "noload"},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def latest_job_source(self) -> Optional["JobSource"]:
|
def latest_job_source(self) -> Optional["JobSource"]:
|
||||||
"""Return the most recent job execution record for this source."""
|
"""Return the most recent job execution record for this source.
|
||||||
if not self.job_sources:
|
|
||||||
return None
|
``JobSource`` carries no timestamp of its own, so recency is the parent
|
||||||
return max(self.job_sources, key=lambda js: js.executed_at)
|
job's creation time. ``(job_id, source_id)`` is unique per source, so
|
||||||
|
this is exactly "the most recent job that included this page".
|
||||||
|
"""
|
||||||
|
job_sources = _loaded_attribute(self, "job_sources") or ()
|
||||||
|
dated = [
|
||||||
|
(job, job_source) for job_source in job_sources if (job := _loaded_attribute(job_source, "job")) is not None
|
||||||
|
]
|
||||||
|
if dated:
|
||||||
|
return max(dated, key=lambda pair: pair[0].date_created)[1]
|
||||||
|
return job_sources[0] if job_sources else None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def latest_status(self) -> JobSourceStatus | None:
|
def latest_status(self) -> JobSourceStatus | None:
|
||||||
@@ -280,9 +660,19 @@ class Source(SQLModel, table=True):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def latest_error_detail(self) -> str | None:
|
def latest_error_detail(self) -> str | None:
|
||||||
"""Return the error detail from the latest job run, if present."""
|
"""Return the error detail of the latest attempt on the latest job run.
|
||||||
|
|
||||||
|
Failure detail lives on ``ExecutionAttempt``; ``JobSource`` records only
|
||||||
|
which page a job is working on and how far it got.
|
||||||
|
"""
|
||||||
latest = self.latest_job_source
|
latest = self.latest_job_source
|
||||||
return latest.error_detail if latest else None
|
if latest is None:
|
||||||
|
return None
|
||||||
|
attempts = _loaded_attribute(latest, "execution_attempts") or ()
|
||||||
|
if not attempts:
|
||||||
|
return None
|
||||||
|
latest_attempt = max(attempts, key=lambda item: item.attempt_number)
|
||||||
|
return latest_attempt.error_detail
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def document_name(self) -> str | None:
|
def document_name(self) -> str | None:
|
||||||
@@ -294,10 +684,11 @@ class JobSource(SQLModel, table=True):
|
|||||||
"""A single AI execution record for one source page."""
|
"""A single AI execution record for one source page."""
|
||||||
|
|
||||||
__tablename__ = "job_source"
|
__tablename__ = "job_source"
|
||||||
|
__table_args__ = (UniqueConstraint("job_id", "source_id", name="uq_job_source_job_source"),)
|
||||||
|
|
||||||
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
||||||
job_id: UUID = Field(foreign_key="job.id")
|
job_id: UUID = Field(foreign_key="job.id", index=True)
|
||||||
source_id: UUID = Field(foreign_key="source.id")
|
source_id: UUID = Field(foreign_key="source.id", index=True)
|
||||||
status: JobSourceStatus = Field(
|
status: JobSourceStatus = Field(
|
||||||
default=JobSourceStatus.PENDING,
|
default=JobSourceStatus.PENDING,
|
||||||
sa_column=Column(
|
sa_column=Column(
|
||||||
@@ -309,14 +700,9 @@ class JobSource(SQLModel, table=True):
|
|||||||
nullable=False,
|
nullable=False,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
raw_transcription: str | None = None
|
|
||||||
ai_metadata: dict[str, JsonValue] | None = Field(default=None, sa_column=Column(JSONBCompat(), nullable=True))
|
|
||||||
raw_api_response: dict[str, JsonValue] | None = Field(default=None, sa_column=Column(JSONBCompat(), nullable=True))
|
|
||||||
error_detail: str | None = None
|
|
||||||
executed_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
|
||||||
|
|
||||||
job: Optional["Job"] = Relationship(back_populates="job_sources", sa_relationship_kwargs={"lazy": "selectin"})
|
job: Optional["Job"] = Relationship(back_populates="job_sources", sa_relationship_kwargs={"lazy": "raise"})
|
||||||
source: Optional["Source"] = Relationship(back_populates="job_sources", sa_relationship_kwargs={"lazy": "selectin"})
|
source: Optional["Source"] = Relationship(back_populates="job_sources", sa_relationship_kwargs={"lazy": "raise"})
|
||||||
execution_attempts: list["ExecutionAttempt"] = Relationship(
|
execution_attempts: list["ExecutionAttempt"] = Relationship(
|
||||||
back_populates="job_source",
|
back_populates="job_source",
|
||||||
sa_relationship_kwargs={"lazy": "noload", "order_by": "ExecutionAttempt.attempt_number"},
|
sa_relationship_kwargs={"lazy": "noload", "order_by": "ExecutionAttempt.attempt_number"},
|
||||||
@@ -334,7 +720,19 @@ class ExecutionAttempt(SQLModel, table=True):
|
|||||||
job_id: UUID = Field(foreign_key="job.id", index=True)
|
job_id: UUID = Field(foreign_key="job.id", index=True)
|
||||||
source_id: UUID = Field(foreign_key="source.id", index=True)
|
source_id: UUID = Field(foreign_key="source.id", index=True)
|
||||||
attempt_number: int = Field(ge=1)
|
attempt_number: int = Field(ge=1)
|
||||||
status: JobSourceStatus
|
status: JobSourceStatus = Field(
|
||||||
|
sa_column=Column(
|
||||||
|
# Declared identically to job_source.status. Without values_callable
|
||||||
|
# SQLAlchemy persists enum *names*, which is defect [45]: the two
|
||||||
|
# columns spelled the same status differently and never compared equal.
|
||||||
|
SAEnum(
|
||||||
|
JobSourceStatus,
|
||||||
|
values_callable=lambda enum_cls: [item.value for item in enum_cls],
|
||||||
|
native_enum=False,
|
||||||
|
),
|
||||||
|
nullable=False,
|
||||||
|
)
|
||||||
|
)
|
||||||
provider: str
|
provider: str
|
||||||
model: str | None = None
|
model: str | None = None
|
||||||
request_manifest: dict[str, JsonValue] | None = Field(default=None, sa_column=Column(JSONBCompat(), nullable=True))
|
request_manifest: dict[str, JsonValue] | None = Field(default=None, sa_column=Column(JSONBCompat(), nullable=True))
|
||||||
@@ -364,43 +762,8 @@ class ExecutionAttempt(SQLModel, table=True):
|
|||||||
started_at: datetime
|
started_at: datetime
|
||||||
finished_at: datetime
|
finished_at: datetime
|
||||||
duration_ms: int = Field(ge=0)
|
duration_ms: int = Field(ge=0)
|
||||||
created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
created_at: datetime = Field(default_factory=_utc_now_naive)
|
||||||
|
|
||||||
job_source: Optional["JobSource"] = Relationship(back_populates="execution_attempts")
|
job_source: Optional["JobSource"] = Relationship(
|
||||||
artifacts: list["ProcessingArtifact"] = Relationship(
|
back_populates="execution_attempts", sa_relationship_kwargs={"lazy": "raise"}
|
||||||
back_populates="execution_attempt", sa_relationship_kwargs={"lazy": "noload"}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ProcessingArtifact(SQLModel, table=True):
|
|
||||||
"""Provider-neutral, versioned output derived from a Source."""
|
|
||||||
|
|
||||||
__tablename__ = "processing_artifact"
|
|
||||||
__table_args__ = (
|
|
||||||
CheckConstraint(
|
|
||||||
"(inline_payload IS NOT NULL AND external_reference IS NULL) OR "
|
|
||||||
"(inline_payload IS NULL AND external_reference IS NOT NULL)",
|
|
||||||
name="ck_processing_artifact_one_content_location",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
id: UUID = Field(default_factory=uuid4, primary_key=True)
|
|
||||||
source_id: UUID = Field(foreign_key="source.id", index=True)
|
|
||||||
execution_attempt_id: UUID | None = Field(default=None, foreign_key="execution_attempt.id", index=True)
|
|
||||||
artifact_type: str
|
|
||||||
media_type: str
|
|
||||||
schema_name: str
|
|
||||||
schema_version: str
|
|
||||||
producer: str
|
|
||||||
producer_version: str
|
|
||||||
inline_payload: dict[str, JsonValue] | None = Field(default=None, sa_column=Column(JSONBCompat(), nullable=True))
|
|
||||||
external_reference: str | None = None
|
|
||||||
payload_sha256: str = Field(index=True)
|
|
||||||
byte_size: int = Field(sa_column=Column(BigInteger(), nullable=False))
|
|
||||||
coordinate_metadata: dict[str, JsonValue] | None = Field(
|
|
||||||
default=None, sa_column=Column(JSONBCompat(), nullable=True)
|
|
||||||
)
|
|
||||||
created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
|
||||||
|
|
||||||
execution_attempt: Optional["ExecutionAttempt"] = Relationship(back_populates="artifacts")
|
|
||||||
source: Optional["Source"] = Relationship(back_populates="processing_artifacts")
|
|
||||||
|
|||||||
+203
-149
@@ -1,10 +1,10 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from sqlalchemy import inspect
|
from sqlalchemy import inspect as sqlalchemy_inspect
|
||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
from sqlalchemy.ext.asyncio import AsyncConnection
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncEngine
|
from sqlalchemy.ext.asyncio import AsyncEngine
|
||||||
from sqlalchemy.ext.asyncio import async_sessionmaker
|
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||||||
from sqlmodel import SQLModel
|
from sqlmodel import SQLModel
|
||||||
@@ -13,29 +13,13 @@ from sqlmodel.ext.asyncio.session import AsyncSession
|
|||||||
|
|
||||||
from .engine import resolve_engine
|
from .engine import resolve_engine
|
||||||
from .models import DocumentType
|
from .models import DocumentType
|
||||||
from .models import Job
|
|
||||||
from .models import JobStatus
|
|
||||||
from .models import PersonRole
|
from .models import PersonRole
|
||||||
|
from .registries import BUILT_IN_DOCUMENT_TYPES
|
||||||
|
from .registries import BUILT_IN_PERSON_ROLES
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_PERSON_ROLES: tuple[tuple[str, str], ...] = (
|
|
||||||
("author", "Author"),
|
|
||||||
("recipient", "Recipient"),
|
|
||||||
("mentioned", "Mentioned"),
|
|
||||||
)
|
|
||||||
|
|
||||||
DEFAULT_DOCUMENT_TYPES: tuple[str, ...] = (
|
|
||||||
"Letter",
|
|
||||||
"Record",
|
|
||||||
"Memo",
|
|
||||||
"Postcard",
|
|
||||||
"Journal",
|
|
||||||
"Note",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def create_all(*, engine: AsyncEngine | None = None) -> None:
|
async def create_all(*, engine: AsyncEngine | None = None) -> None:
|
||||||
"""Create any missing tables on the selected engine."""
|
"""Create any missing tables on the selected engine."""
|
||||||
# Import models so SQLModel metadata is fully registered before bootstrap.
|
# Import models so SQLModel metadata is fully registered before bootstrap.
|
||||||
@@ -44,123 +28,193 @@ async def create_all(*, engine: AsyncEngine | None = None) -> None:
|
|||||||
active_engine = engine or resolve_engine()
|
active_engine = engine or resolve_engine()
|
||||||
async with active_engine.begin() as connection:
|
async with active_engine.begin() as connection:
|
||||||
await connection.run_sync(SQLModel.metadata.create_all)
|
await connection.run_sync(SQLModel.metadata.create_all)
|
||||||
await _upgrade_document_type_uuid_identity(connection)
|
|
||||||
await _upgrade_person_family_search_id(connection)
|
|
||||||
await _upgrade_v42_evidence_tables(connection)
|
|
||||||
await seed_registry_defaults(engine=active_engine)
|
await seed_registry_defaults(engine=active_engine)
|
||||||
logger.debug("Database schema bootstrap complete for database_url=%s", active_engine.url)
|
logger.debug("Database schema bootstrap complete for database_url=%s", active_engine.url)
|
||||||
|
|
||||||
|
|
||||||
async def upgrade_schema(*, engine: AsyncEngine | None = None) -> None:
|
async def reconcile_legacy_job_source_columns(*, engine: AsyncEngine | None = None) -> int:
|
||||||
"""Apply non-destructive additive upgrades to an existing schema."""
|
"""Remove stale V4.6 ``job_source`` evidence columns from existing databases.
|
||||||
|
|
||||||
|
Runtime models define ``job_source`` as a queue/projection table only. If an
|
||||||
|
older database still carries the retired evidence columns, writes can fail
|
||||||
|
on stale constraints (for example ``executed_at NOT NULL``).
|
||||||
|
"""
|
||||||
active_engine = engine or resolve_engine()
|
active_engine = engine or resolve_engine()
|
||||||
async with active_engine.begin() as connection:
|
if not hasattr(active_engine, "begin"):
|
||||||
await _upgrade_document_type_uuid_identity(connection)
|
return 0
|
||||||
await _upgrade_person_family_search_id(connection)
|
|
||||||
await _upgrade_v42_evidence_tables(connection)
|
|
||||||
|
|
||||||
|
def _reconcile(sync_connection) -> int:
|
||||||
async def _upgrade_v42_evidence_tables(connection: AsyncConnection) -> None:
|
inspector = sqlalchemy_inspect(sync_connection)
|
||||||
"""Create the additive V4.2 evidence tables without rewriting historical rows."""
|
table_names = set(inspector.get_table_names())
|
||||||
|
if "job_source" not in table_names:
|
||||||
def create_tables(sync_connection) -> None:
|
return 0
|
||||||
SQLModel.metadata.tables["execution_attempt"].create(sync_connection, checkfirst=True)
|
present_columns = {column["name"] for column in inspector.get_columns("job_source")}
|
||||||
SQLModel.metadata.tables["processing_artifact"].create(sync_connection, checkfirst=True)
|
dropped = 0
|
||||||
|
for column_name in (
|
||||||
await connection.run_sync(create_tables)
|
"raw_transcription",
|
||||||
|
"ai_metadata",
|
||||||
|
"raw_api_response",
|
||||||
async def _upgrade_document_type_uuid_identity(connection: AsyncConnection) -> None:
|
"error_detail",
|
||||||
"""Backfill UUID references and retire legacy Document Type code/order columns."""
|
"executed_at",
|
||||||
|
|
||||||
def inspect_schema(sync_connection) -> tuple[set[str], set[str]]:
|
|
||||||
database = inspect(sync_connection)
|
|
||||||
tables = set(database.get_table_names())
|
|
||||||
type_columns = (
|
|
||||||
{column["name"] for column in database.get_columns("document_type")} if "document_type" in tables else set()
|
|
||||||
)
|
|
||||||
document_columns = (
|
|
||||||
{column["name"] for column in database.get_columns("document")} if "document" in tables else set()
|
|
||||||
)
|
|
||||||
return type_columns, document_columns
|
|
||||||
|
|
||||||
type_columns, document_columns = await connection.run_sync(inspect_schema)
|
|
||||||
if not type_columns:
|
|
||||||
return
|
|
||||||
|
|
||||||
if "normalized_label" not in type_columns:
|
|
||||||
await connection.execute(text("ALTER TABLE document_type ADD COLUMN normalized_label VARCHAR"))
|
|
||||||
await connection.execute(
|
|
||||||
text("UPDATE document_type SET normalized_label = lower(trim(label)) WHERE normalized_label IS NULL")
|
|
||||||
)
|
|
||||||
|
|
||||||
duplicates = (
|
|
||||||
await connection.execute(
|
|
||||||
text("SELECT normalized_label FROM document_type GROUP BY normalized_label HAVING count(*) > 1")
|
|
||||||
)
|
|
||||||
).first()
|
|
||||||
if duplicates is not None:
|
|
||||||
raise RuntimeError(
|
|
||||||
"Document Type migration requires unique labels ignoring case and whitespace; "
|
|
||||||
f"duplicate normalized label: {duplicates[0]!r}"
|
|
||||||
)
|
|
||||||
|
|
||||||
if "code" in type_columns and {"document_type", "document_type_id"}.issubset(document_columns):
|
|
||||||
await connection.execute(
|
|
||||||
text(
|
|
||||||
"UPDATE document SET document_type_id = ("
|
|
||||||
"SELECT id FROM document_type WHERE "
|
|
||||||
"lower(trim(document_type.code)) = lower(trim(document.document_type))"
|
|
||||||
") WHERE document_type_id IS NULL AND document_type IS NOT NULL"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
if connection.dialect.name == "postgresql":
|
|
||||||
await connection.execute(text("ALTER TABLE document_type ALTER COLUMN normalized_label SET NOT NULL"))
|
|
||||||
if "code" in type_columns:
|
|
||||||
await connection.execute(text("ALTER TABLE document_type DROP COLUMN code CASCADE"))
|
|
||||||
if "sort_order" in type_columns:
|
|
||||||
await connection.execute(text("ALTER TABLE document_type DROP COLUMN sort_order"))
|
|
||||||
if "document_type" in document_columns:
|
|
||||||
await connection.execute(text("ALTER TABLE document DROP COLUMN document_type"))
|
|
||||||
elif connection.dialect.name == "sqlite":
|
|
||||||
await connection.execute(text("DROP INDEX IF EXISTS ix_document_type_code"))
|
|
||||||
if "code" in type_columns:
|
|
||||||
await connection.execute(text("ALTER TABLE document_type DROP COLUMN code"))
|
|
||||||
if "sort_order" in type_columns:
|
|
||||||
await connection.execute(text("ALTER TABLE document_type DROP COLUMN sort_order"))
|
|
||||||
if "document_type" in document_columns:
|
|
||||||
await connection.execute(text("ALTER TABLE document DROP COLUMN document_type"))
|
|
||||||
|
|
||||||
await connection.execute(
|
|
||||||
text("CREATE UNIQUE INDEX IF NOT EXISTS ix_document_type_normalized_label ON document_type (normalized_label)")
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def _upgrade_person_family_search_id(connection: AsyncConnection) -> None:
|
|
||||||
"""Add the nullable V4.1 FamilySearch field to an existing database."""
|
|
||||||
|
|
||||||
def inspect_person(sync_connection) -> tuple[bool, bool]:
|
|
||||||
database = inspect(sync_connection)
|
|
||||||
if "person" not in database.get_table_names():
|
|
||||||
return False, False
|
|
||||||
columns = {column["name"] for column in database.get_columns("person")}
|
|
||||||
indexes = database.get_indexes("person")
|
|
||||||
constraints = database.get_unique_constraints("person")
|
|
||||||
has_unique_id = any(entry.get("column_names") == ["family_search_id"] for entry in [*indexes, *constraints])
|
|
||||||
return "family_search_id" in columns, has_unique_id
|
|
||||||
|
|
||||||
has_column, has_unique_id = await connection.run_sync(inspect_person)
|
|
||||||
if not has_column and not await connection.run_sync(
|
|
||||||
lambda sync_connection: "person" in inspect(sync_connection).get_table_names()
|
|
||||||
):
|
):
|
||||||
return
|
if column_name not in present_columns:
|
||||||
if not has_column:
|
continue
|
||||||
await connection.execute(text("ALTER TABLE person ADD COLUMN family_search_id VARCHAR"))
|
sync_connection.execute(text(f'alter table "job_source" drop column "{column_name}"'))
|
||||||
if not has_unique_id:
|
dropped += 1
|
||||||
await connection.execute(
|
return dropped
|
||||||
text("CREATE UNIQUE INDEX IF NOT EXISTS ix_person_family_search_id ON person (family_search_id)")
|
|
||||||
|
async with active_engine.begin() as connection:
|
||||||
|
dropped_columns = await connection.run_sync(_reconcile)
|
||||||
|
if dropped_columns:
|
||||||
|
logger.warning("Dropped %s legacy job_source column(s) during startup reconciliation", dropped_columns)
|
||||||
|
return dropped_columns
|
||||||
|
|
||||||
|
|
||||||
|
async def reconcile_canonical_media_paths(*, engine: AsyncEngine | None = None) -> int:
|
||||||
|
"""Normalize stored media paths to upload-root-relative POSIX form."""
|
||||||
|
active_engine = engine or resolve_engine()
|
||||||
|
if not hasattr(active_engine, "begin"):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def _reconcile(sync_connection) -> int:
|
||||||
|
rows_changed = 0
|
||||||
|
inspector = sqlalchemy_inspect(sync_connection)
|
||||||
|
table_names = set(inspector.get_table_names())
|
||||||
|
if "source" in table_names:
|
||||||
|
rows = (
|
||||||
|
sync_connection.execute(text('select id, file_path from "source" where file_path is not null'))
|
||||||
|
.mappings()
|
||||||
|
.all()
|
||||||
)
|
)
|
||||||
|
for row in rows:
|
||||||
|
original = str(row["file_path"])
|
||||||
|
normalized = _canonical_relative_path(original, preferred_prefix="documents/")
|
||||||
|
if normalized is None or normalized == original:
|
||||||
|
continue
|
||||||
|
sync_connection.execute(
|
||||||
|
text('update "source" set file_path = :file_path where id = :id'),
|
||||||
|
{"id": row["id"], "file_path": normalized},
|
||||||
|
)
|
||||||
|
rows_changed += 1
|
||||||
|
|
||||||
|
if "photo" in table_names:
|
||||||
|
rows = sync_connection.execute(text('select id, path from "photo" where path is not null')).mappings().all()
|
||||||
|
for row in rows:
|
||||||
|
original = str(row["path"])
|
||||||
|
normalized = _canonical_relative_path(original, preferred_prefix="photos/")
|
||||||
|
if normalized is None or normalized == original:
|
||||||
|
continue
|
||||||
|
sync_connection.execute(
|
||||||
|
text('update "photo" set path = :path where id = :id'),
|
||||||
|
{"id": row["id"], "path": normalized},
|
||||||
|
)
|
||||||
|
rows_changed += 1
|
||||||
|
return rows_changed
|
||||||
|
|
||||||
|
async with active_engine.begin() as connection:
|
||||||
|
rows_changed = await connection.run_sync(_reconcile)
|
||||||
|
if rows_changed:
|
||||||
|
logger.warning("Normalized %s media-path row(s) to canonical relative format", rows_changed)
|
||||||
|
return rows_changed
|
||||||
|
|
||||||
|
|
||||||
|
async def reconcile_person_name_columns(*, engine: AsyncEngine | None = None) -> int:
|
||||||
|
"""Backfill V5.1 Person name columns on existing databases."""
|
||||||
|
active_engine = engine or resolve_engine()
|
||||||
|
if not hasattr(active_engine, "begin"):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def _reconcile(sync_connection) -> int:
|
||||||
|
rows_changed = 0
|
||||||
|
inspector = sqlalchemy_inspect(sync_connection)
|
||||||
|
table_names = set(inspector.get_table_names())
|
||||||
|
if "person" not in table_names:
|
||||||
|
return 0
|
||||||
|
present_columns = {column["name"] for column in inspector.get_columns("person")}
|
||||||
|
if "last_name" not in present_columns:
|
||||||
|
sync_connection.execute(text('alter table "person" add column "last_name" varchar'))
|
||||||
|
if "given_names" not in present_columns:
|
||||||
|
sync_connection.execute(text('alter table "person" add column "given_names" varchar'))
|
||||||
|
|
||||||
|
query = (
|
||||||
|
text('select id, full_name, given_names, last_name from "person"')
|
||||||
|
if "full_name" in present_columns
|
||||||
|
else text('select id, null as full_name, given_names, last_name from "person"')
|
||||||
|
)
|
||||||
|
rows = sync_connection.execute(query).mappings().all()
|
||||||
|
for row in rows:
|
||||||
|
given_names = (str(row.get("given_names") or "")).strip()
|
||||||
|
last_name = (str(row.get("last_name") or "")).strip()
|
||||||
|
if given_names and last_name:
|
||||||
|
continue
|
||||||
|
tokens = [token for token in str(row.get("full_name") or "").split() if token]
|
||||||
|
if len(tokens) >= 2:
|
||||||
|
given_names, last_name = (" ".join(tokens[:-1]), tokens[-1])
|
||||||
|
elif len(tokens) == 1:
|
||||||
|
given_names = tokens[0]
|
||||||
|
last_name = tokens[0]
|
||||||
|
else:
|
||||||
|
given_names = "Unknown"
|
||||||
|
last_name = "Unknown"
|
||||||
|
sync_connection.execute(
|
||||||
|
text('update "person" set given_names = :given_names, last_name = :last_name where id = :id'),
|
||||||
|
{
|
||||||
|
"id": row["id"],
|
||||||
|
"given_names": given_names,
|
||||||
|
"last_name": last_name,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
rows_changed += 1
|
||||||
|
return rows_changed
|
||||||
|
|
||||||
|
async with active_engine.begin() as connection:
|
||||||
|
rows_changed = await connection.run_sync(_reconcile)
|
||||||
|
if rows_changed:
|
||||||
|
logger.warning("Backfilled V5.1 name columns for %s person row(s)", rows_changed)
|
||||||
|
return rows_changed
|
||||||
|
|
||||||
|
|
||||||
|
def _canonical_relative_path(value: str, *, preferred_prefix: str) -> str | None:
|
||||||
|
normalized = value.strip().replace("\\", "/")
|
||||||
|
if not normalized:
|
||||||
|
return None
|
||||||
|
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
if lowered.startswith(("http://", "https://", "data:")):
|
||||||
|
return None
|
||||||
|
|
||||||
|
if lowered.startswith("/uploads/"):
|
||||||
|
normalized = normalized[len("/uploads/") :]
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
elif lowered.startswith("uploads/"):
|
||||||
|
normalized = normalized[len("uploads/") :]
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
elif lowered.startswith("data/"):
|
||||||
|
normalized = normalized[len("data/") :]
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
|
||||||
|
for prefix in ("documents/", "photos/", "persons/", "portraits/"):
|
||||||
|
marker = f"/{prefix}"
|
||||||
|
index = lowered.find(marker)
|
||||||
|
if index >= 0:
|
||||||
|
normalized = normalized[index + 1 :]
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
break
|
||||||
|
if lowered.startswith(prefix):
|
||||||
|
break
|
||||||
|
|
||||||
|
if preferred_prefix == "persons/" and lowered.startswith("portraits/"):
|
||||||
|
normalized = "persons/" + normalized[len("portraits/") :]
|
||||||
|
lowered = normalized.casefold()
|
||||||
|
|
||||||
|
if not lowered.startswith(preferred_prefix):
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Collapse any accidental "." segments while preserving relative semantics.
|
||||||
|
collapsed = Path(normalized).as_posix()
|
||||||
|
if collapsed.startswith("../") or collapsed == "..":
|
||||||
|
return None
|
||||||
|
return collapsed
|
||||||
|
|
||||||
|
|
||||||
async def seed_registry_defaults(*, engine: AsyncEngine | None = None) -> None:
|
async def seed_registry_defaults(*, engine: AsyncEngine | None = None) -> None:
|
||||||
@@ -169,26 +223,26 @@ async def seed_registry_defaults(*, engine: AsyncEngine | None = None) -> None:
|
|||||||
session_factory = async_sessionmaker(active_engine, class_=AsyncSession, expire_on_commit=False)
|
session_factory = async_sessionmaker(active_engine, class_=AsyncSession, expire_on_commit=False)
|
||||||
|
|
||||||
async with session_factory() as session:
|
async with session_factory() as session:
|
||||||
role_codes = set((await session.exec(select(PersonRole.code))).all())
|
role_keys = set((await session.exec(select(PersonRole.semantic_key))).all())
|
||||||
for code, label in DEFAULT_PERSON_ROLES:
|
for semantic_key, label in BUILT_IN_PERSON_ROLES:
|
||||||
if code not in role_codes:
|
if semantic_key not in role_keys:
|
||||||
session.add(PersonRole(code=code, label=label))
|
session.add(
|
||||||
|
PersonRole(
|
||||||
|
semantic_key=semantic_key,
|
||||||
|
label=label,
|
||||||
|
normalized_label=label.casefold(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
type_labels = set((await session.exec(select(DocumentType.normalized_label))).all())
|
type_keys = set((await session.exec(select(DocumentType.semantic_key))).all())
|
||||||
for label in DEFAULT_DOCUMENT_TYPES:
|
for semantic_key, label in BUILT_IN_DOCUMENT_TYPES:
|
||||||
normalized_label = label.casefold()
|
if semantic_key not in type_keys:
|
||||||
if normalized_label not in type_labels:
|
session.add(
|
||||||
session.add(DocumentType(label=label, normalized_label=normalized_label))
|
DocumentType(
|
||||||
|
semantic_key=semantic_key,
|
||||||
|
label=label,
|
||||||
|
normalized_label=label.casefold(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
await session.commit()
|
await session.commit()
|
||||||
|
|
||||||
|
|
||||||
async def get_next_queued_job(*, session: AsyncSession) -> Job | None:
|
|
||||||
"""Get the next queued job, if any."""
|
|
||||||
result = await session.exec(
|
|
||||||
select(Job)
|
|
||||||
.where(Job.status == JobStatus.QUEUED)
|
|
||||||
.order_by(Job.date_created) # pyright: ignore[reportArgumentType]
|
|
||||||
.limit(1)
|
|
||||||
) # fmt: skip
|
|
||||||
return result.first()
|
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
"""Application-defined semantic registry entries."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
BUILT_IN_DOCUMENT_TYPES: tuple[tuple[str, str], ...] = (
|
||||||
|
("book", "Book"),
|
||||||
|
("letter", "Letter"),
|
||||||
|
("postcard", "Postcard"),
|
||||||
|
("photo", "Photo"),
|
||||||
|
("journal", "Journal"),
|
||||||
|
("form", "Form"),
|
||||||
|
)
|
||||||
|
|
||||||
|
BUILT_IN_PERSON_ROLES: tuple[tuple[str, str], ...] = (
|
||||||
|
("author", "Author"),
|
||||||
|
("recipient", "Recipient"),
|
||||||
|
("mentioned", "Mentioned"),
|
||||||
|
)
|
||||||
|
|
||||||
|
AUTHOR_ROLE_SEMANTIC_KEY = "author"
|
||||||
@@ -50,8 +50,7 @@ def initialize_database_runtime(*, settings: Settings | None = None) -> Database
|
|||||||
runtime_url = runtime.engine.url.render_as_string(hide_password=False)
|
runtime_url = runtime.engine.url.render_as_string(hide_password=False)
|
||||||
if runtime_url != database_url:
|
if runtime_url != database_url:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"Database runtime is already initialized for a different database: "
|
f"Database runtime is already initialized for a different database: {runtime_url!r} != {database_url!r}"
|
||||||
f"{runtime_url!r} != {database_url!r}"
|
|
||||||
)
|
)
|
||||||
return runtime
|
return runtime
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
from collections.abc import AsyncGenerator
|
from collections.abc import AsyncGenerator
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from functools import cache
|
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
|
||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
from sqlalchemy.ext.asyncio import AsyncSessionTransaction
|
|
||||||
from sqlalchemy.ext.asyncio import async_sessionmaker
|
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||||
|
|
||||||
@@ -17,13 +15,20 @@ from .engine import get_engine
|
|||||||
type SessionFactory = async_sessionmaker[AsyncSession]
|
type SessionFactory = async_sessionmaker[AsyncSession]
|
||||||
|
|
||||||
|
|
||||||
@cache
|
_SESSION_FACTORIES: dict[str, SessionFactory] = {}
|
||||||
|
|
||||||
|
|
||||||
def get_session_factory(database_url: str) -> SessionFactory:
|
def get_session_factory(database_url: str) -> SessionFactory:
|
||||||
return async_sessionmaker(
|
"""Return the process-wide session factory for ``database_url``."""
|
||||||
|
factory = _SESSION_FACTORIES.get(database_url)
|
||||||
|
if factory is None:
|
||||||
|
factory = async_sessionmaker(
|
||||||
bind=get_engine(database_url),
|
bind=get_engine(database_url),
|
||||||
class_=AsyncSession,
|
class_=AsyncSession,
|
||||||
expire_on_commit=False,
|
expire_on_commit=False,
|
||||||
)
|
)
|
||||||
|
_SESSION_FACTORIES[database_url] = factory
|
||||||
|
return factory
|
||||||
|
|
||||||
|
|
||||||
def resolve_session_factory(
|
def resolve_session_factory(
|
||||||
@@ -46,7 +51,8 @@ type SessionFactoryDep = Annotated[SessionFactory, Depends(resolve_session_facto
|
|||||||
|
|
||||||
|
|
||||||
async def dispose_session_factory(database_url: str) -> None:
|
async def dispose_session_factory(database_url: str) -> None:
|
||||||
get_session_factory.cache_clear()
|
"""Drop the session factory and engine for ``database_url`` only."""
|
||||||
|
_SESSION_FACTORIES.pop(database_url, None)
|
||||||
await dispose_engine(database_url)
|
await dispose_engine(database_url)
|
||||||
|
|
||||||
|
|
||||||
@@ -79,16 +85,12 @@ async def transaction_scope(
|
|||||||
settings: Settings | None = None,
|
settings: Settings | None = None,
|
||||||
database_url: str | None = None,
|
database_url: str | None = None,
|
||||||
session_factory: SessionFactory | None = None,
|
session_factory: SessionFactory | None = None,
|
||||||
session: AsyncSession | AsyncSessionTransaction | None = None,
|
session: AsyncSession | None = None,
|
||||||
) -> AsyncGenerator[AsyncSession | AsyncSessionTransaction]:
|
) -> AsyncGenerator[AsyncSession]:
|
||||||
match session:
|
if session is not None:
|
||||||
case AsyncSession() as async_session:
|
if not session.in_transaction():
|
||||||
if not async_session.in_transaction():
|
|
||||||
raise RuntimeError("A supplied session must have an active transaction")
|
raise RuntimeError("A supplied session must have an active transaction")
|
||||||
yield async_session
|
yield session
|
||||||
return
|
|
||||||
case AsyncSessionTransaction() as async_transaction:
|
|
||||||
yield async_transaction
|
|
||||||
return
|
return
|
||||||
|
|
||||||
active_session_factory = session_factory or resolve_session_factory(
|
active_session_factory = session_factory or resolve_session_factory(
|
||||||
@@ -99,7 +101,4 @@ async def transaction_scope(
|
|||||||
yield owned_session
|
yield owned_session
|
||||||
|
|
||||||
|
|
||||||
type TransactionScopeDep = Annotated[
|
type TransactionScopeDep = Annotated[AsyncSession, Depends(transaction_scope)]
|
||||||
AsyncSession | AsyncSessionTransaction,
|
|
||||||
Depends(transaction_scope),
|
|
||||||
]
|
|
||||||
|
|||||||
@@ -2,12 +2,15 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import UTC
|
from datetime import UTC
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ErrorCategory(StrEnum):
|
class ErrorCategory(StrEnum):
|
||||||
"""Stable error categories defined by docs/error_handling.md."""
|
"""Stable error categories defined by docs/error_handling.md."""
|
||||||
@@ -17,6 +20,7 @@ class ErrorCategory(StrEnum):
|
|||||||
NOT_FOUND = "not_found_error"
|
NOT_FOUND = "not_found_error"
|
||||||
CONFLICT = "conflict_error"
|
CONFLICT = "conflict_error"
|
||||||
EXTERNAL_PROVIDER = "external_provider_error"
|
EXTERNAL_PROVIDER = "external_provider_error"
|
||||||
|
EXTERNAL_TIMEOUT = "external_timeout_error"
|
||||||
PROCESSING = "processing_error"
|
PROCESSING = "processing_error"
|
||||||
INFRA_TRANSIENT = "infrastructure_transient_error"
|
INFRA_TRANSIENT = "infrastructure_transient_error"
|
||||||
INFRA_PERSISTENT = "infrastructure_persistent_error"
|
INFRA_PERSISTENT = "infrastructure_persistent_error"
|
||||||
@@ -28,6 +32,11 @@ def new_error_id() -> str:
|
|||||||
return uuid4().hex[:8]
|
return uuid4().hex[:8]
|
||||||
|
|
||||||
|
|
||||||
|
def exception_detail(exc: BaseException) -> str:
|
||||||
|
"""Return internal-only root-cause text for persisted diagnostics."""
|
||||||
|
return f"{type(exc).__name__}: {exc}"
|
||||||
|
|
||||||
|
|
||||||
class AppError(RuntimeError):
|
class AppError(RuntimeError):
|
||||||
"""Base application error carrying user-safe handling metadata."""
|
"""Base application error carrying user-safe handling metadata."""
|
||||||
|
|
||||||
@@ -39,6 +48,7 @@ class AppError(RuntimeError):
|
|||||||
suggestion: str = "Retry once. If it persists, review logs and report the error reference id.",
|
suggestion: str = "Retry once. If it persists, review logs and report the error reference id.",
|
||||||
retriable: bool = False,
|
retriable: bool = False,
|
||||||
error_id: str | None = None,
|
error_id: str | None = None,
|
||||||
|
detail: str | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
self.message = message
|
self.message = message
|
||||||
@@ -46,6 +56,10 @@ class AppError(RuntimeError):
|
|||||||
self.suggestion = suggestion
|
self.suggestion = suggestion
|
||||||
self.retriable = retriable
|
self.retriable = retriable
|
||||||
self.error_id = error_id or new_error_id()
|
self.error_id = error_id or new_error_id()
|
||||||
|
# Internal-only diagnostic text. Persisted to evidence and logs, never rendered
|
||||||
|
# to users or serialized into API envelopes, because it may embed local
|
||||||
|
# filesystem paths and other infrastructure detail.
|
||||||
|
self.detail = detail
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
@@ -59,11 +73,28 @@ class ErrorEnvelope:
|
|||||||
timestamp: str
|
timestamp: str
|
||||||
|
|
||||||
|
|
||||||
|
def canonical_error_category(error: AppError) -> str:
|
||||||
|
"""Map internal categories to canonical API/UI envelope categories."""
|
||||||
|
mapping: dict[ErrorCategory, str] = {
|
||||||
|
ErrorCategory.VALIDATION: "validation",
|
||||||
|
ErrorCategory.USER_INPUT: "validation",
|
||||||
|
ErrorCategory.NOT_FOUND: "not_found",
|
||||||
|
ErrorCategory.CONFLICT: "conflict",
|
||||||
|
ErrorCategory.EXTERNAL_PROVIDER: "external",
|
||||||
|
ErrorCategory.EXTERNAL_TIMEOUT: "timeout",
|
||||||
|
ErrorCategory.INFRA_TRANSIENT: "timeout",
|
||||||
|
ErrorCategory.PROCESSING: "internal",
|
||||||
|
ErrorCategory.INFRA_PERSISTENT: "internal",
|
||||||
|
ErrorCategory.INTERNAL_UNEXPECTED: "internal",
|
||||||
|
}
|
||||||
|
return mapping.get(error.category, "internal")
|
||||||
|
|
||||||
|
|
||||||
def build_error_envelope(error: AppError) -> ErrorEnvelope:
|
def build_error_envelope(error: AppError) -> ErrorEnvelope:
|
||||||
"""Build an API-safe response envelope from an AppError."""
|
"""Build an API-safe response envelope from an AppError."""
|
||||||
return ErrorEnvelope(
|
return ErrorEnvelope(
|
||||||
error_id=error.error_id,
|
error_id=error.error_id,
|
||||||
category=error.category.value,
|
category=canonical_error_category(error),
|
||||||
message=error.message,
|
message=error.message,
|
||||||
suggestion=error.suggestion,
|
suggestion=error.suggestion,
|
||||||
timestamp=datetime.now(UTC).isoformat(),
|
timestamp=datetime.now(UTC).isoformat(),
|
||||||
@@ -71,15 +102,43 @@ def build_error_envelope(error: AppError) -> ErrorEnvelope:
|
|||||||
|
|
||||||
|
|
||||||
def classify_unexpected_error(exc: Exception, *, operation: str) -> AppError:
|
def classify_unexpected_error(exc: Exception, *, operation: str) -> AppError:
|
||||||
"""Normalize unknown exceptions into internal_unexpected_error."""
|
"""Normalize unknown exceptions into internal_unexpected_error.
|
||||||
return AppError(
|
|
||||||
f"Unexpected error during {operation}: {exc}",
|
The exception text is deliberately excluded from ``message``. ``AppError.message``
|
||||||
|
is rendered directly to users by the UI error presenter and is serialized into API
|
||||||
|
responses by :func:`build_error_envelope`, and unexpected exceptions routinely embed
|
||||||
|
local filesystem paths (SQLAlchemy ``OperationalError`` carries the database path,
|
||||||
|
``OSError`` carries the storage root). Leaking those is forbidden by
|
||||||
|
``.github/instructions/error-handling.instructions.md``.
|
||||||
|
|
||||||
|
The detail is preserved on ``AppError.detail`` and logged against ``error_id``. That
|
||||||
|
keeps the root cause in evidence records and operator logs, which are internal, while
|
||||||
|
keeping it out of user-facing and API-facing text.
|
||||||
|
"""
|
||||||
|
error = AppError(
|
||||||
|
f"Unexpected error during {operation}.",
|
||||||
category=ErrorCategory.INTERNAL_UNEXPECTED,
|
category=ErrorCategory.INTERNAL_UNEXPECTED,
|
||||||
suggestion="Retry once. If it persists, review logs and report the error reference id.",
|
suggestion="Retry once. If it persists, review logs and report the error reference id.",
|
||||||
retriable=False,
|
retriable=False,
|
||||||
|
detail=exception_detail(exc),
|
||||||
)
|
)
|
||||||
|
logger.error(
|
||||||
|
"Unexpected error operation=%s error_id=%s",
|
||||||
|
operation,
|
||||||
|
error.error_id,
|
||||||
|
exc_info=exc,
|
||||||
|
)
|
||||||
|
return error
|
||||||
|
|
||||||
|
|
||||||
def format_error_detail(error: AppError) -> str:
|
def format_error_detail(error: AppError) -> str:
|
||||||
"""Return a compact persisted failure string for transcript.error_detail."""
|
"""Return a compact persisted failure string for transcript.error_detail.
|
||||||
return f"[{error.category.value}] {error.message} | suggestion={error.suggestion} | error_id={error.error_id}"
|
|
||||||
|
This is internal provenance, not user-facing output, so it carries
|
||||||
|
``AppError.detail`` (the root cause) in addition to the user-safe message.
|
||||||
|
"""
|
||||||
|
parts = [f"[{error.category.value}] {error.message}"]
|
||||||
|
if error.detail:
|
||||||
|
parts.append(f"detail={error.detail}")
|
||||||
|
parts.extend((f"suggestion={error.suggestion}", f"error_id={error.error_id}"))
|
||||||
|
return " | ".join(parts)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ from transcription.config import Provider
|
|||||||
from transcription.config import Settings
|
from transcription.config import Settings
|
||||||
from transcription.config import get_settings
|
from transcription.config import get_settings
|
||||||
from transcription.providers.base import ProviderAuthError
|
from transcription.providers.base import ProviderAuthError
|
||||||
|
from transcription.providers.base import ProviderCallEvidence
|
||||||
from transcription.providers.base import ProviderError
|
from transcription.providers.base import ProviderError
|
||||||
from transcription.providers.base import ProviderResponseError
|
from transcription.providers.base import ProviderResponseError
|
||||||
from transcription.providers.base import TranscriptionMetadata
|
from transcription.providers.base import TranscriptionMetadata
|
||||||
@@ -27,6 +28,7 @@ def get_transcription_provider(*, settings: Settings | None = None) -> Transcrip
|
|||||||
__all__ = [
|
__all__ = [
|
||||||
"OpenRouterTranscriptionProvider",
|
"OpenRouterTranscriptionProvider",
|
||||||
"ProviderAuthError",
|
"ProviderAuthError",
|
||||||
|
"ProviderCallEvidence",
|
||||||
"ProviderError",
|
"ProviderError",
|
||||||
"ProviderResponseError",
|
"ProviderResponseError",
|
||||||
"RequestManifest",
|
"RequestManifest",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"""Provider interfaces and validated shared contracts for transcription adapters."""
|
"""Provider interfaces and validated shared contracts for transcription adapters."""
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
from typing import Protocol
|
from typing import Protocol
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@@ -37,6 +38,14 @@ class ProviderResponseError(ProviderError):
|
|||||||
"""Raised when provider responses are malformed or unusable."""
|
"""Raised when provider responses are malformed or unusable."""
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(slots=True)
|
||||||
|
class ProviderCallEvidence:
|
||||||
|
"""Caller-owned evidence sink for one provider invocation."""
|
||||||
|
|
||||||
|
request_manifest: RequestManifest | None = None
|
||||||
|
transport_evidence: TransportEvidence | None = None
|
||||||
|
|
||||||
|
|
||||||
class ProviderUsage(BaseModel):
|
class ProviderUsage(BaseModel):
|
||||||
"""Normalized provider token accounting."""
|
"""Normalized provider token accounting."""
|
||||||
|
|
||||||
@@ -102,6 +111,11 @@ class TranscriptionResult(BaseModel):
|
|||||||
class TranscriptionProvider(Protocol):
|
class TranscriptionProvider(Protocol):
|
||||||
"""Contract every transcription provider adapter must satisfy."""
|
"""Contract every transcription provider adapter must satisfy."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def model(self) -> str:
|
||||||
|
"""Return the resolved model slug this adapter will call."""
|
||||||
|
...
|
||||||
|
|
||||||
async def transcribe(
|
async def transcribe(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
@@ -111,6 +125,12 @@ class TranscriptionProvider(Protocol):
|
|||||||
temperature: float | None = None,
|
temperature: float | None = None,
|
||||||
top_p: float | None = None,
|
top_p: float | None = None,
|
||||||
source_reference: SourceEvidenceReference | None = None,
|
source_reference: SourceEvidenceReference | None = None,
|
||||||
|
requested_model: str | None = None,
|
||||||
|
evidence_capture: ProviderCallEvidence | None = None,
|
||||||
) -> TranscriptionResult:
|
) -> TranscriptionResult:
|
||||||
"""Transcribe the provided image according to the prompt text."""
|
"""Transcribe one source and write failure evidence into the provided capture sink."""
|
||||||
|
...
|
||||||
|
|
||||||
|
async def aclose(self) -> None:
|
||||||
|
"""Release any pooled network resources held by the adapter."""
|
||||||
...
|
...
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import os
|
|
||||||
import platform
|
import platform
|
||||||
from importlib.metadata import PackageNotFoundError
|
from importlib.metadata import PackageNotFoundError
|
||||||
from importlib.metadata import version
|
from importlib.metadata import version
|
||||||
@@ -17,6 +16,8 @@ from pydantic import ConfigDict
|
|||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
from pydantic import JsonValue
|
from pydantic import JsonValue
|
||||||
|
|
||||||
|
from transcription.config import Settings
|
||||||
|
|
||||||
REQUEST_MANIFEST_SCHEMA = "transcription.request-manifest"
|
REQUEST_MANIFEST_SCHEMA = "transcription.request-manifest"
|
||||||
REQUEST_MANIFEST_VERSION = "1"
|
REQUEST_MANIFEST_VERSION = "1"
|
||||||
SOFTWARE_CONTEXT_SCHEMA = "transcription.software-context"
|
SOFTWARE_CONTEXT_SCHEMA = "transcription.software-context"
|
||||||
@@ -141,11 +142,17 @@ def package_version(package: str) -> str:
|
|||||||
return "unknown"
|
return "unknown"
|
||||||
|
|
||||||
|
|
||||||
def build_software_context(*, adapter_name: str, adapter_version: str, client_library: str) -> SoftwareContext:
|
def build_software_context(
|
||||||
|
*,
|
||||||
|
adapter_name: str,
|
||||||
|
adapter_version: str,
|
||||||
|
client_library: str,
|
||||||
|
settings: Settings,
|
||||||
|
) -> SoftwareContext:
|
||||||
"""Build the runtime software identity for an execution."""
|
"""Build the runtime software identity for an execution."""
|
||||||
return SoftwareContext(
|
return SoftwareContext(
|
||||||
application_version=package_version("transcription"),
|
application_version=package_version("transcription"),
|
||||||
application_commit=os.environ.get("TRANSCRIPTION_COMMIT") or None,
|
application_commit=settings.transcription_commit,
|
||||||
adapter_name=adapter_name,
|
adapter_name=adapter_name,
|
||||||
adapter_version=adapter_version,
|
adapter_version=adapter_version,
|
||||||
client_library=client_library,
|
client_library=client_library,
|
||||||
|
|||||||
@@ -3,11 +3,13 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
|
import contextvars
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from collections.abc import AsyncIterator
|
from collections.abc import AsyncIterator
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
@@ -25,6 +27,7 @@ from pydantic import ValidationError
|
|||||||
from transcription.config import Settings
|
from transcription.config import Settings
|
||||||
from transcription.config import get_settings
|
from transcription.config import get_settings
|
||||||
from transcription.providers.base import ProviderAuthError
|
from transcription.providers.base import ProviderAuthError
|
||||||
|
from transcription.providers.base import ProviderCallEvidence
|
||||||
from transcription.providers.base import ProviderError
|
from transcription.providers.base import ProviderError
|
||||||
from transcription.providers.base import ProviderResponseError
|
from transcription.providers.base import ProviderResponseError
|
||||||
from transcription.providers.base import ProviderUsage
|
from transcription.providers.base import ProviderUsage
|
||||||
@@ -65,16 +68,24 @@ class _CapturingAsyncClient:
|
|||||||
|
|
||||||
def __init__(self, client: httpx.AsyncClient):
|
def __init__(self, client: httpx.AsyncClient):
|
||||||
self._client = client
|
self._client = client
|
||||||
self.last_response: httpx.Response | None = None
|
self._active_capture: contextvars.ContextVar[_TransportCapture | None] = contextvars.ContextVar(
|
||||||
self.last_body: bytes | None = None
|
"openrouter_transport_capture",
|
||||||
|
default=None,
|
||||||
|
)
|
||||||
|
|
||||||
async def send(self, request: httpx.Request, **kwargs: Any) -> httpx.Response:
|
async def send(self, request: httpx.Request, **kwargs: Any) -> httpx.Response:
|
||||||
|
capture = self._active_capture.get()
|
||||||
response = await self._client.send(request, **kwargs)
|
response = await self._client.send(request, **kwargs)
|
||||||
self.last_response = response
|
if capture is None:
|
||||||
|
return response
|
||||||
|
capture.response = response
|
||||||
try:
|
try:
|
||||||
self.last_body = response.content
|
capture.body = response.content
|
||||||
except httpx.ResponseNotRead:
|
except httpx.ResponseNotRead:
|
||||||
response.stream = _CapturingAsyncByteStream(response.stream, self._capture_body)
|
stream = response.stream
|
||||||
|
if not isinstance(stream, httpx.AsyncByteStream):
|
||||||
|
raise
|
||||||
|
response.stream = _CapturingAsyncByteStream(stream, lambda body: self._capture_body(capture, body))
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def build_request(self, *args: Any, **kwargs: Any) -> httpx.Request:
|
def build_request(self, *args: Any, **kwargs: Any) -> httpx.Request:
|
||||||
@@ -83,12 +94,21 @@ class _CapturingAsyncClient:
|
|||||||
async def aclose(self) -> None:
|
async def aclose(self) -> None:
|
||||||
await self._client.aclose()
|
await self._client.aclose()
|
||||||
|
|
||||||
def reset(self) -> None:
|
def begin_capture(self, capture: _TransportCapture) -> contextvars.Token[_TransportCapture | None]:
|
||||||
self.last_response = None
|
return self._active_capture.set(capture)
|
||||||
self.last_body = None
|
|
||||||
|
|
||||||
def _capture_body(self, body: bytes) -> None:
|
def end_capture(self, token: contextvars.Token[_TransportCapture | None]) -> None:
|
||||||
self.last_body = body
|
self._active_capture.reset(token)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _capture_body(capture: _TransportCapture, body: bytes) -> None:
|
||||||
|
capture.body = body
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(slots=True)
|
||||||
|
class _TransportCapture:
|
||||||
|
response: httpx.Response | None = None
|
||||||
|
body: bytes | None = None
|
||||||
|
|
||||||
|
|
||||||
class _ProviderModel(BaseModel):
|
class _ProviderModel(BaseModel):
|
||||||
@@ -192,10 +212,16 @@ class OpenRouterTranscriptionProvider:
|
|||||||
self._settings = settings or get_settings()
|
self._settings = settings or get_settings()
|
||||||
self._model = self._settings.provider_model or DEFAULT_OPENROUTER_MODEL
|
self._model = self._settings.provider_model or DEFAULT_OPENROUTER_MODEL
|
||||||
self._capturing_client: _CapturingAsyncClient | None = None
|
self._capturing_client: _CapturingAsyncClient | None = None
|
||||||
self._current_request_manifest: RequestManifest | None = None
|
|
||||||
self._current_transport_evidence: TransportEvidence | None = None
|
|
||||||
if client is None:
|
if client is None:
|
||||||
self._capturing_client = _CapturingAsyncClient(async_client or httpx.AsyncClient(follow_redirects=True))
|
# httpx defaults every phase to 5s, which silently caps provider calls far
|
||||||
|
# below worker_provider_timeout_seconds. Track the configured budget instead.
|
||||||
|
timeout = httpx.Timeout(
|
||||||
|
self._settings.worker_provider_timeout_seconds,
|
||||||
|
connect=10.0,
|
||||||
|
)
|
||||||
|
self._capturing_client = _CapturingAsyncClient(
|
||||||
|
async_client or httpx.AsyncClient(follow_redirects=True, timeout=timeout)
|
||||||
|
)
|
||||||
client = OpenRouter(
|
client = OpenRouter(
|
||||||
api_key=self._settings.openrouter_api_key.get_secret_value(),
|
api_key=self._settings.openrouter_api_key.get_secret_value(),
|
||||||
async_client=self._capturing_client,
|
async_client=self._capturing_client,
|
||||||
@@ -207,18 +233,6 @@ class OpenRouterTranscriptionProvider:
|
|||||||
"""Return the resolved OpenRouter model slug."""
|
"""Return the resolved OpenRouter model slug."""
|
||||||
return self._model
|
return self._model
|
||||||
|
|
||||||
@property
|
|
||||||
def current_request_manifest(self) -> RequestManifest | None:
|
|
||||||
return self._current_request_manifest
|
|
||||||
|
|
||||||
@property
|
|
||||||
def current_transport_evidence(self) -> TransportEvidence | None:
|
|
||||||
if self._current_transport_evidence is not None:
|
|
||||||
return self._current_transport_evidence
|
|
||||||
if self._current_request_manifest is None:
|
|
||||||
return None
|
|
||||||
return self._captured_transport_evidence()
|
|
||||||
|
|
||||||
async def aclose(self) -> None:
|
async def aclose(self) -> None:
|
||||||
if self._capturing_client is not None:
|
if self._capturing_client is not None:
|
||||||
await self._capturing_client.aclose()
|
await self._capturing_client.aclose()
|
||||||
@@ -232,6 +246,8 @@ class OpenRouterTranscriptionProvider:
|
|||||||
temperature: float | None = None,
|
temperature: float | None = None,
|
||||||
top_p: float | None = None,
|
top_p: float | None = None,
|
||||||
source_reference: SourceEvidenceReference | None = None,
|
source_reference: SourceEvidenceReference | None = None,
|
||||||
|
requested_model: str | None = None,
|
||||||
|
evidence_capture: ProviderCallEvidence | None = None,
|
||||||
) -> TranscriptionResult:
|
) -> TranscriptionResult:
|
||||||
"""Send prompt + image to OpenRouter and return normalized text output."""
|
"""Send prompt + image to OpenRouter and return normalized text output."""
|
||||||
request = self._build_request(
|
request = self._build_request(
|
||||||
@@ -240,6 +256,7 @@ class OpenRouterTranscriptionProvider:
|
|||||||
mime_type=mime_type,
|
mime_type=mime_type,
|
||||||
temperature=temperature,
|
temperature=temperature,
|
||||||
top_p=top_p,
|
top_p=top_p,
|
||||||
|
requested_model=requested_model,
|
||||||
)
|
)
|
||||||
manifest = self._build_request_manifest(
|
manifest = self._build_request_manifest(
|
||||||
request=request,
|
request=request,
|
||||||
@@ -248,18 +265,21 @@ class OpenRouterTranscriptionProvider:
|
|||||||
temperature=temperature,
|
temperature=temperature,
|
||||||
top_p=top_p,
|
top_p=top_p,
|
||||||
)
|
)
|
||||||
self._current_request_manifest = manifest
|
if evidence_capture is not None:
|
||||||
self._current_transport_evidence = None
|
evidence_capture.request_manifest = manifest
|
||||||
if self._capturing_client is not None:
|
evidence_capture.transport_evidence = None
|
||||||
self._capturing_client.reset()
|
transport_capture = _TransportCapture()
|
||||||
|
token = self._capturing_client.begin_capture(transport_capture) if self._capturing_client is not None else None
|
||||||
|
try:
|
||||||
try:
|
try:
|
||||||
response = await self._client.chat.send_async(
|
response = await self._client.chat.send_async(
|
||||||
**request.model_dump(mode="json", exclude_none=True),
|
**request.model_dump(mode="json", exclude_none=True),
|
||||||
retries=None,
|
retries=None,
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
transport = self._captured_transport_evidence()
|
transport = self._captured_transport_evidence(transport_capture)
|
||||||
self._current_transport_evidence = transport
|
if evidence_capture is not None:
|
||||||
|
evidence_capture.transport_evidence = transport
|
||||||
if isinstance(exc, openrouter_errors.UnauthorizedResponseError):
|
if isinstance(exc, openrouter_errors.UnauthorizedResponseError):
|
||||||
raise ProviderAuthError(
|
raise ProviderAuthError(
|
||||||
"OpenRouter authentication failed",
|
"OpenRouter authentication failed",
|
||||||
@@ -281,8 +301,9 @@ class OpenRouterTranscriptionProvider:
|
|||||||
failure_phase=failure_phase,
|
failure_phase=failure_phase,
|
||||||
) from exc
|
) from exc
|
||||||
|
|
||||||
transport = self._captured_transport_evidence()
|
transport = self._captured_transport_evidence(transport_capture)
|
||||||
self._current_transport_evidence = transport
|
if evidence_capture is not None:
|
||||||
|
evidence_capture.transport_evidence = transport
|
||||||
raw_api_response = self._coerce_raw_response(response)
|
raw_api_response = self._coerce_raw_response(response)
|
||||||
try:
|
try:
|
||||||
validated_response = OpenRouterResponse.model_validate(raw_api_response)
|
validated_response = OpenRouterResponse.model_validate(raw_api_response)
|
||||||
@@ -303,7 +324,7 @@ class OpenRouterTranscriptionProvider:
|
|||||||
transport_evidence=transport,
|
transport_evidence=transport,
|
||||||
failure_phase="response_validation",
|
failure_phase="response_validation",
|
||||||
) from exc
|
) from exc
|
||||||
model = validated_response.model or self.model
|
model = validated_response.model or requested_model or self.model
|
||||||
metadata = self._build_metadata(validated_response)
|
metadata = self._build_metadata(validated_response)
|
||||||
logger.info("OpenRouter transcription completed using model=%s", model)
|
logger.info("OpenRouter transcription completed using model=%s", model)
|
||||||
return TranscriptionResult(
|
return TranscriptionResult(
|
||||||
@@ -321,6 +342,10 @@ class OpenRouterTranscriptionProvider:
|
|||||||
request_manifest=manifest,
|
request_manifest=manifest,
|
||||||
transport_evidence=transport,
|
transport_evidence=transport,
|
||||||
)
|
)
|
||||||
|
finally:
|
||||||
|
client = self._capturing_client
|
||||||
|
if token is not None and client is not None:
|
||||||
|
client.end_capture(token)
|
||||||
|
|
||||||
def _build_request_manifest(
|
def _build_request_manifest(
|
||||||
self,
|
self,
|
||||||
@@ -332,6 +357,7 @@ class OpenRouterTranscriptionProvider:
|
|||||||
top_p: float | None,
|
top_p: float | None,
|
||||||
) -> RequestManifest | None:
|
) -> RequestManifest | None:
|
||||||
if source_reference is None:
|
if source_reference is None:
|
||||||
|
logger.warning("OpenRouter request manifest omitted because source evidence reference is missing.")
|
||||||
return None
|
return None
|
||||||
request_payload = request.model_dump(mode="json", exclude_none=True)
|
request_payload = request.model_dump(mode="json", exclude_none=True)
|
||||||
sanitized_request = self._replace_embedded_media(request_payload, source_reference=source_reference)
|
sanitized_request = self._replace_embedded_media(request_payload, source_reference=source_reference)
|
||||||
@@ -339,7 +365,7 @@ class OpenRouterTranscriptionProvider:
|
|||||||
omitted = tuple(name for name in ("temperature", "top_p") if name not in explicit)
|
omitted = tuple(name for name in ("temperature", "top_p") if name not in explicit)
|
||||||
return RequestManifest(
|
return RequestManifest(
|
||||||
provider="openrouter",
|
provider="openrouter",
|
||||||
requested_model=self.model,
|
requested_model=request.model,
|
||||||
request=JSON_OBJECT_ADAPTER.validate_python(sanitized_request),
|
request=JSON_OBJECT_ADAPTER.validate_python(sanitized_request),
|
||||||
source=source_reference,
|
source=source_reference,
|
||||||
explicitly_supplied_parameters=explicit,
|
explicitly_supplied_parameters=explicit,
|
||||||
@@ -356,6 +382,7 @@ class OpenRouterTranscriptionProvider:
|
|||||||
adapter_name="openrouter",
|
adapter_name="openrouter",
|
||||||
adapter_version=OPENROUTER_ADAPTER_VERSION,
|
adapter_version=OPENROUTER_ADAPTER_VERSION,
|
||||||
client_library="openrouter",
|
client_library="openrouter",
|
||||||
|
settings=self._settings,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -379,16 +406,15 @@ class OpenRouterTranscriptionProvider:
|
|||||||
return [self._replace_embedded_media(item, source_reference=source_reference) for item in value]
|
return [self._replace_embedded_media(item, source_reference=source_reference) for item in value]
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def _captured_transport_evidence(self) -> TransportEvidence:
|
def _captured_transport_evidence(self, capture: _TransportCapture) -> TransportEvidence:
|
||||||
response = self._capturing_client.last_response if self._capturing_client is not None else None
|
response = capture.response
|
||||||
if response is None:
|
if response is None:
|
||||||
return TransportEvidence(response_received=False)
|
return TransportEvidence(response_received=False)
|
||||||
headers = filter_safe_response_headers(response.headers)
|
headers = filter_safe_response_headers(response.headers)
|
||||||
body = self._capturing_client.last_body if self._capturing_client is not None else None
|
|
||||||
return TransportEvidence(
|
return TransportEvidence(
|
||||||
response_received=True,
|
response_received=True,
|
||||||
status_code=response.status_code,
|
status_code=response.status_code,
|
||||||
body=body,
|
body=capture.body,
|
||||||
safe_headers=headers,
|
safe_headers=headers,
|
||||||
content_type=headers.get("content-type"),
|
content_type=headers.get("content-type"),
|
||||||
content_encoding=headers.get("content-encoding"),
|
content_encoding=headers.get("content-encoding"),
|
||||||
@@ -481,6 +507,7 @@ class OpenRouterTranscriptionProvider:
|
|||||||
mime_type: str,
|
mime_type: str,
|
||||||
temperature: float | None,
|
temperature: float | None,
|
||||||
top_p: float | None,
|
top_p: float | None,
|
||||||
|
requested_model: str | None = None,
|
||||||
) -> OpenRouterRequest:
|
) -> OpenRouterRequest:
|
||||||
image_b64 = base64.b64encode(image_bytes).decode("ascii")
|
image_b64 = base64.b64encode(image_bytes).decode("ascii")
|
||||||
data_url = f"data:{mime_type};base64,{image_b64}"
|
data_url = f"data:{mime_type};base64,{image_b64}"
|
||||||
@@ -491,7 +518,7 @@ class OpenRouterTranscriptionProvider:
|
|||||||
media_content = ImageContent(image_url=ImageUrl(url=data_url))
|
media_content = ImageContent(image_url=ImageUrl(url=data_url))
|
||||||
|
|
||||||
return OpenRouterRequest(
|
return OpenRouterRequest(
|
||||||
model=self.model,
|
model=requested_model or self.model,
|
||||||
messages=(UserMessage(content=(TextContent(text=prompt_text), media_content)),),
|
messages=(UserMessage(content=(TextContent(text=prompt_text), media_content)),),
|
||||||
http_referer=self._settings.openrouter_http_referer,
|
http_referer=self._settings.openrouter_http_referer,
|
||||||
x_open_router_title=self._settings.openrouter_app_title,
|
x_open_router_title=self._settings.openrouter_app_title,
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from collections.abc import Awaitable
|
||||||
|
from collections.abc import Callable
|
||||||
|
from typing import TypeVar
|
||||||
|
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
|
ResultT = TypeVar("ResultT")
|
||||||
|
|
||||||
|
|
||||||
|
async def run_blocking(func: Callable[..., ResultT], /, *args, **kwargs) -> ResultT:
|
||||||
|
"""Run blocking CPU/filesystem work on a worker thread."""
|
||||||
|
return await asyncio.to_thread(func, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
async def insert_with_sequence_retry(
|
||||||
|
*,
|
||||||
|
max_retries: int,
|
||||||
|
operation: Callable[[int], Awaitable[ResultT]],
|
||||||
|
on_conflict: Callable[[int, IntegrityError], None] | None = None,
|
||||||
|
) -> ResultT:
|
||||||
|
"""Retry a sequence-based insert operation on unique-key conflicts."""
|
||||||
|
if max_retries < 1:
|
||||||
|
raise ValueError("max_retries must be at least 1")
|
||||||
|
|
||||||
|
for retry in range(1, max_retries + 1):
|
||||||
|
try:
|
||||||
|
return await operation(retry)
|
||||||
|
except IntegrityError as exc:
|
||||||
|
if on_conflict is not None:
|
||||||
|
on_conflict(retry, exc)
|
||||||
|
if retry == max_retries:
|
||||||
|
raise
|
||||||
|
|
||||||
|
raise RuntimeError("insert_with_sequence_retry exhausted retries without returning or raising")
|
||||||
@@ -2,14 +2,32 @@
|
|||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from dataclasses import field
|
from dataclasses import field
|
||||||
|
from typing import Self
|
||||||
|
|
||||||
|
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||||||
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||||
|
|
||||||
|
from ..config import Settings
|
||||||
from .documents import DocumentService
|
from .documents import DocumentService
|
||||||
|
from .evidence import EvidenceService
|
||||||
from .jobs import JobService
|
from .jobs import JobService
|
||||||
|
from .maintenance import MaintenanceService
|
||||||
from .people import PeopleService
|
from .people import PeopleService
|
||||||
|
from .photos import PhotosService
|
||||||
from .prompts import PromptStore
|
from .prompts import PromptStore
|
||||||
from .sources import SourceService
|
from .sources import SourceService
|
||||||
|
|
||||||
__all__ = ["DocumentService", "JobService", "PeopleService", "PromptStore", "ServiceBundle", "SourceService"]
|
__all__ = [
|
||||||
|
"DocumentService",
|
||||||
|
"EvidenceService",
|
||||||
|
"JobService",
|
||||||
|
"MaintenanceService",
|
||||||
|
"PeopleService",
|
||||||
|
"PhotosService",
|
||||||
|
"PromptStore",
|
||||||
|
"ServiceBundle",
|
||||||
|
"SourceService",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, slots=True)
|
@dataclass(frozen=True, slots=True)
|
||||||
@@ -19,4 +37,31 @@ class ServiceBundle:
|
|||||||
documents: DocumentService = field(default_factory=DocumentService)
|
documents: DocumentService = field(default_factory=DocumentService)
|
||||||
sources: SourceService = field(default_factory=SourceService)
|
sources: SourceService = field(default_factory=SourceService)
|
||||||
jobs: JobService = field(default_factory=JobService)
|
jobs: JobService = field(default_factory=JobService)
|
||||||
|
maintenance: MaintenanceService = field(default_factory=MaintenanceService)
|
||||||
people: PeopleService = field(default_factory=PeopleService)
|
people: PeopleService = field(default_factory=PeopleService)
|
||||||
|
photos: PhotosService = field(default_factory=PhotosService)
|
||||||
|
evidence: EvidenceService = field(default_factory=EvidenceService)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_session_factory(
|
||||||
|
cls,
|
||||||
|
session_factory: async_sessionmaker[AsyncSession] | None = None,
|
||||||
|
*,
|
||||||
|
settings: Settings | None = None,
|
||||||
|
) -> Self:
|
||||||
|
"""Build a bundle whose services all share one session factory and settings."""
|
||||||
|
if session_factory is None:
|
||||||
|
return cls()
|
||||||
|
return cls(
|
||||||
|
documents=DocumentService(session_factory=session_factory, settings=settings),
|
||||||
|
sources=SourceService(session_factory=session_factory, settings=settings),
|
||||||
|
jobs=JobService(session_factory=session_factory, settings=settings),
|
||||||
|
maintenance=MaintenanceService(session_factory=session_factory, settings=settings),
|
||||||
|
people=PeopleService(session_factory=session_factory, settings=settings),
|
||||||
|
photos=PhotosService(session_factory=session_factory, settings=settings),
|
||||||
|
evidence=EvidenceService(session_factory=session_factory, settings=settings),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def aclose(self) -> None:
|
||||||
|
"""Release provider resources held by the bundle."""
|
||||||
|
await self.sources.aclose()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import asyncio
|
|
||||||
from abc import ABC
|
from abc import ABC
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from sqlalchemy.ext.asyncio import async_sessionmaker
|
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||||
@@ -10,6 +10,8 @@ from ..config import Settings
|
|||||||
from ..config import get_settings
|
from ..config import get_settings
|
||||||
from ..db.session import resolve_session_factory
|
from ..db.session import resolve_session_factory
|
||||||
from ..db.session import session_scope
|
from ..db.session import session_scope
|
||||||
|
from ..errors import AppError
|
||||||
|
from ..errors import ErrorCategory
|
||||||
|
|
||||||
|
|
||||||
class ServiceBase(ABC):
|
class ServiceBase(ABC):
|
||||||
@@ -17,17 +19,14 @@ class ServiceBase(ABC):
|
|||||||
|
|
||||||
settings: Settings
|
settings: Settings
|
||||||
session_factory: async_sessionmaker[AsyncSession]
|
session_factory: async_sessionmaker[AsyncSession]
|
||||||
queue: asyncio.Queue
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
session_factory: async_sessionmaker[AsyncSession] | None = None,
|
session_factory: async_sessionmaker[AsyncSession] | None = None,
|
||||||
queue: asyncio.Queue | None = None,
|
|
||||||
settings: Settings | None = None,
|
settings: Settings | None = None,
|
||||||
):
|
):
|
||||||
self.settings = settings or get_settings()
|
self.settings = settings or get_settings()
|
||||||
self.session_factory = session_factory or resolve_session_factory(settings=self.settings)
|
self.session_factory = session_factory or resolve_session_factory(settings=self.settings)
|
||||||
self.queue = queue or asyncio.Queue()
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def _session_scope(self, session: AsyncSession | None = None):
|
async def _session_scope(self, session: AsyncSession | None = None):
|
||||||
@@ -58,3 +57,28 @@ class ServiceBase(ABC):
|
|||||||
|
|
||||||
for obj in refresh:
|
for obj in refresh:
|
||||||
await session.refresh(obj)
|
await session.refresh(obj)
|
||||||
|
|
||||||
|
async def _get_or_raise[ModelT](
|
||||||
|
self,
|
||||||
|
model: type[ModelT],
|
||||||
|
entity_id: object,
|
||||||
|
*,
|
||||||
|
session: AsyncSession,
|
||||||
|
error: type[AppError],
|
||||||
|
noun: str,
|
||||||
|
suggestion: str,
|
||||||
|
options: Sequence[Any] = (),
|
||||||
|
) -> ModelT:
|
||||||
|
"""Load an entity by primary key or raise a not-found service error.
|
||||||
|
|
||||||
|
``noun`` and ``suggestion`` are supplied by the caller so each domain
|
||||||
|
keeps its own user-facing wording.
|
||||||
|
"""
|
||||||
|
entity = await session.get(model, entity_id, options=list(options) or None)
|
||||||
|
if entity is None:
|
||||||
|
raise error(
|
||||||
|
f"{noun} with id {entity_id} not found",
|
||||||
|
category=ErrorCategory.NOT_FOUND,
|
||||||
|
suggestion=suggestion,
|
||||||
|
)
|
||||||
|
return entity
|
||||||
|
|||||||
@@ -2,23 +2,35 @@ import logging
|
|||||||
import shutil
|
import shutil
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import UTC
|
from datetime import date
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from sqlalchemy import func
|
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
from sqlalchemy.orm import selectinload
|
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||||||
|
from sqlmodel import SQLModel
|
||||||
from sqlmodel import col
|
from sqlmodel import col
|
||||||
from sqlmodel import select
|
from sqlmodel import select
|
||||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||||
|
|
||||||
|
from ..config import Settings
|
||||||
|
from ..db.loading import orm_attribute
|
||||||
|
from ..db.loading import selectinload
|
||||||
from ..db.models import Document
|
from ..db.models import Document
|
||||||
from ..db.models import DocumentPerson
|
from ..db.models import DocumentPerson
|
||||||
|
from ..db.models import DocumentTag
|
||||||
from ..db.models import DocumentType
|
from ..db.models import DocumentType
|
||||||
|
from ..db.models import Tag
|
||||||
|
from ..db.registries import AUTHOR_ROLE_SEMANTIC_KEY
|
||||||
from ..errors import AppError
|
from ..errors import AppError
|
||||||
from ..errors import ErrorCategory
|
from ..errors import ErrorCategory
|
||||||
from .base import ServiceBase
|
from .base import ServiceBase
|
||||||
|
from .registry import RegistryService
|
||||||
|
from .registry import RegistrySummary
|
||||||
|
from .source_media import lookup_source_mime_type
|
||||||
|
from .source_media import supported_source_formats
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -43,34 +55,100 @@ class DocumentTypeError(DocumentError):
|
|||||||
"""Raised when Document Type maintenance fails."""
|
"""Raised when Document Type maintenance fails."""
|
||||||
|
|
||||||
|
|
||||||
def _normalize_registry_label(label: str) -> str:
|
class TagError(DocumentError):
|
||||||
normalized = label.strip()
|
"""Raised when Tag maintenance fails."""
|
||||||
if not normalized:
|
|
||||||
raise DocumentTypeError(
|
|
||||||
"Document Type label is required",
|
|
||||||
category=ErrorCategory.VALIDATION,
|
|
||||||
suggestion="Enter a user-facing label and retry.",
|
|
||||||
)
|
|
||||||
return normalized
|
|
||||||
|
|
||||||
|
|
||||||
def _document_type_label_key(label: str) -> str:
|
class DocumentTypeRegistry(RegistryService[DocumentType]):
|
||||||
return _normalize_registry_label(label).casefold()
|
"""Document Type registry maintenance."""
|
||||||
|
|
||||||
|
model = DocumentType
|
||||||
|
error = DocumentTypeError
|
||||||
|
noun = "Document Type"
|
||||||
|
short_noun = "type"
|
||||||
|
referenced_retainer = "historical Documents"
|
||||||
|
|
||||||
|
def reference_model(self) -> type[SQLModel]:
|
||||||
|
return Document
|
||||||
|
|
||||||
|
def reference_id_column(self) -> Any:
|
||||||
|
return col(Document.id)
|
||||||
|
|
||||||
|
def reference_key_column(self) -> Any:
|
||||||
|
return col(Document.document_type_id)
|
||||||
|
|
||||||
|
|
||||||
|
type DocumentTypeSummary = RegistrySummary
|
||||||
|
|
||||||
|
|
||||||
|
class TagRegistry(RegistryService[Tag]):
|
||||||
|
"""Tag registry maintenance."""
|
||||||
|
|
||||||
|
model = Tag
|
||||||
|
error = TagError
|
||||||
|
noun = "Tag"
|
||||||
|
short_noun = "tag"
|
||||||
|
referenced_retainer = "historical Documents"
|
||||||
|
|
||||||
|
def reference_model(self) -> type[SQLModel]:
|
||||||
|
return DocumentTag
|
||||||
|
|
||||||
|
def reference_id_column(self) -> Any:
|
||||||
|
return col(DocumentTag.id)
|
||||||
|
|
||||||
|
def reference_key_column(self) -> Any:
|
||||||
|
return col(DocumentTag.tag_id)
|
||||||
|
|
||||||
|
|
||||||
|
type TagSummary = RegistrySummary
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, slots=True)
|
@dataclass(frozen=True, slots=True)
|
||||||
class DocumentTypeSummary:
|
class DocumentPrintSource:
|
||||||
"""Settings read model for a Document Type and its usage count."""
|
|
||||||
|
|
||||||
id: UUID
|
id: UUID
|
||||||
label: str
|
page_number: int
|
||||||
is_active: bool
|
media_type: str
|
||||||
document_count: int
|
current_text: str | None
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class DocumentPrintJob:
|
||||||
|
id: UUID
|
||||||
|
date_created: datetime
|
||||||
|
provider: str | None
|
||||||
|
model: str | None
|
||||||
|
prompt_name: str | None
|
||||||
|
retry_count: int
|
||||||
|
status: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class DocumentPrintProjection:
|
||||||
|
id: UUID
|
||||||
|
title: str
|
||||||
|
document_type: str | None
|
||||||
|
authors: tuple[str, ...]
|
||||||
|
document_date: date | None
|
||||||
|
document_date_raw: str | None
|
||||||
|
location_created: str | None
|
||||||
|
archive_identifier: str | None
|
||||||
|
notes: str | None
|
||||||
|
sources: tuple[DocumentPrintSource, ...]
|
||||||
|
jobs: tuple[DocumentPrintJob, ...]
|
||||||
|
|
||||||
|
|
||||||
class DocumentService(ServiceBase):
|
class DocumentService(ServiceBase):
|
||||||
"""Thin service class for managing documents in the database."""
|
"""Thin service class for managing documents in the database."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
session_factory: async_sessionmaker[AsyncSession] | None = None,
|
||||||
|
settings: Settings | None = None,
|
||||||
|
) -> None:
|
||||||
|
super().__init__(session_factory, settings)
|
||||||
|
self._document_types = DocumentTypeRegistry(self.session_factory, self.settings)
|
||||||
|
self._tags = TagRegistry(self.session_factory, self.settings)
|
||||||
|
|
||||||
async def _validate_document_type(self, *, session: AsyncSession, document: Document) -> None:
|
async def _validate_document_type(self, *, session: AsyncSession, document: Document) -> None:
|
||||||
"""Validate the UUID-backed Document Type reference."""
|
"""Validate the UUID-backed Document Type reference."""
|
||||||
if document.document_type_id is None:
|
if document.document_type_id is None:
|
||||||
@@ -82,16 +160,23 @@ class DocumentService(ServiceBase):
|
|||||||
suggestion="Select a valid document type and retry.",
|
suggestion="Select a valid document type and retry.",
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _get_document_or_raise(self, *, session: AsyncSession, document_id: UUID) -> Document:
|
async def _read_document(
|
||||||
"""Get a document by id or raise a not-found service error."""
|
self,
|
||||||
document = await session.get(Document, document_id)
|
*,
|
||||||
if document is None:
|
session: AsyncSession,
|
||||||
raise DocumentError(
|
document_id: UUID,
|
||||||
f"Document with id {document_id} not found",
|
options: Sequence[Any] = (),
|
||||||
category=ErrorCategory.NOT_FOUND,
|
suggestion: str = "Verify the document id and retry.",
|
||||||
suggestion="Verify the document id and retry.",
|
) -> Document:
|
||||||
|
return await self._get_or_raise(
|
||||||
|
Document,
|
||||||
|
document_id,
|
||||||
|
session=session,
|
||||||
|
error=DocumentError,
|
||||||
|
noun="Document",
|
||||||
|
suggestion=suggestion,
|
||||||
|
options=options,
|
||||||
)
|
)
|
||||||
return document
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# CRUD Operations
|
# CRUD Operations
|
||||||
@@ -123,21 +208,16 @@ class DocumentService(ServiceBase):
|
|||||||
The selectinload option is used to eagerly load related jobs and sources.
|
The selectinload option is used to eagerly load related jobs and sources.
|
||||||
"""
|
"""
|
||||||
async with self._session_scope(session) as _session:
|
async with self._session_scope(session) as _session:
|
||||||
document = await _session.get(
|
document = await self._read_document(
|
||||||
Document,
|
session=_session,
|
||||||
document_id,
|
document_id=document_id,
|
||||||
options=(
|
options=(
|
||||||
selectinload(Document.jobs), # pyright: ignore[reportArgumentType]
|
selectinload(Document.jobs),
|
||||||
selectinload(Document.sources), # pyright: ignore[reportArgumentType]
|
selectinload(Document.sources),
|
||||||
),
|
),
|
||||||
)
|
|
||||||
if document is None:
|
|
||||||
raise DocumentError(
|
|
||||||
f"Document with id {document_id} not found",
|
|
||||||
category=ErrorCategory.NOT_FOUND,
|
|
||||||
suggestion="Re-upload the source document and retry.",
|
suggestion="Re-upload the source document and retry.",
|
||||||
)
|
)
|
||||||
elif not document.sources:
|
if not document.sources:
|
||||||
raise MissingSourceError(
|
raise MissingSourceError(
|
||||||
f"Document with id {document_id} has no associated source records",
|
f"Document with id {document_id} has no associated source records",
|
||||||
category=ErrorCategory.NOT_FOUND,
|
category=ErrorCategory.NOT_FOUND,
|
||||||
@@ -149,7 +229,6 @@ class DocumentService(ServiceBase):
|
|||||||
"""Update an existing document in the database."""
|
"""Update an existing document in the database."""
|
||||||
async with self._session_scope(session) as _session:
|
async with self._session_scope(session) as _session:
|
||||||
await self._validate_document_type(session=_session, document=document)
|
await self._validate_document_type(session=_session, document=document)
|
||||||
document.updated_at = datetime.now(UTC)
|
|
||||||
merged = await _session.merge(document)
|
merged = await _session.merge(document)
|
||||||
await self._finalize(session=_session, caller_session=session, refresh=(merged,))
|
await self._finalize(session=_session, caller_session=session, refresh=(merged,))
|
||||||
return merged
|
return merged
|
||||||
@@ -158,21 +237,15 @@ class DocumentService(ServiceBase):
|
|||||||
"""Delete a document from the database."""
|
"""Delete a document from the database."""
|
||||||
document_id = document.id
|
document_id = document.id
|
||||||
async with self._session_scope(session) as _session:
|
async with self._session_scope(session) as _session:
|
||||||
existing = await _session.get(
|
existing = await self._read_document(
|
||||||
Document,
|
session=_session,
|
||||||
document.id,
|
document_id=document.id,
|
||||||
options=(
|
options=(
|
||||||
selectinload(Document.jobs), # pyright: ignore[reportArgumentType]
|
selectinload(Document.jobs),
|
||||||
selectinload(Document.sources), # pyright: ignore[reportArgumentType]
|
selectinload(Document.sources),
|
||||||
selectinload(Document.document_people), # pyright: ignore[reportArgumentType]
|
selectinload(Document.document_people),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
if existing is None:
|
|
||||||
raise DocumentError(
|
|
||||||
f"Document with id {document.id} not found",
|
|
||||||
category=ErrorCategory.NOT_FOUND,
|
|
||||||
suggestion="Verify the document id and retry.",
|
|
||||||
)
|
|
||||||
|
|
||||||
has_jobs = bool(existing.jobs)
|
has_jobs = bool(existing.jobs)
|
||||||
has_sources = bool(existing.sources)
|
has_sources = bool(existing.sources)
|
||||||
@@ -225,9 +298,12 @@ class DocumentService(ServiceBase):
|
|||||||
"""List documents with relations needed by the archival table."""
|
"""List documents with relations needed by the archival table."""
|
||||||
async with self._session_scope(session) as _session:
|
async with self._session_scope(session) as _session:
|
||||||
query = select(Document).options(
|
query = select(Document).options(
|
||||||
selectinload(Document.document_people).selectinload(DocumentPerson.person), # pyright: ignore[reportArgumentType]
|
selectinload(Document.document_people).selectinload(orm_attribute(DocumentPerson.person)),
|
||||||
selectinload(Document.document_people).selectinload(DocumentPerson.role_ref), # pyright: ignore[reportArgumentType]
|
selectinload(Document.document_people).selectinload(orm_attribute(DocumentPerson.role_ref)),
|
||||||
selectinload(Document.document_type_ref), # pyright: ignore[reportArgumentType]
|
selectinload(Document.document_type_ref),
|
||||||
|
selectinload(Document.document_tags).selectinload(orm_attribute(DocumentTag.tag_ref)),
|
||||||
|
selectinload(Document.sources),
|
||||||
|
selectinload(Document.jobs),
|
||||||
)
|
)
|
||||||
result = await _session.exec(query)
|
result = await _session.exec(query)
|
||||||
return result.all()
|
return result.all()
|
||||||
@@ -238,11 +314,12 @@ class DocumentService(ServiceBase):
|
|||||||
query = (
|
query = (
|
||||||
select(Document)
|
select(Document)
|
||||||
.options(
|
.options(
|
||||||
selectinload(Document.jobs), # pyright: ignore[reportArgumentType]
|
selectinload(Document.jobs),
|
||||||
selectinload(Document.sources), # pyright: ignore[reportArgumentType]
|
selectinload(Document.sources),
|
||||||
selectinload(Document.document_people).selectinload(DocumentPerson.person), # pyright: ignore[reportArgumentType]
|
selectinload(Document.document_people).selectinload(orm_attribute(DocumentPerson.person)),
|
||||||
selectinload(Document.document_people).selectinload(DocumentPerson.role_ref), # pyright: ignore[reportArgumentType]
|
selectinload(Document.document_people).selectinload(orm_attribute(DocumentPerson.role_ref)),
|
||||||
selectinload(Document.document_type_ref), # pyright: ignore[reportArgumentType]
|
selectinload(Document.document_type_ref),
|
||||||
|
selectinload(Document.document_tags).selectinload(orm_attribute(DocumentTag.tag_ref)),
|
||||||
)
|
)
|
||||||
.where(Document.id == document_id)
|
.where(Document.id == document_id)
|
||||||
.execution_options(populate_existing=True)
|
.execution_options(populate_existing=True)
|
||||||
@@ -256,6 +333,59 @@ class DocumentService(ServiceBase):
|
|||||||
)
|
)
|
||||||
return document
|
return document
|
||||||
|
|
||||||
|
async def read_document_print_projection(
|
||||||
|
self,
|
||||||
|
document_id: UUID,
|
||||||
|
*,
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> DocumentPrintProjection:
|
||||||
|
"""Build the safe, deterministic read model used by print previews."""
|
||||||
|
document = await self.read_document_detail(document_id, session=session)
|
||||||
|
authors = sorted(
|
||||||
|
(
|
||||||
|
link.person.full_name
|
||||||
|
for link in document.document_people
|
||||||
|
if link.person is not None
|
||||||
|
and link.role_ref is not None
|
||||||
|
and link.role_ref.semantic_key == AUTHOR_ROLE_SEMANTIC_KEY
|
||||||
|
),
|
||||||
|
key=str.casefold,
|
||||||
|
)
|
||||||
|
sources = tuple(
|
||||||
|
DocumentPrintSource(
|
||||||
|
id=source.id,
|
||||||
|
page_number=source.page_number,
|
||||||
|
media_type=_print_media_type(source.filename),
|
||||||
|
current_text=_current_print_text(source.revised_text, source.raw_transcription),
|
||||||
|
)
|
||||||
|
for source in sorted(document.sources, key=lambda item: (item.page_number, item.id))
|
||||||
|
)
|
||||||
|
jobs = tuple(
|
||||||
|
DocumentPrintJob(
|
||||||
|
id=job.id,
|
||||||
|
date_created=job.date_created,
|
||||||
|
provider=job.provider,
|
||||||
|
model=job.model,
|
||||||
|
prompt_name=job.prompt_name,
|
||||||
|
retry_count=job.retry_count,
|
||||||
|
status=getattr(job.status, "value", str(job.status)),
|
||||||
|
)
|
||||||
|
for job in sorted(document.jobs, key=lambda item: (item.date_created, item.id))
|
||||||
|
)
|
||||||
|
return DocumentPrintProjection(
|
||||||
|
id=document.id,
|
||||||
|
title=document.name,
|
||||||
|
document_type=document.document_type_ref.label if document.document_type_ref is not None else None,
|
||||||
|
authors=tuple(authors),
|
||||||
|
document_date=document.document_date,
|
||||||
|
document_date_raw=document.document_date_raw,
|
||||||
|
location_created=document.location_created,
|
||||||
|
archive_identifier=document.archive_identifier,
|
||||||
|
notes=document.notes,
|
||||||
|
sources=sources,
|
||||||
|
jobs=jobs,
|
||||||
|
)
|
||||||
|
|
||||||
async def list_document_types(
|
async def list_document_types(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
@@ -263,13 +393,16 @@ class DocumentService(ServiceBase):
|
|||||||
session: AsyncSession | None = None,
|
session: AsyncSession | None = None,
|
||||||
) -> Sequence[DocumentType]:
|
) -> Sequence[DocumentType]:
|
||||||
"""List configured document types."""
|
"""List configured document types."""
|
||||||
async with self._session_scope(session) as _session:
|
return await self._document_types.list_entries(active_only=active_only, session=session)
|
||||||
query = select(DocumentType)
|
|
||||||
if active_only:
|
async def list_tags(
|
||||||
query = query.where(col(DocumentType.is_active).is_(True))
|
self,
|
||||||
query = query.order_by(col(DocumentType.normalized_label), col(DocumentType.id))
|
*,
|
||||||
result = await _session.exec(query)
|
active_only: bool = True,
|
||||||
return result.all()
|
session: AsyncSession | None = None,
|
||||||
|
) -> Sequence[Tag]:
|
||||||
|
"""List configured tags."""
|
||||||
|
return await self._tags.list_entries(active_only=active_only, session=session)
|
||||||
|
|
||||||
async def list_document_type_summaries(
|
async def list_document_type_summaries(
|
||||||
self,
|
self,
|
||||||
@@ -277,24 +410,36 @@ class DocumentService(ServiceBase):
|
|||||||
session: AsyncSession | None = None,
|
session: AsyncSession | None = None,
|
||||||
) -> Sequence[DocumentTypeSummary]:
|
) -> Sequence[DocumentTypeSummary]:
|
||||||
"""List Document Types alphabetically with current usage counts."""
|
"""List Document Types alphabetically with current usage counts."""
|
||||||
async with self._session_scope(session) as _session:
|
rows = await self._document_types.list_entries_with_counts(session=session)
|
||||||
query = (
|
|
||||||
select(DocumentType, func.count(col(Document.id)))
|
|
||||||
.outerjoin(Document, col(Document.document_type_id) == col(DocumentType.id))
|
|
||||||
.group_by(col(DocumentType.id))
|
|
||||||
.order_by(col(DocumentType.normalized_label), col(DocumentType.id))
|
|
||||||
)
|
|
||||||
rows = (await _session.exec(query)).all()
|
|
||||||
return [
|
return [
|
||||||
DocumentTypeSummary(
|
RegistrySummary(
|
||||||
id=document_type.id,
|
id=document_type.id,
|
||||||
label=document_type.label,
|
label=document_type.label,
|
||||||
is_active=document_type.is_active,
|
is_active=document_type.is_active,
|
||||||
document_count=int(document_count),
|
is_built_in=document_type.semantic_key is not None,
|
||||||
|
reference_count=document_count,
|
||||||
)
|
)
|
||||||
for document_type, document_count in rows
|
for document_type, document_count in rows
|
||||||
]
|
]
|
||||||
|
|
||||||
|
async def list_tag_summaries(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> Sequence[TagSummary]:
|
||||||
|
"""List Tags alphabetically with current usage counts."""
|
||||||
|
rows = await self._tags.list_entries_with_counts(session=session)
|
||||||
|
return [
|
||||||
|
RegistrySummary(
|
||||||
|
id=tag.id,
|
||||||
|
label=tag.label,
|
||||||
|
is_active=tag.is_active,
|
||||||
|
is_built_in=tag.semantic_key is not None,
|
||||||
|
reference_count=document_count,
|
||||||
|
)
|
||||||
|
for tag, document_count in rows
|
||||||
|
]
|
||||||
|
|
||||||
async def create_document_type(
|
async def create_document_type(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
@@ -303,22 +448,17 @@ class DocumentService(ServiceBase):
|
|||||||
session: AsyncSession | None = None,
|
session: AsyncSession | None = None,
|
||||||
) -> DocumentType:
|
) -> DocumentType:
|
||||||
"""Create a UUID-identified Document Type with a unique label."""
|
"""Create a UUID-identified Document Type with a unique label."""
|
||||||
document_type = DocumentType(
|
return await self._document_types.create_entry(label=label, is_active=is_active, session=session)
|
||||||
label=_normalize_registry_label(label),
|
|
||||||
normalized_label=_document_type_label_key(label),
|
async def create_tag(
|
||||||
is_active=is_active,
|
self,
|
||||||
)
|
*,
|
||||||
async with self._session_scope(session) as _session:
|
label: str,
|
||||||
_session.add(document_type)
|
is_active: bool = True,
|
||||||
try:
|
session: AsyncSession | None = None,
|
||||||
await self._finalize(session=_session, caller_session=session, refresh=(document_type,))
|
) -> Tag:
|
||||||
except IntegrityError as exc:
|
"""Create a UUID-identified Tag with a unique label."""
|
||||||
raise DocumentTypeError(
|
return await self._tags.create_entry(label=label, is_active=is_active, session=session)
|
||||||
f"Document Type label {document_type.label!r} already exists",
|
|
||||||
category=ErrorCategory.CONFLICT,
|
|
||||||
suggestion="Choose a different label or edit the existing type.",
|
|
||||||
) from exc
|
|
||||||
return document_type
|
|
||||||
|
|
||||||
async def read_document_type(
|
async def read_document_type(
|
||||||
self,
|
self,
|
||||||
@@ -327,15 +467,16 @@ class DocumentService(ServiceBase):
|
|||||||
session: AsyncSession | None = None,
|
session: AsyncSession | None = None,
|
||||||
) -> DocumentType:
|
) -> DocumentType:
|
||||||
"""Read a Document Type by id."""
|
"""Read a Document Type by id."""
|
||||||
async with self._session_scope(session) as _session:
|
return await self._document_types.read_entry(document_type_id, session=session)
|
||||||
document_type = await _session.get(DocumentType, document_type_id)
|
|
||||||
if document_type is None:
|
async def read_tag(
|
||||||
raise DocumentTypeError(
|
self,
|
||||||
f"Document Type with id {document_type_id} not found",
|
tag_id: UUID,
|
||||||
category=ErrorCategory.NOT_FOUND,
|
*,
|
||||||
suggestion="Refresh Settings and select an available Document Type.",
|
session: AsyncSession | None = None,
|
||||||
)
|
) -> Tag:
|
||||||
return document_type
|
"""Read a Tag by id."""
|
||||||
|
return await self._tags.read_entry(tag_id, session=session)
|
||||||
|
|
||||||
async def update_document_type(
|
async def update_document_type(
|
||||||
self,
|
self,
|
||||||
@@ -346,27 +487,28 @@ class DocumentService(ServiceBase):
|
|||||||
session: AsyncSession | None = None,
|
session: AsyncSession | None = None,
|
||||||
) -> DocumentType:
|
) -> DocumentType:
|
||||||
"""Update a Document Type label and active state."""
|
"""Update a Document Type label and active state."""
|
||||||
async with self._session_scope(session) as _session:
|
return await self._document_types.update_entry(
|
||||||
document_type = await _session.get(DocumentType, document_type_id)
|
document_type_id,
|
||||||
if document_type is None:
|
label=label,
|
||||||
raise DocumentTypeError(
|
is_active=is_active,
|
||||||
f"Document Type with id {document_type_id} not found",
|
session=session,
|
||||||
category=ErrorCategory.NOT_FOUND,
|
)
|
||||||
suggestion="Refresh Settings and select an available Document Type.",
|
|
||||||
|
async def update_tag(
|
||||||
|
self,
|
||||||
|
tag_id: UUID,
|
||||||
|
*,
|
||||||
|
label: str,
|
||||||
|
is_active: bool,
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> Tag:
|
||||||
|
"""Update a Tag label and active state."""
|
||||||
|
return await self._tags.update_entry(
|
||||||
|
tag_id,
|
||||||
|
label=label,
|
||||||
|
is_active=is_active,
|
||||||
|
session=session,
|
||||||
)
|
)
|
||||||
document_type.label = _normalize_registry_label(label)
|
|
||||||
document_type.normalized_label = _document_type_label_key(label)
|
|
||||||
document_type.is_active = is_active
|
|
||||||
document_type.updated_at = datetime.now(UTC)
|
|
||||||
try:
|
|
||||||
await self._finalize(session=_session, caller_session=session, refresh=(document_type,))
|
|
||||||
except IntegrityError as exc:
|
|
||||||
raise DocumentTypeError(
|
|
||||||
f"Document Type label {document_type.label!r} already exists",
|
|
||||||
category=ErrorCategory.CONFLICT,
|
|
||||||
suggestion="Choose a different label or edit the existing type.",
|
|
||||||
) from exc
|
|
||||||
return document_type
|
|
||||||
|
|
||||||
async def delete_document_type(
|
async def delete_document_type(
|
||||||
self,
|
self,
|
||||||
@@ -375,22 +517,16 @@ class DocumentService(ServiceBase):
|
|||||||
session: AsyncSession | None = None,
|
session: AsyncSession | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Delete an unreferenced Document Type without cascade behavior."""
|
"""Delete an unreferenced Document Type without cascade behavior."""
|
||||||
async with self._session_scope(session) as _session:
|
await self._document_types.delete_entry(document_type_id, session=session)
|
||||||
document_type = await _session.get(DocumentType, document_type_id)
|
|
||||||
if document_type is None:
|
async def delete_tag(
|
||||||
raise DocumentTypeError(
|
self,
|
||||||
f"Document Type with id {document_type_id} not found",
|
tag_id: UUID,
|
||||||
category=ErrorCategory.NOT_FOUND,
|
*,
|
||||||
suggestion="Refresh Settings and select an available Document Type.",
|
session: AsyncSession | None = None,
|
||||||
)
|
) -> None:
|
||||||
if await self._document_type_is_referenced(session=_session, document_type=document_type):
|
"""Delete an unreferenced Tag without cascade behavior."""
|
||||||
raise DocumentTypeError(
|
await self._tags.delete_entry(tag_id, session=session)
|
||||||
f"Document Type {document_type.label!r} is referenced and cannot be deleted",
|
|
||||||
category=ErrorCategory.CONFLICT,
|
|
||||||
suggestion="Deactivate the type instead; historical Documents will retain it.",
|
|
||||||
)
|
|
||||||
await _session.delete(document_type)
|
|
||||||
await self._finalize(session=_session, caller_session=session)
|
|
||||||
|
|
||||||
async def is_document_type_referenced(
|
async def is_document_type_referenced(
|
||||||
self,
|
self,
|
||||||
@@ -399,29 +535,16 @@ class DocumentService(ServiceBase):
|
|||||||
session: AsyncSession | None = None,
|
session: AsyncSession | None = None,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Return whether a Document references a Document Type."""
|
"""Return whether a Document references a Document Type."""
|
||||||
async with self._session_scope(session) as _session:
|
return await self._document_types.is_referenced(document_type_id, session=session)
|
||||||
document_type = await _session.get(DocumentType, document_type_id)
|
|
||||||
if document_type is None:
|
|
||||||
raise DocumentTypeError(
|
|
||||||
f"Document Type with id {document_type_id} not found",
|
|
||||||
category=ErrorCategory.NOT_FOUND,
|
|
||||||
suggestion="Refresh Settings and select an available Document Type.",
|
|
||||||
)
|
|
||||||
return await self._document_type_is_referenced(
|
|
||||||
session=_session,
|
|
||||||
document_type=document_type,
|
|
||||||
)
|
|
||||||
|
|
||||||
@staticmethod
|
async def is_tag_referenced(
|
||||||
async def _document_type_is_referenced(
|
self,
|
||||||
|
tag_id: UUID,
|
||||||
*,
|
*,
|
||||||
session: AsyncSession,
|
session: AsyncSession | None = None,
|
||||||
document_type: DocumentType,
|
|
||||||
) -> bool:
|
) -> bool:
|
||||||
reference = (
|
"""Return whether a Document references a Tag."""
|
||||||
await session.exec(select(Document.id).where(Document.document_type_id == document_type.id))
|
return await self._tags.is_referenced(tag_id, session=session)
|
||||||
).first()
|
|
||||||
return reference is not None
|
|
||||||
|
|
||||||
async def set_document_type(
|
async def set_document_type(
|
||||||
self,
|
self,
|
||||||
@@ -432,9 +555,74 @@ class DocumentService(ServiceBase):
|
|||||||
) -> Document:
|
) -> Document:
|
||||||
"""Set a Document Type by UUID."""
|
"""Set a Document Type by UUID."""
|
||||||
async with self._session_scope(session) as _session:
|
async with self._session_scope(session) as _session:
|
||||||
document = await self._get_document_or_raise(session=_session, document_id=document_id)
|
document = await self._read_document(session=_session, document_id=document_id)
|
||||||
document.document_type_id = document_type_id
|
document.document_type_id = document_type_id
|
||||||
await self._validate_document_type(session=_session, document=document)
|
await self._validate_document_type(session=_session, document=document)
|
||||||
document.updated_at = datetime.now(UTC)
|
|
||||||
await self._finalize(session=_session, caller_session=session, refresh=(document,))
|
await self._finalize(session=_session, caller_session=session, refresh=(document,))
|
||||||
return document
|
return document
|
||||||
|
|
||||||
|
async def sync_document_tags_by_labels(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
document_id: UUID,
|
||||||
|
labels: Sequence[str],
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Replace a Document's tag set using label-based assignment."""
|
||||||
|
normalized_labels = [self._tags.normalize_label(label) for label in labels]
|
||||||
|
deduplicated_labels = list(dict.fromkeys(normalized_labels))
|
||||||
|
label_keys = [self._tags.label_key(label) for label in deduplicated_labels]
|
||||||
|
|
||||||
|
async with self._session_scope(session) as _session:
|
||||||
|
existing_document = await _session.get(Document, document_id)
|
||||||
|
if existing_document is None:
|
||||||
|
raise DocumentError(
|
||||||
|
f"Document with id {document_id} not found",
|
||||||
|
category=ErrorCategory.NOT_FOUND,
|
||||||
|
suggestion="Refresh and select an existing document.",
|
||||||
|
)
|
||||||
|
|
||||||
|
existing_tags = (
|
||||||
|
(await _session.exec(select(Tag).where(col(Tag.normalized_label).in_(label_keys)))).all()
|
||||||
|
if label_keys
|
||||||
|
else []
|
||||||
|
)
|
||||||
|
tags_by_key = {tag.normalized_label: tag for tag in existing_tags}
|
||||||
|
selected_tag_ids: set[UUID] = set()
|
||||||
|
|
||||||
|
for label in deduplicated_labels:
|
||||||
|
key = self._tags.label_key(label)
|
||||||
|
tag = tags_by_key.get(key)
|
||||||
|
if tag is None:
|
||||||
|
tag = await self._tags.create_entry(label=label, is_active=True, session=_session)
|
||||||
|
tags_by_key[key] = tag
|
||||||
|
selected_tag_ids.add(tag.id)
|
||||||
|
|
||||||
|
links = (await _session.exec(select(DocumentTag).where(DocumentTag.document_id == document_id))).all()
|
||||||
|
existing_ids = {link.tag_id for link in links}
|
||||||
|
|
||||||
|
for link in links:
|
||||||
|
if link.tag_id not in selected_tag_ids:
|
||||||
|
await _session.delete(link)
|
||||||
|
|
||||||
|
for tag_id in selected_tag_ids - existing_ids:
|
||||||
|
_session.add(DocumentTag(document_id=document_id, tag_id=tag_id))
|
||||||
|
|
||||||
|
await self._finalize(session=_session, caller_session=session)
|
||||||
|
|
||||||
|
|
||||||
|
def _print_media_type(filename: str) -> str:
|
||||||
|
"""Resolve a stored Source filename to its MIME type for print rendering."""
|
||||||
|
mime_type = lookup_source_mime_type(filename)
|
||||||
|
if mime_type is None:
|
||||||
|
raise DocumentError(
|
||||||
|
f"Unsupported Source format: {Path(filename).suffix.lower() or '<none>'}",
|
||||||
|
category=ErrorCategory.USER_INPUT,
|
||||||
|
suggestion=f"Use one of the supported Source formats: {supported_source_formats()}.",
|
||||||
|
)
|
||||||
|
return mime_type
|
||||||
|
|
||||||
|
|
||||||
|
def _current_print_text(revised_text: str | None, raw_transcription: str | None) -> str | None:
|
||||||
|
selected = revised_text if revised_text is not None else raw_transcription
|
||||||
|
return selected if selected is not None and selected.strip() else None
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
"""Error vocabulary shared across the source, evidence, and prompt services.
|
||||||
|
|
||||||
|
These live in a neutral module rather than in the service that raises them
|
||||||
|
because more than one service raises them, and ``services.instructions.md``
|
||||||
|
forbids a service module from importing a sibling. Orchestration modules and
|
||||||
|
the UI import from here, so the exception a caller catches does not change when
|
||||||
|
an operation moves between services.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from transcription.errors import AppError
|
||||||
|
|
||||||
|
|
||||||
|
class PromptLoadError(AppError):
|
||||||
|
"""Raised when prompt artifacts cannot be loaded safely."""
|
||||||
|
|
||||||
|
|
||||||
|
class PromptStoreError(PromptLoadError):
|
||||||
|
"""Raised when prompt storage validation or persistence fails."""
|
||||||
|
|
||||||
|
|
||||||
|
class TranscriptionError(AppError):
|
||||||
|
"""Raised when transcription execution fails."""
|
||||||
|
|
||||||
|
|
||||||
|
class TranscriptionNotFoundError(TranscriptionError):
|
||||||
|
"""Raised when a transcription-related resource is not found."""
|
||||||
|
|
||||||
|
|
||||||
|
class SourceDeleteBlockedError(TranscriptionError):
|
||||||
|
"""Raised when source deletion is blocked by dependency policy."""
|
||||||
|
|
||||||
|
|
||||||
|
class CandidatePromotionError(TranscriptionError):
|
||||||
|
"""Raised when a machine attempt cannot be selected for its Source."""
|
||||||
@@ -0,0 +1,230 @@
|
|||||||
|
"""Read and export the immutable execution evidence trail.
|
||||||
|
|
||||||
|
``ExecutionAttempt`` is append-only: one row per provider call, written once by
|
||||||
|
the transcription workflow and never updated. Everything here is therefore a
|
||||||
|
read, a projection, or an export, with one exception - ``promote_machine_attempt``
|
||||||
|
selects which attempt a ``Source`` presents, which is an evidence decision even
|
||||||
|
though the write lands on ``Source``.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import hashlib
|
||||||
|
from collections.abc import Sequence
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from pydantic import JsonValue
|
||||||
|
from sqlalchemy import inspect as sqlalchemy_inspect
|
||||||
|
from sqlmodel import col
|
||||||
|
from sqlmodel import select
|
||||||
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||||
|
|
||||||
|
from transcription.db.models import ExecutionAttempt
|
||||||
|
from transcription.db.models import JobSourceStatus
|
||||||
|
from transcription.db.models import Source
|
||||||
|
from transcription.errors import ErrorCategory
|
||||||
|
|
||||||
|
from ..db.loading import defer
|
||||||
|
from .base import ServiceBase
|
||||||
|
from .errors import CandidatePromotionError
|
||||||
|
from .errors import TranscriptionNotFoundError
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class LatestExecutionAttempt:
|
||||||
|
"""One execution attempt plus the loader facts a caller needs to render it."""
|
||||||
|
|
||||||
|
attempt: ExecutionAttempt
|
||||||
|
transport_body_deferred: bool
|
||||||
|
|
||||||
|
|
||||||
|
class EvidenceService(ServiceBase):
|
||||||
|
"""Read, project, and export execution attempt evidence."""
|
||||||
|
|
||||||
|
async def read_latest_job_error_category(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
job_id: UUID,
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> str | None:
|
||||||
|
"""Read the latest persisted execution-attempt error category for a job."""
|
||||||
|
async with self._session_scope(session) as _session:
|
||||||
|
query = (
|
||||||
|
select(ExecutionAttempt.error_category)
|
||||||
|
.where(ExecutionAttempt.job_id == job_id)
|
||||||
|
.where(col(ExecutionAttempt.error_category).is_not(None))
|
||||||
|
.order_by(
|
||||||
|
col(ExecutionAttempt.created_at).desc(),
|
||||||
|
col(ExecutionAttempt.id).desc(),
|
||||||
|
)
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
return (await _session.exec(query)).first()
|
||||||
|
|
||||||
|
async def read_latest_execution_attempt(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
job_source_id: UUID,
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> LatestExecutionAttempt | None:
|
||||||
|
"""Read only the latest immutable attempt for one compatibility projection.
|
||||||
|
|
||||||
|
The transport body is deferred because it can be arbitrarily large; the
|
||||||
|
returned read model reports that as a plain flag so callers never have to
|
||||||
|
inspect ORM loader state.
|
||||||
|
"""
|
||||||
|
async with self._session_scope(session) as _session:
|
||||||
|
query = (
|
||||||
|
select(ExecutionAttempt)
|
||||||
|
.options(defer(ExecutionAttempt.transport_body))
|
||||||
|
.where(ExecutionAttempt.job_source_id == job_source_id)
|
||||||
|
.order_by(
|
||||||
|
col(ExecutionAttempt.attempt_number).desc(),
|
||||||
|
col(ExecutionAttempt.id).desc(),
|
||||||
|
)
|
||||||
|
.limit(1)
|
||||||
|
)
|
||||||
|
attempt = (await _session.exec(query)).first()
|
||||||
|
if attempt is None:
|
||||||
|
return None
|
||||||
|
deferred = "transport_body" in sqlalchemy_inspect(attempt).unloaded
|
||||||
|
return LatestExecutionAttempt(attempt=attempt, transport_body_deferred=deferred)
|
||||||
|
|
||||||
|
async def list_execution_attempts(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
source_id: UUID | None = None,
|
||||||
|
job_id: UUID | None = None,
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> Sequence[ExecutionAttempt]:
|
||||||
|
"""List immutable execution evidence in stable attempt order."""
|
||||||
|
async with self._session_scope(session) as _session:
|
||||||
|
query = select(ExecutionAttempt)
|
||||||
|
if source_id is not None:
|
||||||
|
query = query.where(ExecutionAttempt.source_id == source_id)
|
||||||
|
if job_id is not None:
|
||||||
|
query = query.where(ExecutionAttempt.job_id == job_id)
|
||||||
|
query = query.order_by(
|
||||||
|
col(ExecutionAttempt.job_id),
|
||||||
|
col(ExecutionAttempt.source_id),
|
||||||
|
col(ExecutionAttempt.attempt_number),
|
||||||
|
col(ExecutionAttempt.id),
|
||||||
|
)
|
||||||
|
return (await _session.exec(query)).all()
|
||||||
|
|
||||||
|
async def promote_machine_attempt(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
source_id: UUID,
|
||||||
|
execution_attempt_id: UUID,
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> Source:
|
||||||
|
"""Atomically select one successful machine attempt as the Source projection."""
|
||||||
|
async with self._session_scope(session) as _session:
|
||||||
|
source = await self._read_source(
|
||||||
|
session=_session,
|
||||||
|
source_id=source_id,
|
||||||
|
suggestion="Refresh Source Detail and retry.",
|
||||||
|
)
|
||||||
|
attempt = await _session.get(ExecutionAttempt, execution_attempt_id)
|
||||||
|
if (
|
||||||
|
attempt is None
|
||||||
|
or attempt.source_id != source_id
|
||||||
|
or attempt.status != JobSourceStatus.TRANSCRIBED
|
||||||
|
or not attempt.raw_transcription
|
||||||
|
):
|
||||||
|
raise CandidatePromotionError(
|
||||||
|
"Only a successful transcription attempt belonging to this Source can be selected",
|
||||||
|
category=ErrorCategory.VALIDATION,
|
||||||
|
suggestion="Select an available successful candidate from Source Detail.",
|
||||||
|
)
|
||||||
|
source.preferred_execution_attempt_id = attempt.id
|
||||||
|
source.raw_transcription = attempt.raw_transcription
|
||||||
|
await self._finalize(session=_session, caller_session=session, refresh=(source,))
|
||||||
|
return source
|
||||||
|
|
||||||
|
async def build_evidence_export(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
source_id: UUID,
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> dict[str, JsonValue]:
|
||||||
|
"""Build a versioned, source-reference-only evidence export."""
|
||||||
|
async with self._session_scope(session) as _session:
|
||||||
|
source = await self._read_source(session=_session, source_id=source_id)
|
||||||
|
attempts = list(await self.list_execution_attempts(source_id=source_id, session=_session))
|
||||||
|
|
||||||
|
attempt_payloads = [
|
||||||
|
{
|
||||||
|
"id": str(attempt.id),
|
||||||
|
"job_id": str(attempt.job_id),
|
||||||
|
"source_id": str(attempt.source_id),
|
||||||
|
"attempt_number": attempt.attempt_number,
|
||||||
|
"status": attempt.status.value,
|
||||||
|
"provider": attempt.provider,
|
||||||
|
"model": attempt.model,
|
||||||
|
"request_manifest": attempt.request_manifest,
|
||||||
|
"request_manifest_sha256": attempt.request_manifest_sha256,
|
||||||
|
"request_manifest_schema_version": attempt.request_manifest_schema_version,
|
||||||
|
"transport": {
|
||||||
|
"response_received": attempt.response_received,
|
||||||
|
"status_code": attempt.transport_status_code,
|
||||||
|
"body_base64": (
|
||||||
|
base64.b64encode(attempt.transport_body).decode("ascii")
|
||||||
|
if attempt.transport_body is not None
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
"body_sha256": (
|
||||||
|
hashlib.sha256(attempt.transport_body).hexdigest()
|
||||||
|
if attempt.transport_body is not None
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
"content_type": attempt.transport_content_type,
|
||||||
|
"content_encoding": attempt.transport_content_encoding,
|
||||||
|
"safe_headers": attempt.transport_safe_headers,
|
||||||
|
"request_id": attempt.router_request_id,
|
||||||
|
"generation_id": attempt.router_generation_id,
|
||||||
|
},
|
||||||
|
"sdk_response_snapshot": attempt.sdk_response_snapshot,
|
||||||
|
"normalized_metadata": attempt.normalized_metadata,
|
||||||
|
"software_context": attempt.software_context,
|
||||||
|
"raw_transcription": attempt.raw_transcription,
|
||||||
|
"error_category": attempt.error_category,
|
||||||
|
"error_detail": attempt.error_detail,
|
||||||
|
"failure_phase": attempt.failure_phase,
|
||||||
|
"started_at": attempt.started_at.isoformat(),
|
||||||
|
"finished_at": attempt.finished_at.isoformat(),
|
||||||
|
"duration_ms": attempt.duration_ms,
|
||||||
|
}
|
||||||
|
for attempt in attempts
|
||||||
|
]
|
||||||
|
return {
|
||||||
|
"schema_name": "transcription.evidence-export",
|
||||||
|
"schema_version": "1",
|
||||||
|
"source": {
|
||||||
|
"id": str(source.id),
|
||||||
|
"digest_sha256": source.file_hash,
|
||||||
|
"byte_size": source.file_size_bytes,
|
||||||
|
"page_number": source.page_number,
|
||||||
|
"upload_name": source.upload_name,
|
||||||
|
},
|
||||||
|
"attempts": attempt_payloads,
|
||||||
|
}
|
||||||
|
|
||||||
|
async def _read_source(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
session: AsyncSession,
|
||||||
|
source_id: UUID,
|
||||||
|
suggestion: str = "Verify the source id and retry.",
|
||||||
|
) -> Source:
|
||||||
|
return await self._get_or_raise(
|
||||||
|
Source,
|
||||||
|
source_id,
|
||||||
|
session=session,
|
||||||
|
error=TranscriptionNotFoundError,
|
||||||
|
noun="Source",
|
||||||
|
suggestion=suggestion,
|
||||||
|
)
|
||||||
@@ -0,0 +1,599 @@
|
|||||||
|
"""GEDCOM parsing and import helpers for maintenance-driven genealogy sync."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Mapping
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from datetime import date
|
||||||
|
from pathlib import Path
|
||||||
|
from uuid import UUID
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from gedcom.element.element import Element
|
||||||
|
from gedcom.parser import Parser
|
||||||
|
from sqlalchemy import or_
|
||||||
|
from sqlmodel import col
|
||||||
|
from sqlmodel import delete
|
||||||
|
from sqlmodel import select
|
||||||
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||||
|
|
||||||
|
from transcription.db.models import GenealogyCitation
|
||||||
|
from transcription.db.models import GenealogyCitationFactType
|
||||||
|
from transcription.db.models import GenealogyCitationSourceKind
|
||||||
|
from transcription.db.models import GenealogyFamily
|
||||||
|
from transcription.db.models import GenealogyFamilyChild
|
||||||
|
from transcription.db.models import GenealogyPerson
|
||||||
|
from transcription.errors import AppError
|
||||||
|
from transcription.errors import ErrorCategory
|
||||||
|
|
||||||
|
_MONTHS = {
|
||||||
|
"JAN": 1,
|
||||||
|
"FEB": 2,
|
||||||
|
"MAR": 3,
|
||||||
|
"APR": 4,
|
||||||
|
"MAY": 5,
|
||||||
|
"JUN": 6,
|
||||||
|
"JUL": 7,
|
||||||
|
"AUG": 8,
|
||||||
|
"SEP": 9,
|
||||||
|
"OCT": 10,
|
||||||
|
"NOV": 11,
|
||||||
|
"DEC": 12,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class ParsedCitation:
|
||||||
|
fact_type: GenealogyCitationFactType
|
||||||
|
raw_citation_text: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class ParsedPerson:
|
||||||
|
pointer: str
|
||||||
|
fs_id: str | None
|
||||||
|
full_name: str
|
||||||
|
birth_date: date | None
|
||||||
|
birth_date_raw: str | None
|
||||||
|
birth_place: str | None
|
||||||
|
death_date: date | None
|
||||||
|
death_date_raw: str | None
|
||||||
|
death_place: str | None
|
||||||
|
citations: tuple[ParsedCitation, ...]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class ParsedFamilyChild:
|
||||||
|
child_pointer: str
|
||||||
|
relationship_type: str | None
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class ParsedFamily:
|
||||||
|
fs_family_id: str | None
|
||||||
|
husband_pointer: str | None
|
||||||
|
wife_pointer: str | None
|
||||||
|
marriage_date: date | None
|
||||||
|
marriage_date_raw: str | None
|
||||||
|
marriage_place: str | None
|
||||||
|
children: tuple[ParsedFamilyChild, ...]
|
||||||
|
citations: tuple[ParsedCitation, ...]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class ParsedGedcom:
|
||||||
|
people: tuple[ParsedPerson, ...]
|
||||||
|
families: tuple[ParsedFamily, ...]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class GedcomImportResult:
|
||||||
|
new_people: int
|
||||||
|
updated_people: int
|
||||||
|
skipped_people_without_fs_id: int
|
||||||
|
new_families: int
|
||||||
|
updated_families: int
|
||||||
|
skipped_families_without_fs_id: int
|
||||||
|
family_children: int
|
||||||
|
citations: int
|
||||||
|
|
||||||
|
|
||||||
|
class GedcomImportError(AppError):
|
||||||
|
"""Raised when GEDCOM content cannot be parsed or imported."""
|
||||||
|
|
||||||
|
|
||||||
|
def parse_gedcom(*, file_path: Path) -> ParsedGedcom:
|
||||||
|
parser = Parser()
|
||||||
|
try:
|
||||||
|
parser.parse_file(str(file_path))
|
||||||
|
except Exception as exc:
|
||||||
|
raise GedcomImportError(
|
||||||
|
"GEDCOM file could not be parsed.",
|
||||||
|
category=ErrorCategory.VALIDATION,
|
||||||
|
suggestion="Upload a GEDCOM 5.5.1-compatible export and retry.",
|
||||||
|
detail=f"{type(exc).__name__}: {exc}",
|
||||||
|
) from exc
|
||||||
|
|
||||||
|
people: list[ParsedPerson] = []
|
||||||
|
families: list[ParsedFamily] = []
|
||||||
|
for element in parser.get_root_child_elements():
|
||||||
|
tag = element.get_tag()
|
||||||
|
if tag == "INDI":
|
||||||
|
people.append(_parse_person(element))
|
||||||
|
elif tag == "FAM":
|
||||||
|
families.append(_parse_family(element))
|
||||||
|
return ParsedGedcom(people=tuple(people), families=tuple(families))
|
||||||
|
|
||||||
|
|
||||||
|
async def import_gedcom_file(*, session: AsyncSession, file_path: Path) -> GedcomImportResult:
|
||||||
|
parsed = parse_gedcom(file_path=file_path)
|
||||||
|
pointer_to_fs_id = _pointer_to_fs_id_map(parsed=parsed)
|
||||||
|
people_by_fs_id, new_people, updated_people, skipped_people_without_fs_id = await _upsert_people(
|
||||||
|
session=session,
|
||||||
|
parsed=parsed,
|
||||||
|
)
|
||||||
|
(
|
||||||
|
families_by_fs_id,
|
||||||
|
new_families,
|
||||||
|
updated_families,
|
||||||
|
skipped_families_without_fs_id,
|
||||||
|
family_children,
|
||||||
|
) = await _upsert_families(
|
||||||
|
session=session,
|
||||||
|
parsed=parsed,
|
||||||
|
pointer_to_fs_id=pointer_to_fs_id,
|
||||||
|
people_by_fs_id=people_by_fs_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
citation_rows = _citation_rows(
|
||||||
|
parsed=parsed,
|
||||||
|
pointer_to_fs_id=pointer_to_fs_id,
|
||||||
|
people=people_by_fs_id,
|
||||||
|
families=families_by_fs_id,
|
||||||
|
)
|
||||||
|
await _replace_imported_citations(
|
||||||
|
session=session,
|
||||||
|
person_ids={item.id for item in people_by_fs_id.values()},
|
||||||
|
family_ids={item.id for item in families_by_fs_id.values()},
|
||||||
|
citations=citation_rows,
|
||||||
|
)
|
||||||
|
await session.commit()
|
||||||
|
return GedcomImportResult(
|
||||||
|
new_people=new_people,
|
||||||
|
updated_people=updated_people,
|
||||||
|
skipped_people_without_fs_id=skipped_people_without_fs_id,
|
||||||
|
new_families=new_families,
|
||||||
|
updated_families=updated_families,
|
||||||
|
skipped_families_without_fs_id=skipped_families_without_fs_id,
|
||||||
|
family_children=family_children,
|
||||||
|
citations=len(citation_rows),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def _load_people_by_fs_id(*, session: AsyncSession, fs_ids: set[str]) -> dict[str, GenealogyPerson]:
|
||||||
|
if not fs_ids:
|
||||||
|
return {}
|
||||||
|
query = select(GenealogyPerson).where(col(GenealogyPerson.fs_id).in_(fs_ids))
|
||||||
|
return {person.fs_id: person for person in (await session.exec(query)).all()}
|
||||||
|
|
||||||
|
|
||||||
|
async def _load_families_by_fs_id(*, session: AsyncSession, fs_family_ids: set[str]) -> dict[str, GenealogyFamily]:
|
||||||
|
if not fs_family_ids:
|
||||||
|
return {}
|
||||||
|
query = select(GenealogyFamily).where(col(GenealogyFamily.fs_family_id).in_(fs_family_ids))
|
||||||
|
return {family.fs_family_id: family for family in (await session.exec(query)).all()}
|
||||||
|
|
||||||
|
|
||||||
|
def _pointer_to_fs_id_map(*, parsed: ParsedGedcom) -> dict[str, str]:
|
||||||
|
return {person.pointer: person.fs_id for person in parsed.people if person.pointer and person.fs_id is not None}
|
||||||
|
|
||||||
|
|
||||||
|
async def _upsert_people(
|
||||||
|
*,
|
||||||
|
session: AsyncSession,
|
||||||
|
parsed: ParsedGedcom,
|
||||||
|
) -> tuple[dict[str, GenealogyPerson], int, int, int]:
|
||||||
|
people_with_fs_id = [person for person in parsed.people if person.fs_id is not None]
|
||||||
|
fs_ids = {person.fs_id for person in people_with_fs_id if person.fs_id is not None}
|
||||||
|
people_by_fs_id = await _load_people_by_fs_id(session=session, fs_ids=fs_ids)
|
||||||
|
new_people = 0
|
||||||
|
updated_people = 0
|
||||||
|
|
||||||
|
for person in people_with_fs_id:
|
||||||
|
assert person.fs_id is not None
|
||||||
|
existing = people_by_fs_id.get(person.fs_id)
|
||||||
|
if existing is None:
|
||||||
|
existing = GenealogyPerson(
|
||||||
|
fs_id=person.fs_id,
|
||||||
|
full_name=person.full_name,
|
||||||
|
birth_date=person.birth_date,
|
||||||
|
birth_date_raw=person.birth_date_raw,
|
||||||
|
birth_place=person.birth_place,
|
||||||
|
death_date=person.death_date,
|
||||||
|
death_date_raw=person.death_date_raw,
|
||||||
|
death_place=person.death_place,
|
||||||
|
)
|
||||||
|
session.add(existing)
|
||||||
|
await session.flush()
|
||||||
|
people_by_fs_id[person.fs_id] = existing
|
||||||
|
new_people += 1
|
||||||
|
continue
|
||||||
|
if _apply_person_updates(existing=existing, person=person):
|
||||||
|
updated_people += 1
|
||||||
|
|
||||||
|
skipped = len(parsed.people) - len(people_with_fs_id)
|
||||||
|
return people_by_fs_id, new_people, updated_people, skipped
|
||||||
|
|
||||||
|
|
||||||
|
def _apply_person_updates(*, existing: GenealogyPerson, person: ParsedPerson) -> bool:
|
||||||
|
changed = False
|
||||||
|
fields = (
|
||||||
|
("full_name", person.full_name),
|
||||||
|
("birth_date", person.birth_date),
|
||||||
|
("birth_date_raw", person.birth_date_raw),
|
||||||
|
("birth_place", person.birth_place),
|
||||||
|
("death_date", person.death_date),
|
||||||
|
("death_date_raw", person.death_date_raw),
|
||||||
|
("death_place", person.death_place),
|
||||||
|
)
|
||||||
|
for name, value in fields:
|
||||||
|
if getattr(existing, name) != value:
|
||||||
|
setattr(existing, name, value)
|
||||||
|
changed = True
|
||||||
|
return changed
|
||||||
|
|
||||||
|
|
||||||
|
async def _upsert_families(
|
||||||
|
*,
|
||||||
|
session: AsyncSession,
|
||||||
|
parsed: ParsedGedcom,
|
||||||
|
pointer_to_fs_id: Mapping[str, str],
|
||||||
|
people_by_fs_id: dict[str, GenealogyPerson],
|
||||||
|
) -> tuple[dict[str, GenealogyFamily], int, int, int, int]:
|
||||||
|
families_with_fs_id = [family for family in parsed.families if family.fs_family_id is not None]
|
||||||
|
fs_family_ids = {family.fs_family_id for family in families_with_fs_id if family.fs_family_id is not None}
|
||||||
|
families_by_fs_id = await _load_families_by_fs_id(session=session, fs_family_ids=fs_family_ids)
|
||||||
|
new_families = 0
|
||||||
|
updated_families = 0
|
||||||
|
family_children = 0
|
||||||
|
|
||||||
|
for family in families_with_fs_id:
|
||||||
|
assert family.fs_family_id is not None
|
||||||
|
husband_id = _person_id_from_pointer(
|
||||||
|
pointer=family.husband_pointer,
|
||||||
|
pointer_to_fs_id=pointer_to_fs_id,
|
||||||
|
people=people_by_fs_id,
|
||||||
|
)
|
||||||
|
wife_id = _person_id_from_pointer(
|
||||||
|
pointer=family.wife_pointer,
|
||||||
|
pointer_to_fs_id=pointer_to_fs_id,
|
||||||
|
people=people_by_fs_id,
|
||||||
|
)
|
||||||
|
target = families_by_fs_id.get(family.fs_family_id)
|
||||||
|
if target is None:
|
||||||
|
target = GenealogyFamily(
|
||||||
|
fs_family_id=family.fs_family_id,
|
||||||
|
husband_id=husband_id,
|
||||||
|
wife_id=wife_id,
|
||||||
|
marriage_date=family.marriage_date,
|
||||||
|
marriage_date_raw=family.marriage_date_raw,
|
||||||
|
marriage_place=family.marriage_place,
|
||||||
|
)
|
||||||
|
session.add(target)
|
||||||
|
await session.flush()
|
||||||
|
families_by_fs_id[family.fs_family_id] = target
|
||||||
|
new_families += 1
|
||||||
|
elif _apply_family_updates(existing=target, family=family, husband_id=husband_id, wife_id=wife_id):
|
||||||
|
updated_families += 1
|
||||||
|
|
||||||
|
family_children += await _replace_family_children(
|
||||||
|
session=session,
|
||||||
|
family=family,
|
||||||
|
family_id=target.id,
|
||||||
|
pointer_to_fs_id=pointer_to_fs_id,
|
||||||
|
people=people_by_fs_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
skipped = len(parsed.families) - len(families_with_fs_id)
|
||||||
|
return families_by_fs_id, new_families, updated_families, skipped, family_children
|
||||||
|
|
||||||
|
|
||||||
|
def _apply_family_updates(
|
||||||
|
*,
|
||||||
|
existing: GenealogyFamily,
|
||||||
|
family: ParsedFamily,
|
||||||
|
husband_id: UUID | None,
|
||||||
|
wife_id: UUID | None,
|
||||||
|
) -> bool:
|
||||||
|
changed = False
|
||||||
|
fields = (
|
||||||
|
("husband_id", husband_id),
|
||||||
|
("wife_id", wife_id),
|
||||||
|
("marriage_date", family.marriage_date),
|
||||||
|
("marriage_date_raw", family.marriage_date_raw),
|
||||||
|
("marriage_place", family.marriage_place),
|
||||||
|
)
|
||||||
|
for name, value in fields:
|
||||||
|
if getattr(existing, name) != value:
|
||||||
|
setattr(existing, name, value)
|
||||||
|
changed = True
|
||||||
|
return changed
|
||||||
|
|
||||||
|
|
||||||
|
async def _replace_family_children(
|
||||||
|
*,
|
||||||
|
session: AsyncSession,
|
||||||
|
family: ParsedFamily,
|
||||||
|
family_id: UUID,
|
||||||
|
pointer_to_fs_id: Mapping[str, str],
|
||||||
|
people: dict[str, GenealogyPerson],
|
||||||
|
) -> int:
|
||||||
|
await session.exec(delete(GenealogyFamilyChild).where(col(GenealogyFamilyChild.family_id) == family_id))
|
||||||
|
child_rows = _family_child_rows(
|
||||||
|
family=family,
|
||||||
|
family_id=family_id,
|
||||||
|
pointer_to_fs_id=pointer_to_fs_id,
|
||||||
|
people=people,
|
||||||
|
)
|
||||||
|
for child_row in child_rows:
|
||||||
|
session.add(child_row)
|
||||||
|
return len(child_rows)
|
||||||
|
|
||||||
|
|
||||||
|
def _person_id_from_pointer(
|
||||||
|
*,
|
||||||
|
pointer: str | None,
|
||||||
|
pointer_to_fs_id: Mapping[str, str],
|
||||||
|
people: dict[str, GenealogyPerson],
|
||||||
|
) -> UUID | None:
|
||||||
|
if pointer is None:
|
||||||
|
return None
|
||||||
|
fs_id = pointer_to_fs_id.get(pointer)
|
||||||
|
if fs_id is None:
|
||||||
|
return None
|
||||||
|
person = people.get(fs_id)
|
||||||
|
return person.id if person is not None else None
|
||||||
|
|
||||||
|
|
||||||
|
def _family_child_rows(
|
||||||
|
*,
|
||||||
|
family: ParsedFamily,
|
||||||
|
family_id: UUID,
|
||||||
|
pointer_to_fs_id: Mapping[str, str],
|
||||||
|
people: dict[str, GenealogyPerson],
|
||||||
|
) -> list[GenealogyFamilyChild]:
|
||||||
|
rows: list[GenealogyFamilyChild] = []
|
||||||
|
seen_child_ids: set[UUID] = set()
|
||||||
|
for child in family.children:
|
||||||
|
child_id = _person_id_from_pointer(
|
||||||
|
pointer=child.child_pointer,
|
||||||
|
pointer_to_fs_id=pointer_to_fs_id,
|
||||||
|
people=people,
|
||||||
|
)
|
||||||
|
if child_id is None or child_id in seen_child_ids:
|
||||||
|
continue
|
||||||
|
seen_child_ids.add(child_id)
|
||||||
|
rows.append(
|
||||||
|
GenealogyFamilyChild(
|
||||||
|
id=uuid4(),
|
||||||
|
family_id=family_id,
|
||||||
|
child_id=child_id,
|
||||||
|
relationship_type=child.relationship_type,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return rows
|
||||||
|
|
||||||
|
|
||||||
|
def _citation_rows(
|
||||||
|
*,
|
||||||
|
parsed: ParsedGedcom,
|
||||||
|
pointer_to_fs_id: Mapping[str, str],
|
||||||
|
people: dict[str, GenealogyPerson],
|
||||||
|
families: dict[str, GenealogyFamily],
|
||||||
|
) -> list[GenealogyCitation]:
|
||||||
|
rows: list[GenealogyCitation] = []
|
||||||
|
seen: set[tuple[UUID | None, UUID | None, str, str]] = set()
|
||||||
|
|
||||||
|
for person in parsed.people:
|
||||||
|
fs_id = pointer_to_fs_id.get(person.pointer)
|
||||||
|
if fs_id is None:
|
||||||
|
continue
|
||||||
|
person_row = people.get(fs_id)
|
||||||
|
if person_row is None:
|
||||||
|
continue
|
||||||
|
for citation in person.citations:
|
||||||
|
key = (person_row.id, None, citation.fact_type.value, citation.raw_citation_text)
|
||||||
|
if key in seen:
|
||||||
|
continue
|
||||||
|
seen.add(key)
|
||||||
|
rows.append(
|
||||||
|
GenealogyCitation(
|
||||||
|
id=uuid4(),
|
||||||
|
genealogy_person_id=person_row.id,
|
||||||
|
genealogy_family_id=None,
|
||||||
|
fact_type=citation.fact_type,
|
||||||
|
raw_citation_text=citation.raw_citation_text,
|
||||||
|
source_kind=GenealogyCitationSourceKind.FAMILYSEARCH_IMPORTED,
|
||||||
|
document_id=None,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
for family in parsed.families:
|
||||||
|
if family.fs_family_id is None:
|
||||||
|
continue
|
||||||
|
family_row = families.get(family.fs_family_id)
|
||||||
|
if family_row is None:
|
||||||
|
continue
|
||||||
|
for citation in family.citations:
|
||||||
|
key = (None, family_row.id, citation.fact_type.value, citation.raw_citation_text)
|
||||||
|
if key in seen:
|
||||||
|
continue
|
||||||
|
seen.add(key)
|
||||||
|
rows.append(
|
||||||
|
GenealogyCitation(
|
||||||
|
id=uuid4(),
|
||||||
|
genealogy_person_id=None,
|
||||||
|
genealogy_family_id=family_row.id,
|
||||||
|
fact_type=citation.fact_type,
|
||||||
|
raw_citation_text=citation.raw_citation_text,
|
||||||
|
source_kind=GenealogyCitationSourceKind.FAMILYSEARCH_IMPORTED,
|
||||||
|
document_id=None,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return rows
|
||||||
|
|
||||||
|
|
||||||
|
async def _replace_imported_citations(
|
||||||
|
*,
|
||||||
|
session: AsyncSession,
|
||||||
|
person_ids: set[UUID],
|
||||||
|
family_ids: set[UUID],
|
||||||
|
citations: list[GenealogyCitation],
|
||||||
|
) -> None:
|
||||||
|
where_clauses = []
|
||||||
|
if person_ids:
|
||||||
|
where_clauses.append(col(GenealogyCitation.genealogy_person_id).in_(person_ids))
|
||||||
|
if family_ids:
|
||||||
|
where_clauses.append(col(GenealogyCitation.genealogy_family_id).in_(family_ids))
|
||||||
|
if where_clauses:
|
||||||
|
target_scope = where_clauses[0] if len(where_clauses) == 1 else or_(*where_clauses)
|
||||||
|
await session.exec(
|
||||||
|
delete(GenealogyCitation).where(
|
||||||
|
col(GenealogyCitation.source_kind) == GenealogyCitationSourceKind.FAMILYSEARCH_IMPORTED,
|
||||||
|
target_scope,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
for citation in citations:
|
||||||
|
session.add(citation)
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_person(element: Element) -> ParsedPerson:
|
||||||
|
birth_event = _first_child(element, "BIRT")
|
||||||
|
death_event = _first_child(element, "DEAT")
|
||||||
|
birth_date_raw = _child_value(birth_event, "DATE")
|
||||||
|
death_date_raw = _child_value(death_event, "DATE")
|
||||||
|
return ParsedPerson(
|
||||||
|
pointer=element.get_pointer() or "",
|
||||||
|
fs_id=_extract_fs_identifier(element),
|
||||||
|
full_name=_person_name(element),
|
||||||
|
birth_date=_parse_exact_date(birth_date_raw),
|
||||||
|
birth_date_raw=birth_date_raw,
|
||||||
|
birth_place=_child_value(birth_event, "PLAC"),
|
||||||
|
death_date=_parse_exact_date(death_date_raw),
|
||||||
|
death_date_raw=death_date_raw,
|
||||||
|
death_place=_child_value(death_event, "PLAC"),
|
||||||
|
citations=(
|
||||||
|
*_fact_citations(fact_element=birth_event, fact_type=GenealogyCitationFactType.BIRTH),
|
||||||
|
*_fact_citations(fact_element=death_event, fact_type=GenealogyCitationFactType.DEATH),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_family(element: Element) -> ParsedFamily:
|
||||||
|
marriage_event = _first_child(element, "MARR")
|
||||||
|
marriage_date_raw = _child_value(marriage_event, "DATE")
|
||||||
|
children = tuple(
|
||||||
|
ParsedFamilyChild(
|
||||||
|
child_pointer=(child.get_value() or "").strip(),
|
||||||
|
relationship_type=_child_value(child, "PEDI"),
|
||||||
|
)
|
||||||
|
for child in _children(element, "CHIL")
|
||||||
|
if (child.get_value() or "").strip()
|
||||||
|
)
|
||||||
|
return ParsedFamily(
|
||||||
|
fs_family_id=_extract_fs_identifier(element),
|
||||||
|
husband_pointer=(_child_value(element, "HUSB") or "").strip() or None,
|
||||||
|
wife_pointer=(_child_value(element, "WIFE") or "").strip() or None,
|
||||||
|
marriage_date=_parse_exact_date(marriage_date_raw),
|
||||||
|
marriage_date_raw=marriage_date_raw,
|
||||||
|
marriage_place=_child_value(marriage_event, "PLAC"),
|
||||||
|
children=children,
|
||||||
|
citations=_fact_citations(fact_element=marriage_event, fact_type=GenealogyCitationFactType.MARRIAGE),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _fact_citations(
|
||||||
|
*, fact_element: Element | None, fact_type: GenealogyCitationFactType
|
||||||
|
) -> tuple[ParsedCitation, ...]:
|
||||||
|
if fact_element is None:
|
||||||
|
return ()
|
||||||
|
citations: list[ParsedCitation] = []
|
||||||
|
for source in _children(fact_element, "SOUR"):
|
||||||
|
raw_citation = _flatten_tag_values(source)
|
||||||
|
if raw_citation is None:
|
||||||
|
continue
|
||||||
|
citations.append(ParsedCitation(fact_type=fact_type, raw_citation_text=raw_citation))
|
||||||
|
return tuple(citations)
|
||||||
|
|
||||||
|
|
||||||
|
def _extract_fs_identifier(element: Element) -> str | None:
|
||||||
|
for tag in ("_FSFTID", "FSFTID"):
|
||||||
|
value = _child_value(element, tag)
|
||||||
|
if value:
|
||||||
|
return value
|
||||||
|
for refn in _children(element, "REFN"):
|
||||||
|
refn_value = (refn.get_value() or "").strip()
|
||||||
|
refn_type = (_child_value(refn, "TYPE") or "").strip().casefold()
|
||||||
|
if refn_value and ("fsftid" in refn_type or "familysearch" in refn_type):
|
||||||
|
return refn_value
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _person_name(element: Element) -> str:
|
||||||
|
raw_name = _child_value(element, "NAME")
|
||||||
|
if raw_name is None:
|
||||||
|
return "Unknown"
|
||||||
|
cleaned = raw_name.replace("/", " ").strip()
|
||||||
|
return " ".join(part for part in cleaned.split() if part) or "Unknown"
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_exact_date(raw: str | None) -> date | None:
|
||||||
|
if raw is None:
|
||||||
|
return None
|
||||||
|
tokens = [token for token in raw.strip().upper().split() if token]
|
||||||
|
if len(tokens) != 3:
|
||||||
|
return None
|
||||||
|
day_token, month_token, year_token = tokens
|
||||||
|
if month_token not in _MONTHS:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return date(year=int(year_token), month=_MONTHS[month_token], day=int(day_token))
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _first_child(element: Element | None, tag: str) -> Element | None:
|
||||||
|
if element is None:
|
||||||
|
return None
|
||||||
|
for child in element.get_child_elements():
|
||||||
|
if child.get_tag() == tag:
|
||||||
|
return child
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _children(element: Element | None, tag: str) -> list[Element]:
|
||||||
|
if element is None:
|
||||||
|
return []
|
||||||
|
return [child for child in element.get_child_elements() if child.get_tag() == tag]
|
||||||
|
|
||||||
|
|
||||||
|
def _child_value(element: Element | None, tag: str) -> str | None:
|
||||||
|
child = _first_child(element, tag)
|
||||||
|
if child is None:
|
||||||
|
return None
|
||||||
|
value = (child.get_value() or "").strip()
|
||||||
|
return value or None
|
||||||
|
|
||||||
|
|
||||||
|
def _flatten_tag_values(element: Element) -> str | None:
|
||||||
|
lines: list[str] = []
|
||||||
|
|
||||||
|
def walk(node: Element) -> None:
|
||||||
|
value = (node.get_value() or "").strip()
|
||||||
|
if value:
|
||||||
|
lines.append(f"{node.get_tag()}: {value}")
|
||||||
|
for child in node.get_child_elements():
|
||||||
|
walk(child)
|
||||||
|
|
||||||
|
walk(element)
|
||||||
|
return " | ".join(lines) if lines else None
|
||||||
@@ -2,20 +2,22 @@ import logging
|
|||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from datetime import UTC
|
from datetime import UTC
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
from sqlalchemy.orm import selectinload
|
from sqlalchemy import update
|
||||||
|
from sqlmodel import col
|
||||||
from sqlmodel import select
|
from sqlmodel import select
|
||||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||||
|
|
||||||
|
from ..db.loading import orm_attribute
|
||||||
|
from ..db.loading import selectinload
|
||||||
|
from ..db.models import Document
|
||||||
from ..db.models import ExecutionAttempt
|
from ..db.models import ExecutionAttempt
|
||||||
from ..db.models import Job
|
from ..db.models import Job
|
||||||
from ..db.models import JobSource
|
from ..db.models import JobSource
|
||||||
from ..db.models import JobSourceStatus
|
from ..db.models import JobSourceStatus
|
||||||
from ..db.models import JobStatus
|
from ..db.models import JobStatus
|
||||||
from ..db.models import ProcessingArtifact
|
|
||||||
from ..db.models import Source
|
from ..db.models import Source
|
||||||
from ..errors import AppError
|
from ..errors import AppError
|
||||||
from ..errors import ErrorCategory
|
from ..errors import ErrorCategory
|
||||||
@@ -24,6 +26,18 @@ from .base import ServiceBase
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _utc_now_naive() -> datetime:
|
||||||
|
"""Return current UTC as naive datetime for DB timestamp columns."""
|
||||||
|
return datetime.now(UTC).replace(tzinfo=None)
|
||||||
|
|
||||||
|
|
||||||
|
def _as_naive_utc(value: datetime) -> datetime:
|
||||||
|
"""Normalize datetimes to naive UTC for DB comparisons/binds."""
|
||||||
|
if value.tzinfo is None:
|
||||||
|
return value
|
||||||
|
return value.astimezone(UTC).replace(tzinfo=None)
|
||||||
|
|
||||||
|
|
||||||
class JobDeleteBlockedError(AppError):
|
class JobDeleteBlockedError(AppError):
|
||||||
"""Raised when a job delete operation is blocked by lifecycle policy."""
|
"""Raised when a job delete operation is blocked by lifecycle policy."""
|
||||||
|
|
||||||
@@ -64,8 +78,8 @@ class JobService(ServiceBase):
|
|||||||
query = (
|
query = (
|
||||||
select(Job)
|
select(Job)
|
||||||
.options(
|
.options(
|
||||||
selectinload(Job.document), # pyright: ignore[reportArgumentType]
|
selectinload(Job.document).selectinload(orm_attribute(Document.sources)),
|
||||||
selectinload(Job.job_sources).selectinload(JobSource.source), # pyright: ignore[reportArgumentType]
|
selectinload(Job.job_sources).selectinload(orm_attribute(JobSource.source)),
|
||||||
)
|
)
|
||||||
.where(Job.id == job_id)
|
.where(Job.id == job_id)
|
||||||
.execution_options(populate_existing=True)
|
.execution_options(populate_existing=True)
|
||||||
@@ -100,28 +114,28 @@ class JobService(ServiceBase):
|
|||||||
"""Query jobs from the database based on provided filters."""
|
"""Query jobs from the database based on provided filters."""
|
||||||
async with self._session_scope(session) as _session:
|
async with self._session_scope(session) as _session:
|
||||||
query = select(Job).options(
|
query = select(Job).options(
|
||||||
selectinload(Job.document), # pyright: ignore[reportArgumentType]
|
selectinload(Job.document),
|
||||||
selectinload(Job.job_sources).selectinload(JobSource.source), # pyright: ignore[reportArgumentType]
|
selectinload(Job.job_sources).selectinload(orm_attribute(JobSource.source)),
|
||||||
)
|
)
|
||||||
if status is not None:
|
if status is not None:
|
||||||
query = query.where(Job.status == status)
|
query = query.where(Job.status == status)
|
||||||
if filename is not None:
|
if filename is not None:
|
||||||
query = query.where(Job.job_sources.any(JobSource.source.has(Source.filename == filename)))
|
query = query.where(
|
||||||
|
col(Job.job_sources).any(col(JobSource.source).has(col(Source.filename) == filename))
|
||||||
|
)
|
||||||
result = await _session.exec(query)
|
result = await _session.exec(query)
|
||||||
return result.all()
|
return result.all()
|
||||||
|
|
||||||
async def list_jobs(
|
async def list_jobs(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
load_docs: bool = False,
|
|
||||||
session: AsyncSession | None = None,
|
session: AsyncSession | None = None,
|
||||||
) -> Sequence[Job]:
|
) -> Sequence[Job]:
|
||||||
"""List all jobs in the database with eagerly loaded documents."""
|
"""List all jobs in the database with eagerly loaded documents."""
|
||||||
_ = load_docs
|
|
||||||
async with self._session_scope(session) as _session:
|
async with self._session_scope(session) as _session:
|
||||||
query = select(Job).options(
|
query = select(Job).options(
|
||||||
selectinload(Job.document), # pyright: ignore[reportArgumentType]
|
selectinload(Job.document),
|
||||||
selectinload(Job.job_sources).selectinload(JobSource.source), # pyright: ignore[reportArgumentType]
|
selectinload(Job.job_sources).selectinload(orm_attribute(JobSource.source)),
|
||||||
)
|
)
|
||||||
result = await _session.exec(query)
|
result = await _session.exec(query)
|
||||||
return result.all()
|
return result.all()
|
||||||
@@ -153,7 +167,10 @@ class JobService(ServiceBase):
|
|||||||
async with self._session_scope(session) as _session:
|
async with self._session_scope(session) as _session:
|
||||||
query = (
|
query = (
|
||||||
select(Job)
|
select(Job)
|
||||||
.options(selectinload(Job.document)) # pyright: ignore[reportArgumentType]
|
.options(
|
||||||
|
selectinload(Job.document),
|
||||||
|
selectinload(Job.job_sources).selectinload(orm_attribute(JobSource.source)),
|
||||||
|
)
|
||||||
.where(Job.id == job_id)
|
.where(Job.id == job_id)
|
||||||
.execution_options(populate_existing=True)
|
.execution_options(populate_existing=True)
|
||||||
)
|
)
|
||||||
@@ -163,28 +180,74 @@ class JobService(ServiceBase):
|
|||||||
job.status = status
|
job.status = status
|
||||||
if retry_count_increment:
|
if retry_count_increment:
|
||||||
job.retry_count += retry_count_increment
|
job.retry_count += retry_count_increment
|
||||||
job.date_updated = datetime.now(UTC)
|
|
||||||
await self._finalize(session=_session, caller_session=session, refresh=(job,))
|
await self._finalize(session=_session, caller_session=session, refresh=(job,))
|
||||||
return job
|
return job
|
||||||
|
|
||||||
async def read_next_queued_job(
|
async def note_processing_progress(self, *, job_id: UUID, session: AsyncSession | None = None) -> Job:
|
||||||
|
"""Refresh job liveness while a multi-page batch is still in progress."""
|
||||||
|
async with self._session_scope(session) as _session:
|
||||||
|
job = await _session.get(Job, job_id)
|
||||||
|
if job is None:
|
||||||
|
raise self._not_found(job_id)
|
||||||
|
job.date_updated = _utc_now_naive()
|
||||||
|
await self._finalize(session=_session, caller_session=session, refresh=(job,))
|
||||||
|
return job
|
||||||
|
|
||||||
|
async def claim_next_queued_job(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
session: AsyncSession | None = None,
|
session: AsyncSession | None = None,
|
||||||
) -> Job | None:
|
) -> Job | None:
|
||||||
"""Read the next queued job ordered by creation time."""
|
"""Atomically claim the oldest queued job by transitioning it to PROCESSING.
|
||||||
|
|
||||||
|
The selection is deliberately unadorned: no eager loads are applied to the
|
||||||
|
hot poll, because callers re-read the claimed job with the relationships
|
||||||
|
they actually need. On PostgreSQL the row is locked with ``SKIP LOCKED`` so
|
||||||
|
concurrent workers never contend for the same job.
|
||||||
|
"""
|
||||||
async with self._session_scope(session) as _session:
|
async with self._session_scope(session) as _session:
|
||||||
|
dialect = _session.get_bind().dialect.name
|
||||||
|
if dialect == "postgresql":
|
||||||
query = (
|
query = (
|
||||||
select(Job)
|
select(Job)
|
||||||
.options(
|
|
||||||
selectinload(Job.document), # pyright: ignore[reportArgumentType]
|
|
||||||
selectinload(Job.job_sources).selectinload(JobSource.source), # pyright: ignore[reportArgumentType]
|
|
||||||
)
|
|
||||||
.where(Job.status == JobStatus.QUEUED)
|
.where(Job.status == JobStatus.QUEUED)
|
||||||
# Break ties by id so "next" is stable when two rows share close timestamps.
|
# Break ties by id so "next" is stable when two rows share close timestamps.
|
||||||
.order_by(Job.date_created, Job.id) # pyright: ignore[reportArgumentType]
|
.order_by(col(Job.date_created), col(Job.id))
|
||||||
|
.limit(1)
|
||||||
|
.with_for_update(skip_locked=True)
|
||||||
)
|
)
|
||||||
return (await _session.exec(query)).first()
|
job = (await _session.exec(query)).first()
|
||||||
|
if job is None:
|
||||||
|
return None
|
||||||
|
job.status = JobStatus.PROCESSING
|
||||||
|
await self._finalize(session=_session, caller_session=session, refresh=(job,))
|
||||||
|
return job
|
||||||
|
|
||||||
|
now = _utc_now_naive()
|
||||||
|
queued_job_id = (
|
||||||
|
select(col(Job.id))
|
||||||
|
.where(col(Job.status) == JobStatus.QUEUED)
|
||||||
|
.order_by(col(Job.date_created), col(Job.id))
|
||||||
|
.limit(1)
|
||||||
|
.scalar_subquery()
|
||||||
|
)
|
||||||
|
claim_statement = (
|
||||||
|
update(Job)
|
||||||
|
.where(col(Job.id) == queued_job_id)
|
||||||
|
.where(col(Job.status) == JobStatus.QUEUED)
|
||||||
|
.values(status=JobStatus.PROCESSING, date_updated=now)
|
||||||
|
.returning(col(Job.id))
|
||||||
|
)
|
||||||
|
claimed_row = (await _session.exec(claim_statement)).first()
|
||||||
|
if claimed_row is None:
|
||||||
|
return None
|
||||||
|
claimed_job_id = claimed_row if isinstance(claimed_row, UUID) else claimed_row[0]
|
||||||
|
|
||||||
|
job = (await _session.exec(select(Job).where(Job.id == claimed_job_id))).first()
|
||||||
|
if job is None:
|
||||||
|
return None
|
||||||
|
await self._finalize(session=_session, caller_session=session, refresh=(job,))
|
||||||
|
return job
|
||||||
|
|
||||||
async def requeue_stale_processing_jobs(
|
async def requeue_stale_processing_jobs(
|
||||||
self,
|
self,
|
||||||
@@ -198,12 +261,15 @@ class JobService(ServiceBase):
|
|||||||
``stale_before`` are considered stale and re-queued.
|
``stale_before`` are considered stale and re-queued.
|
||||||
"""
|
"""
|
||||||
async with self._session_scope(session) as _session:
|
async with self._session_scope(session) as _session:
|
||||||
query = select(Job).where(Job.status == JobStatus.PROCESSING).where(Job.date_updated < stale_before)
|
normalized_stale_before = _as_naive_utc(stale_before)
|
||||||
|
query = (
|
||||||
|
select(Job).where(Job.status == JobStatus.PROCESSING).where(Job.date_updated < normalized_stale_before)
|
||||||
|
)
|
||||||
stale_jobs = (await _session.exec(query)).all()
|
stale_jobs = (await _session.exec(query)).all()
|
||||||
if not stale_jobs:
|
if not stale_jobs:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
now = datetime.now(UTC)
|
now = _utc_now_naive()
|
||||||
for job in stale_jobs:
|
for job in stale_jobs:
|
||||||
job.status = JobStatus.QUEUED
|
job.status = JobStatus.QUEUED
|
||||||
job.date_updated = now
|
job.date_updated = now
|
||||||
@@ -237,9 +303,7 @@ class JobService(ServiceBase):
|
|||||||
)
|
)
|
||||||
attempt_count = (
|
attempt_count = (
|
||||||
await _session.exec(
|
await _session.exec(
|
||||||
select(func.count())
|
select(func.count()).select_from(ExecutionAttempt).where(ExecutionAttempt.job_id == job_id)
|
||||||
.select_from(ExecutionAttempt)
|
|
||||||
.where(ExecutionAttempt.job_id == job_id)
|
|
||||||
)
|
)
|
||||||
).one()
|
).one()
|
||||||
if attempt_count:
|
if attempt_count:
|
||||||
@@ -260,7 +324,6 @@ class JobService(ServiceBase):
|
|||||||
|
|
||||||
async def delete_job_and_evidence(self, *, job_id: UUID) -> None:
|
async def delete_job_and_evidence(self, *, job_id: UUID) -> None:
|
||||||
"""Explicitly delete a terminal job and all evidence owned by its attempts."""
|
"""Explicitly delete a terminal job and all evidence owned by its attempts."""
|
||||||
external_references: list[str] = []
|
|
||||||
async with self._session_scope() as session:
|
async with self._session_scope() as session:
|
||||||
job = (
|
job = (
|
||||||
await session.exec(
|
await session.exec(
|
||||||
@@ -280,31 +343,8 @@ class JobService(ServiceBase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
attempts = list(
|
attempts = list(
|
||||||
(
|
(await session.exec(select(ExecutionAttempt).where(ExecutionAttempt.job_id == job_id))).all()
|
||||||
await session.exec(
|
|
||||||
select(ExecutionAttempt).where(ExecutionAttempt.job_id == job_id)
|
|
||||||
)
|
)
|
||||||
).all()
|
|
||||||
)
|
|
||||||
if attempts:
|
|
||||||
attempt_ids = [attempt.id for attempt in attempts]
|
|
||||||
artifacts = list(
|
|
||||||
(
|
|
||||||
await session.exec(
|
|
||||||
select(ProcessingArtifact).where(
|
|
||||||
ProcessingArtifact.execution_attempt_id.in_(attempt_ids)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
).all()
|
|
||||||
)
|
|
||||||
external_references = [
|
|
||||||
artifact.external_reference
|
|
||||||
for artifact in artifacts
|
|
||||||
if artifact.external_reference is not None
|
|
||||||
]
|
|
||||||
for artifact in artifacts:
|
|
||||||
await session.delete(artifact)
|
|
||||||
await session.flush()
|
|
||||||
for attempt in attempts:
|
for attempt in attempts:
|
||||||
await session.delete(attempt)
|
await session.delete(attempt)
|
||||||
await session.flush()
|
await session.flush()
|
||||||
@@ -315,31 +355,13 @@ class JobService(ServiceBase):
|
|||||||
await session.delete(job)
|
await session.delete(job)
|
||||||
await self._finalize(session=session, caller_session=None)
|
await self._finalize(session=session, caller_session=None)
|
||||||
|
|
||||||
for external_reference in external_references:
|
|
||||||
self._delete_external_artifact(external_reference)
|
|
||||||
|
|
||||||
def _delete_external_artifact(self, external_reference: str) -> None:
|
|
||||||
relative_path = Path(external_reference)
|
|
||||||
if relative_path.is_absolute() or ".." in relative_path.parts:
|
|
||||||
logger.warning("Skipped unsafe external artifact reference during job deletion: %s", external_reference)
|
|
||||||
return
|
|
||||||
artifact_root = self.settings.artifact_dir.resolve()
|
|
||||||
artifact_path = (artifact_root / relative_path).resolve()
|
|
||||||
if artifact_root not in artifact_path.parents:
|
|
||||||
logger.warning("Skipped external artifact outside configured root: %s", external_reference)
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
artifact_path.unlink(missing_ok=True)
|
|
||||||
except OSError:
|
|
||||||
logger.warning("Failed to delete external artifact: %s", artifact_path)
|
|
||||||
|
|
||||||
async def cancel_job(self, *, job_id: UUID, session: AsyncSession | None = None) -> Job:
|
async def cancel_job(self, *, job_id: UUID, session: AsyncSession | None = None) -> Job:
|
||||||
"""Cancel a queued/processing job and stop remaining source work."""
|
"""Cancel a queued/processing job and stop remaining source work."""
|
||||||
async with self._session_scope(session) as _session:
|
async with self._session_scope(session) as _session:
|
||||||
query = (
|
query = (
|
||||||
select(Job)
|
select(Job)
|
||||||
.options(
|
.options(
|
||||||
selectinload(Job.job_sources).selectinload(JobSource.source), # pyright: ignore[reportArgumentType]
|
selectinload(Job.job_sources).selectinload(orm_attribute(JobSource.source)),
|
||||||
)
|
)
|
||||||
.where(Job.id == job_id)
|
.where(Job.id == job_id)
|
||||||
.execution_options(populate_existing=True)
|
.execution_options(populate_existing=True)
|
||||||
@@ -348,24 +370,21 @@ class JobService(ServiceBase):
|
|||||||
if job is None:
|
if job is None:
|
||||||
raise self._not_found(job_id)
|
raise self._not_found(job_id)
|
||||||
|
|
||||||
if job.status in {JobStatus.TRANSCRIBED, JobStatus.COMPLETED}:
|
if job.status == JobStatus.TRANSCRIBED:
|
||||||
raise JobCancelBlockedError(
|
raise JobCancelBlockedError(
|
||||||
"Job cancel is not allowed for transcribed/completed jobs",
|
"Job cancel is not allowed for transcribed jobs",
|
||||||
category=ErrorCategory.VALIDATION,
|
category=ErrorCategory.VALIDATION,
|
||||||
suggestion="Use resubmit for reprocessing needs, or leave the terminal job unchanged.",
|
suggestion="Use resubmit for reprocessing needs, or leave the terminal job unchanged.",
|
||||||
)
|
)
|
||||||
|
|
||||||
now = datetime.now(UTC)
|
now = _utc_now_naive()
|
||||||
job.status = JobStatus.FAILED
|
job.status = JobStatus.FAILED
|
||||||
job.date_updated = now
|
job.date_updated = now
|
||||||
|
|
||||||
for job_source in job.job_sources:
|
for job_source in job.job_sources:
|
||||||
if job_source.status == JobSourceStatus.TRANSCRIBED:
|
if job_source.status == JobSourceStatus.TRANSCRIBED:
|
||||||
continue
|
continue
|
||||||
job_source.status = JobSourceStatus.FAILED
|
job_source.status = JobSourceStatus.CANCELLED
|
||||||
job_source.raw_transcription = None
|
|
||||||
job_source.error_detail = "Cancelled by user"
|
|
||||||
job_source.executed_at = now
|
|
||||||
|
|
||||||
await self._finalize(session=_session, caller_session=session, refresh=(job,))
|
await self._finalize(session=_session, caller_session=session, refresh=(job,))
|
||||||
return job
|
return job
|
||||||
@@ -376,7 +395,7 @@ class JobService(ServiceBase):
|
|||||||
query = (
|
query = (
|
||||||
select(Job)
|
select(Job)
|
||||||
.options(
|
.options(
|
||||||
selectinload(Job.job_sources).selectinload(JobSource.source), # pyright: ignore[reportArgumentType]
|
selectinload(Job.job_sources).selectinload(orm_attribute(JobSource.source)),
|
||||||
)
|
)
|
||||||
.where(Job.id == job_id)
|
.where(Job.id == job_id)
|
||||||
.execution_options(populate_existing=True)
|
.execution_options(populate_existing=True)
|
||||||
@@ -392,20 +411,21 @@ class JobService(ServiceBase):
|
|||||||
suggestion="Cancel processing first, then resubmit remaining sources.",
|
suggestion="Cancel processing first, then resubmit remaining sources.",
|
||||||
)
|
)
|
||||||
|
|
||||||
candidates = [job_source for job_source in job.job_sources if job_source.status == JobSourceStatus.FAILED]
|
# Cancelled pages are re-attemptable: older flows wrote FAILED,
|
||||||
|
# so resubmit already reset them. Excluding CANCELLED here would make
|
||||||
|
# cancelled work permanently unrecoverable.
|
||||||
|
resubmittable = {JobSourceStatus.FAILED, JobSourceStatus.CANCELLED}
|
||||||
|
candidates = [job_source for job_source in job.job_sources if job_source.status in resubmittable]
|
||||||
if not candidates:
|
if not candidates:
|
||||||
raise JobResubmitBlockedError(
|
raise JobResubmitBlockedError(
|
||||||
"Job has no failed sources to resubmit",
|
"Job has no failed or cancelled sources to resubmit",
|
||||||
category=ErrorCategory.VALIDATION,
|
category=ErrorCategory.VALIDATION,
|
||||||
suggestion="Only failed sources can be resubmitted.",
|
suggestion="Only failed or cancelled sources can be resubmitted.",
|
||||||
)
|
)
|
||||||
|
|
||||||
now = datetime.now(UTC)
|
now = _utc_now_naive()
|
||||||
for job_source in candidates:
|
for job_source in candidates:
|
||||||
job_source.status = JobSourceStatus.PENDING
|
job_source.status = JobSourceStatus.PENDING
|
||||||
job_source.raw_transcription = None
|
|
||||||
job_source.error_detail = None
|
|
||||||
job_source.executed_at = now
|
|
||||||
|
|
||||||
job.status = JobStatus.QUEUED
|
job.status = JobStatus.QUEUED
|
||||||
job.date_updated = now
|
job.date_updated = now
|
||||||
|
|||||||
@@ -0,0 +1,444 @@
|
|||||||
|
"""Queue-backed maintenance operations executed by the worker loop."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from datetime import UTC
|
||||||
|
from datetime import datetime
|
||||||
|
from pathlib import Path
|
||||||
|
from uuid import UUID
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from sqlalchemy import update
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from sqlmodel import col
|
||||||
|
from sqlmodel import func
|
||||||
|
from sqlmodel import select
|
||||||
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||||
|
|
||||||
|
from transcription.db.models import Document
|
||||||
|
from transcription.db.models import MaintenanceJobType
|
||||||
|
from transcription.db.models import MaintenanceRun
|
||||||
|
from transcription.db.models import MaintenanceRunStatus
|
||||||
|
from transcription.db.models import Source
|
||||||
|
from transcription.errors import AppError
|
||||||
|
from transcription.errors import ErrorCategory
|
||||||
|
from transcription.errors import classify_unexpected_error
|
||||||
|
|
||||||
|
from .base import ServiceBase
|
||||||
|
from .gedcom_import import GedcomImportError
|
||||||
|
from .gedcom_import import import_gedcom_file
|
||||||
|
from .media_storage import persist_named_media
|
||||||
|
|
||||||
|
|
||||||
|
def _utc_now_naive() -> datetime:
|
||||||
|
return datetime.now(UTC).replace(tzinfo=None)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class MaintenanceExecution:
|
||||||
|
"""In-memory result for one executed maintenance run."""
|
||||||
|
|
||||||
|
status: MaintenanceRunStatus
|
||||||
|
summary: str
|
||||||
|
output: str
|
||||||
|
error_detail: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class MaintenanceError(AppError):
|
||||||
|
"""Raised when maintenance operations cannot be enqueued or executed."""
|
||||||
|
|
||||||
|
|
||||||
|
class MaintenanceService(ServiceBase):
|
||||||
|
"""Persist and execute background maintenance runs."""
|
||||||
|
|
||||||
|
async def store_gedcom_upload(self, *, filename: str, file_bytes: bytes) -> str:
|
||||||
|
if not file_bytes:
|
||||||
|
raise MaintenanceError(
|
||||||
|
"GEDCOM upload is empty.",
|
||||||
|
category=ErrorCategory.VALIDATION,
|
||||||
|
suggestion="Upload a non-empty .ged file and retry.",
|
||||||
|
)
|
||||||
|
if Path(filename).suffix.casefold() != ".ged":
|
||||||
|
raise MaintenanceError(
|
||||||
|
"Unsupported GEDCOM upload format.",
|
||||||
|
category=ErrorCategory.USER_INPUT,
|
||||||
|
suggestion="Upload a file with a .ged extension.",
|
||||||
|
)
|
||||||
|
stored_path = await persist_named_media(
|
||||||
|
root=self.settings.upload_dir,
|
||||||
|
namespace=Path("genealogy"),
|
||||||
|
filename=filename,
|
||||||
|
file_bytes=file_bytes,
|
||||||
|
filename_stem=str(uuid4()),
|
||||||
|
error=MaintenanceError,
|
||||||
|
failure_message="GEDCOM file could not be persisted.",
|
||||||
|
failure_suggestion="Check upload directory permissions and retry.",
|
||||||
|
log_label="gedcom file",
|
||||||
|
)
|
||||||
|
return str(stored_path.resolve().relative_to(self.settings.upload_dir.resolve()).as_posix())
|
||||||
|
|
||||||
|
def latest_gedcom_upload_path(self) -> str | None:
|
||||||
|
latest = self._latest_gedcom_upload()
|
||||||
|
if latest is None:
|
||||||
|
return None
|
||||||
|
return str(latest.resolve().relative_to(self.settings.upload_dir.resolve()).as_posix())
|
||||||
|
|
||||||
|
async def list_runs(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
limit: int = 100,
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> list[MaintenanceRun]:
|
||||||
|
try:
|
||||||
|
async with self._session_scope(session) as _session:
|
||||||
|
query = (
|
||||||
|
select(MaintenanceRun)
|
||||||
|
.order_by(col(MaintenanceRun.created_at).desc(), col(MaintenanceRun.id).desc())
|
||||||
|
.limit(limit)
|
||||||
|
)
|
||||||
|
return list((await _session.exec(query)).all())
|
||||||
|
except SQLAlchemyError as exc:
|
||||||
|
raise MaintenanceError(
|
||||||
|
"Maintenance runs are unavailable.",
|
||||||
|
category=ErrorCategory.INFRA_PERSISTENT,
|
||||||
|
suggestion="Verify database schema access and retry.",
|
||||||
|
detail=f"Failed to list maintenance runs: {type(exc).__name__}: {exc}",
|
||||||
|
) from exc
|
||||||
|
|
||||||
|
async def enqueue_run(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
job_type: MaintenanceJobType,
|
||||||
|
triggered_by: str = "ui.settings",
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> MaintenanceRun:
|
||||||
|
run = MaintenanceRun(
|
||||||
|
job_type=job_type,
|
||||||
|
status=MaintenanceRunStatus.QUEUED,
|
||||||
|
triggered_by=triggered_by,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
async with self._session_scope(session) as _session:
|
||||||
|
_session.add(run)
|
||||||
|
await self._finalize(session=_session, caller_session=session, refresh=(run,))
|
||||||
|
except SQLAlchemyError as exc:
|
||||||
|
raise MaintenanceError(
|
||||||
|
"Maintenance run could not be queued.",
|
||||||
|
category=ErrorCategory.INFRA_PERSISTENT,
|
||||||
|
suggestion="Run the V6.1 schema migration, then retry.",
|
||||||
|
detail=f"Failed to enqueue maintenance run: {type(exc).__name__}: {exc}",
|
||||||
|
) from exc
|
||||||
|
return run
|
||||||
|
|
||||||
|
async def claim_next_queued_run(self, *, session: AsyncSession | None = None) -> MaintenanceRun | None:
|
||||||
|
try:
|
||||||
|
async with self._session_scope(session) as _session:
|
||||||
|
now = _utc_now_naive()
|
||||||
|
queued_run_id = (
|
||||||
|
select(col(MaintenanceRun.id))
|
||||||
|
.where(col(MaintenanceRun.status) == MaintenanceRunStatus.QUEUED)
|
||||||
|
.order_by(col(MaintenanceRun.created_at), col(MaintenanceRun.id))
|
||||||
|
.limit(1)
|
||||||
|
.scalar_subquery()
|
||||||
|
)
|
||||||
|
claim_statement = (
|
||||||
|
update(MaintenanceRun)
|
||||||
|
.where(col(MaintenanceRun.id) == queued_run_id)
|
||||||
|
.where(col(MaintenanceRun.status) == MaintenanceRunStatus.QUEUED)
|
||||||
|
.values(
|
||||||
|
status=MaintenanceRunStatus.PROCESSING,
|
||||||
|
started_at=now,
|
||||||
|
updated_at=now,
|
||||||
|
)
|
||||||
|
.returning(col(MaintenanceRun.id))
|
||||||
|
)
|
||||||
|
claimed_row = (await _session.exec(claim_statement)).first()
|
||||||
|
if claimed_row is None:
|
||||||
|
return None
|
||||||
|
claimed_run_id = claimed_row if isinstance(claimed_row, UUID) else claimed_row[0]
|
||||||
|
run = await _session.get(MaintenanceRun, claimed_run_id)
|
||||||
|
if run is None:
|
||||||
|
return None
|
||||||
|
await self._finalize(session=_session, caller_session=session, refresh=(run,))
|
||||||
|
return run
|
||||||
|
except SQLAlchemyError as exc:
|
||||||
|
raise MaintenanceError(
|
||||||
|
"Maintenance queue claim failed.",
|
||||||
|
category=ErrorCategory.INFRA_PERSISTENT,
|
||||||
|
suggestion="Verify database schema access and retry.",
|
||||||
|
detail=f"Failed to claim queued maintenance run: {type(exc).__name__}: {exc}",
|
||||||
|
) from exc
|
||||||
|
|
||||||
|
async def process_next_queued_run(self, *, session: AsyncSession | None = None) -> bool:
|
||||||
|
run = await self.claim_next_queued_run(session=session)
|
||||||
|
if run is None:
|
||||||
|
return False
|
||||||
|
|
||||||
|
execution = await self._execute_run(run)
|
||||||
|
await self._finalize_run(run_id=run.id, execution=execution, session=session)
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def _finalize_run(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
run_id: UUID,
|
||||||
|
execution: MaintenanceExecution,
|
||||||
|
session: AsyncSession | None = None,
|
||||||
|
) -> None:
|
||||||
|
now = _utc_now_naive()
|
||||||
|
log_path = self._write_log(run_id=run_id, output=execution.output)
|
||||||
|
async with self._session_scope(session) as _session:
|
||||||
|
run = await _session.get(MaintenanceRun, run_id)
|
||||||
|
if run is None:
|
||||||
|
raise MaintenanceError(
|
||||||
|
"Maintenance run not found while finalizing.",
|
||||||
|
category=ErrorCategory.NOT_FOUND,
|
||||||
|
suggestion="Refresh the page and retry.",
|
||||||
|
)
|
||||||
|
run.status = execution.status
|
||||||
|
run.summary = execution.summary
|
||||||
|
run.error_detail = execution.error_detail
|
||||||
|
run.log_path = log_path
|
||||||
|
run.finished_at = now
|
||||||
|
run.updated_at = now
|
||||||
|
await self._finalize(session=_session, caller_session=session, refresh=(run,))
|
||||||
|
|
||||||
|
async def _execute_run(self, run: MaintenanceRun) -> MaintenanceExecution:
|
||||||
|
if run.job_type == MaintenanceJobType.BACKUP:
|
||||||
|
return await self._execute_backup()
|
||||||
|
if run.job_type == MaintenanceJobType.STORAGE_RECONCILIATION:
|
||||||
|
return await self._execute_storage_reconciliation()
|
||||||
|
if run.job_type == MaintenanceJobType.GEDCOM_IMPORT:
|
||||||
|
return await self._execute_gedcom_import()
|
||||||
|
raise MaintenanceError(
|
||||||
|
"Unsupported maintenance job type.",
|
||||||
|
category=ErrorCategory.VALIDATION,
|
||||||
|
suggestion="Choose a supported maintenance action and retry.",
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _execute_backup(self) -> MaintenanceExecution:
|
||||||
|
script_path = Path("deploy") / "backup" / "create_postgres_backup.sh"
|
||||||
|
if not script_path.is_file():
|
||||||
|
return MaintenanceExecution(
|
||||||
|
status=MaintenanceRunStatus.FAILED,
|
||||||
|
summary="Backup script is unavailable in this environment.",
|
||||||
|
output="Backup script not found.",
|
||||||
|
error_detail=f"Missing script: {script_path}",
|
||||||
|
)
|
||||||
|
|
||||||
|
command = ["sh", str(script_path)]
|
||||||
|
try:
|
||||||
|
process = await asyncio.create_subprocess_exec(
|
||||||
|
*command,
|
||||||
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
stderr=asyncio.subprocess.STDOUT,
|
||||||
|
)
|
||||||
|
stdout, _ = await process.communicate()
|
||||||
|
except OSError as exc:
|
||||||
|
return MaintenanceExecution(
|
||||||
|
status=MaintenanceRunStatus.FAILED,
|
||||||
|
summary="Backup command failed to start.",
|
||||||
|
output=f"Failed to execute {' '.join(command)}",
|
||||||
|
error_detail=f"{type(exc).__name__}: {exc}",
|
||||||
|
)
|
||||||
|
|
||||||
|
output = stdout.decode("utf-8", errors="replace")
|
||||||
|
if process.returncode == 0:
|
||||||
|
return MaintenanceExecution(
|
||||||
|
status=MaintenanceRunStatus.SUCCEEDED,
|
||||||
|
summary="Backup completed successfully.",
|
||||||
|
output=output,
|
||||||
|
)
|
||||||
|
return MaintenanceExecution(
|
||||||
|
status=MaintenanceRunStatus.FAILED,
|
||||||
|
summary="Backup command failed.",
|
||||||
|
output=output,
|
||||||
|
error_detail=f"Exit code: {process.returncode}",
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _execute_storage_reconciliation(self) -> MaintenanceExecution:
|
||||||
|
try:
|
||||||
|
mismatches = await self._collect_storage_mismatches()
|
||||||
|
except Exception as exc: # noqa: BLE001
|
||||||
|
error = classify_unexpected_error(exc, operation="maintenance.storage_reconciliation")
|
||||||
|
return MaintenanceExecution(
|
||||||
|
status=MaintenanceRunStatus.FAILED,
|
||||||
|
summary="Storage reconciliation failed.",
|
||||||
|
output="Storage reconciliation failed before completion.",
|
||||||
|
error_detail=error.detail,
|
||||||
|
)
|
||||||
|
|
||||||
|
if mismatches:
|
||||||
|
report = "\n".join(f"- {item}" for item in mismatches)
|
||||||
|
return MaintenanceExecution(
|
||||||
|
status=MaintenanceRunStatus.FAILED,
|
||||||
|
summary=f"Storage reconciliation found {len(mismatches)} issue(s).",
|
||||||
|
output=report,
|
||||||
|
error_detail="Reconciliation mismatches were detected.",
|
||||||
|
)
|
||||||
|
|
||||||
|
return MaintenanceExecution(
|
||||||
|
status=MaintenanceRunStatus.SUCCEEDED,
|
||||||
|
summary="Storage reconciliation found no mismatches.",
|
||||||
|
output="No storage reconciliation mismatches detected.",
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _execute_gedcom_import(self) -> MaintenanceExecution:
|
||||||
|
latest_upload = self._latest_gedcom_upload()
|
||||||
|
if latest_upload is None:
|
||||||
|
return MaintenanceExecution(
|
||||||
|
status=MaintenanceRunStatus.FAILED,
|
||||||
|
summary="No GEDCOM upload is available.",
|
||||||
|
output="No .ged file found under uploads/genealogy.",
|
||||||
|
error_detail="Missing GEDCOM upload in uploads/genealogy.",
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with self._session_scope() as session:
|
||||||
|
result = await import_gedcom_file(session=session, file_path=latest_upload)
|
||||||
|
except GedcomImportError as exc:
|
||||||
|
return MaintenanceExecution(
|
||||||
|
status=MaintenanceRunStatus.FAILED,
|
||||||
|
summary="GEDCOM import failed.",
|
||||||
|
output=f"GEDCOM import failed for {latest_upload.name}.",
|
||||||
|
error_detail=exc.detail,
|
||||||
|
)
|
||||||
|
except Exception as exc: # noqa: BLE001
|
||||||
|
error = classify_unexpected_error(exc, operation="maintenance.gedcom_import")
|
||||||
|
return MaintenanceExecution(
|
||||||
|
status=MaintenanceRunStatus.FAILED,
|
||||||
|
summary="GEDCOM import failed.",
|
||||||
|
output=f"GEDCOM import failed for {latest_upload.name}.",
|
||||||
|
error_detail=error.detail,
|
||||||
|
)
|
||||||
|
|
||||||
|
summary = (
|
||||||
|
f"Imported {result.new_people + result.updated_people} people "
|
||||||
|
f"({result.new_people} new, {result.updated_people} updated) and "
|
||||||
|
f"{result.new_families + result.updated_families} families "
|
||||||
|
f"({result.new_families} new, {result.updated_families} updated)."
|
||||||
|
)
|
||||||
|
output_lines = [
|
||||||
|
f"GEDCOM file: {latest_upload.as_posix()}",
|
||||||
|
summary,
|
||||||
|
f"Skipped people without FamilySearch ID: {result.skipped_people_without_fs_id}",
|
||||||
|
f"Skipped families without FamilySearch ID: {result.skipped_families_without_fs_id}",
|
||||||
|
f"Family child links written: {result.family_children}",
|
||||||
|
f"Imported citations: {result.citations}",
|
||||||
|
]
|
||||||
|
return MaintenanceExecution(
|
||||||
|
status=MaintenanceRunStatus.SUCCEEDED,
|
||||||
|
summary=summary,
|
||||||
|
output="\n".join(output_lines),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _collect_storage_mismatches(self) -> list[str]:
|
||||||
|
upload_root = self.settings.upload_dir
|
||||||
|
folder_ids = _document_folder_ids(upload_root)
|
||||||
|
doc_ids = await self._document_ids()
|
||||||
|
source_counts = await self._source_counts_by_document()
|
||||||
|
folder_by_normalized = {_normalize_identifier(folder_id): folder_id for folder_id in folder_ids}
|
||||||
|
doc_by_normalized = {_normalize_identifier(doc_id): doc_id for doc_id in doc_ids}
|
||||||
|
source_counts_by_normalized = {
|
||||||
|
_normalize_identifier(document_id): count for document_id, count in source_counts.items()
|
||||||
|
}
|
||||||
|
mismatches: list[str] = []
|
||||||
|
|
||||||
|
missing_in_table = sorted(set(folder_by_normalized) - set(doc_by_normalized))
|
||||||
|
for folder_key in missing_in_table:
|
||||||
|
folder_name = folder_by_normalized[folder_key]
|
||||||
|
mismatches.append(f"document-folder-without-row: documents/{folder_name}")
|
||||||
|
|
||||||
|
missing_in_folders = sorted(set(doc_by_normalized) - set(folder_by_normalized))
|
||||||
|
for doc_key in missing_in_folders:
|
||||||
|
doc_id = doc_by_normalized[doc_key]
|
||||||
|
source_count = source_counts_by_normalized.get(doc_key, 0)
|
||||||
|
mismatches.append(f"document-row-without-folder: {doc_id} (source rows: {source_count})")
|
||||||
|
|
||||||
|
for doc_key in sorted(doc_by_normalized):
|
||||||
|
doc_id = doc_by_normalized[doc_key]
|
||||||
|
folder_name = folder_by_normalized.get(doc_key)
|
||||||
|
db_count = source_counts_by_normalized.get(doc_key, 0)
|
||||||
|
file_count = _source_file_count_for_document(upload_root, folder_name) if folder_name is not None else 0
|
||||||
|
if db_count != file_count:
|
||||||
|
path_label = f"documents/{folder_name}" if folder_name is not None else "documents/<missing-folder>"
|
||||||
|
mismatches.append(
|
||||||
|
f"source-count-mismatch: {doc_id} -> source rows: {db_count}, files in {path_label}: {file_count}"
|
||||||
|
)
|
||||||
|
return mismatches
|
||||||
|
|
||||||
|
def _latest_gedcom_upload(self) -> Path | None:
|
||||||
|
genealogy_root = self.settings.upload_dir / "genealogy"
|
||||||
|
if not genealogy_root.exists():
|
||||||
|
return None
|
||||||
|
candidates = [path for path in genealogy_root.rglob("*.ged") if path.is_file()]
|
||||||
|
if not candidates:
|
||||||
|
return None
|
||||||
|
return max(candidates, key=lambda path: (path.stat().st_mtime_ns, path.name.casefold()))
|
||||||
|
|
||||||
|
async def _document_ids(self) -> set[str]:
|
||||||
|
async with self._session_scope() as session:
|
||||||
|
rows = await session.exec(select(Document.id))
|
||||||
|
return {str(item) for item in rows.all()}
|
||||||
|
|
||||||
|
async def _source_counts_by_document(self) -> dict[str, int]:
|
||||||
|
async with self._session_scope() as session:
|
||||||
|
rows = await session.exec(
|
||||||
|
select(
|
||||||
|
Source.document_id,
|
||||||
|
func.count(Source.id), # ty: ignore[invalid-argument-type] - SQLAlchemy descriptor false positive.
|
||||||
|
).group_by(
|
||||||
|
Source.document_id # ty: ignore[invalid-argument-type] - SQLAlchemy descriptor false positive.
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return {str(document_id): int(count) for document_id, count in rows}
|
||||||
|
|
||||||
|
def read_log_bytes(self, *, log_path: str) -> bytes:
|
||||||
|
candidate = (self.settings.log_dir / Path(log_path)).resolve()
|
||||||
|
base = self.settings.log_dir.resolve()
|
||||||
|
try:
|
||||||
|
candidate.relative_to(base)
|
||||||
|
except ValueError as exc:
|
||||||
|
raise MaintenanceError(
|
||||||
|
"Maintenance log path is invalid.",
|
||||||
|
category=ErrorCategory.VALIDATION,
|
||||||
|
suggestion="Refresh and retry.",
|
||||||
|
detail=f"Requested path outside log root: {candidate}",
|
||||||
|
) from exc
|
||||||
|
if not candidate.is_file():
|
||||||
|
raise MaintenanceError(
|
||||||
|
"Maintenance log file is unavailable.",
|
||||||
|
category=ErrorCategory.NOT_FOUND,
|
||||||
|
suggestion="Refresh and retry.",
|
||||||
|
)
|
||||||
|
return candidate.read_bytes()
|
||||||
|
|
||||||
|
def _write_log(self, *, run_id: UUID, output: str) -> str:
|
||||||
|
timestamp = _utc_now_naive().strftime("%Y%m%d-%H%M%S")
|
||||||
|
logs_dir = self.settings.log_dir / "maintenance"
|
||||||
|
logs_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
file_path = logs_dir / f"maintenance-{run_id}-{timestamp}.log"
|
||||||
|
file_path.write_text(output, encoding="utf-8")
|
||||||
|
return str(file_path.relative_to(self.settings.log_dir).as_posix())
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_identifier(value: str) -> str:
|
||||||
|
return value.replace("-", "").strip().lower()
|
||||||
|
|
||||||
|
|
||||||
|
def _document_folder_ids(root: Path) -> set[str]:
|
||||||
|
documents_root = root / "documents"
|
||||||
|
if not documents_root.exists():
|
||||||
|
return set()
|
||||||
|
return {entry.name for entry in documents_root.iterdir() if entry.is_dir()}
|
||||||
|
|
||||||
|
|
||||||
|
def _source_file_count_for_document(root: Path, document_id: str | None) -> int:
|
||||||
|
if document_id is None:
|
||||||
|
return 0
|
||||||
|
directory = root / "documents" / document_id
|
||||||
|
if not directory.exists():
|
||||||
|
return 0
|
||||||
|
return sum(1 for entry in directory.iterdir() if entry.is_file())
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user