generated from john/python-template
Decompose SourceService along the aggregate boundary and then correct the
instruction file that caused it to grow, in that order. The refactor is the
empirical test of the rule.
services/evidence.py (new)
EvidenceService owns ExecutionAttempt: read_latest_execution_attempt,
list_execution_attempts, promote_machine_attempt, build_evidence_export,
plus the LatestExecutionAttempt projection. Moved verbatim from sources.py.
services/errors.py (new)
The five-class error hierarchy (PromptLoadError, TranscriptionError,
TranscriptionNotFoundError, SourceDeleteBlockedError,
CandidatePromotionError) moved out of sources.py. evidence.py needs
TranscriptionNotFoundError, and test_service_boundaries.py correctly
rejected the sibling import. errors.py defines no *Service class, so it is
a legal shared home. This was the boundary test doing its job, not an
obstacle to route around.
sources.py 1,389 -> 885 lines (1,063 after Phase 2).
services/__init__.py
ServiceBundle and from_session_factory register evidence. Note that
field-by-field ServiceBundle construction silently binds services to the
process-global session factory via default_factory; from_session_factory is
the only safe constructor. Two test bundles were fixed for this.
.github/instructions/services.instructions.md
Rewritten to describe the boundaries the decomposition actually produced,
per plan Phase 3 task 7 and review log [59].
- "1 service class per data model" -> one service class per aggregate.
The table-shaped rule is the measured cause of sources.py reaching
1,389 lines; DocumentType has no lifecycle without Document.
- New Model Ownership section. Junctions are owned by their lifecycle
owner, the service that creates and deletes the rows: document_person
to PeopleService (sole writer, measured), job_source to SourceService.
Two carve-outs are stated rather than left as silent violations:
cascade deletion when a service deletes its own aggregate root, and
status transitions that create and delete nothing (cancel_job,
resubmit_failed_sources), which are Job lifecycle events on the work
queue. EvidenceService.promote_machine_attempt's two-field write to
Source is named and scoped.
- Mandatory CRUD softened to intent. It was already false: five modules
define no service class, EvidenceService has no create/delete because
ExecutionAttempt is append-only, RegistryService uses <op>_entry.
- Separated reading across models via eager loads from the owning root,
which is allowed, from importing another service, which is not. The old
line 13 and lines 75-77 read as contradictory.
- Typo: picutre.
No code was moved to satisfy the rule.
tests/test_service_boundaries.py
Docstring no longer cites the instruction file by line number; that anchor
would desynchronise silently. errors.py added to the neutral-module list.
Verified: 292 passed, 4 skipped, 0 ruff, 0 ty. All 25 /ui/* routes walked
against the live app; 24x 200. /ui/documents/{id}/sources 404s via a 307 that
drops the /ui prefix, confirmed pre-existing (last touched in 6a3ee26) and
left alone as out of scope.
Co-authored-by: Copilot App <[email protected]>
124 lines
4.7 KiB
Python
124 lines
4.7 KiB
Python
"""V4.5 retranscription candidate and promotion tests."""
|
|
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
|
|
from transcription.config import Settings
|
|
from transcription.db.models import Document
|
|
from transcription.db.models import Job
|
|
from transcription.db.models import JobPurpose
|
|
from transcription.db.models import JobSource
|
|
from transcription.db.models import Source
|
|
from transcription.services import ServiceBundle
|
|
from transcription.services.errors import CandidatePromotionError
|
|
from transcription.services.workflows import create_source_retranscription_job
|
|
|
|
|
|
def _services(default_session_factory, settings: Settings) -> ServiceBundle:
|
|
return ServiceBundle.from_session_factory(default_session_factory, settings=settings)
|
|
|
|
|
|
async def _seed_source(services: ServiceBundle) -> Source:
|
|
document = await services.documents.create_document(Document(name="V4.5 source"))
|
|
source = Source(
|
|
document_id=document.id,
|
|
page_number=1,
|
|
upload_name="page.jpg",
|
|
filename="page.jpg",
|
|
file_path="page.jpg",
|
|
file_hash="a" * 64,
|
|
file_size_bytes=1,
|
|
revised_text="human revision",
|
|
)
|
|
return await services.sources.create_source(source)
|
|
|
|
|
|
@pytest.mark.integration
|
|
@pytest.mark.asyncio
|
|
async def test_first_success_is_preferred_and_later_success_remains_candidate(default_session_factory):
|
|
settings = Settings(openrouter_api_key="test-key", provider_models=None)
|
|
services = _services(default_session_factory, settings)
|
|
source = await _seed_source(services)
|
|
|
|
first_job = await services.jobs.create_job(Job(document_id=source.document_id))
|
|
second_job = await services.jobs.create_job(Job(document_id=source.document_id))
|
|
await services.sources.create_job_source(JobSource(job_id=first_job.id, source_id=source.id))
|
|
await services.sources.create_job_source(JobSource(job_id=second_job.id, source_id=source.id))
|
|
|
|
await services.sources.update_job_source_transcription(
|
|
job_id=first_job.id,
|
|
source_id=source.id,
|
|
text="first result",
|
|
provider="fixture",
|
|
model="model-a",
|
|
)
|
|
selected = await services.sources.read_source(source.id)
|
|
first_attempt_id = selected.preferred_execution_attempt_id
|
|
|
|
await services.sources.update_job_source_transcription(
|
|
job_id=second_job.id,
|
|
source_id=source.id,
|
|
text="candidate result",
|
|
provider="fixture",
|
|
model="model-b",
|
|
)
|
|
unchanged = await services.sources.read_source(source.id)
|
|
attempts = await services.evidence.list_execution_attempts(source_id=source.id)
|
|
|
|
assert unchanged.raw_transcription == "first result"
|
|
assert unchanged.preferred_execution_attempt_id == first_attempt_id
|
|
assert {attempt.raw_transcription for attempt in attempts} == {"first result", "candidate result"}
|
|
|
|
candidate = next(attempt for attempt in attempts if attempt.raw_transcription == "candidate result")
|
|
promoted = await services.evidence.promote_machine_attempt(
|
|
source_id=source.id,
|
|
execution_attempt_id=candidate.id,
|
|
)
|
|
assert promoted.raw_transcription == "candidate result"
|
|
assert promoted.preferred_execution_attempt_id == candidate.id
|
|
assert promoted.revised_text == "human revision"
|
|
|
|
|
|
@pytest.mark.integration
|
|
@pytest.mark.asyncio
|
|
async def test_promotion_rejects_unrelated_attempt(default_session_factory):
|
|
settings = Settings(openrouter_api_key="test-key", provider_models=None)
|
|
services = _services(default_session_factory, settings)
|
|
source = await _seed_source(services)
|
|
|
|
with pytest.raises(CandidatePromotionError):
|
|
await services.evidence.promote_machine_attempt(
|
|
source_id=source.id,
|
|
execution_attempt_id=uuid4(),
|
|
)
|
|
|
|
|
|
@pytest.mark.integration
|
|
@pytest.mark.asyncio
|
|
async def test_retranscription_job_locks_source_and_frozen_model(default_session_factory, tmp_path):
|
|
settings = Settings(
|
|
openrouter_api_key="test-key",
|
|
prompt_dir=tmp_path,
|
|
provider_model="vendor/default",
|
|
provider_models=["vendor/alternate"],
|
|
)
|
|
(tmp_path / settings.default_prompt_name).write_text("Transcribe verbatim.", encoding="utf-8")
|
|
services = _services(default_session_factory, settings)
|
|
source = await _seed_source(services)
|
|
|
|
job = await create_source_retranscription_job(
|
|
source_id=source.id,
|
|
model="vendor/alternate",
|
|
services=services,
|
|
settings=settings,
|
|
)
|
|
loaded = await services.jobs.read_job(job.id)
|
|
|
|
assert loaded.purpose == JobPurpose.RETRANSCRIPTION
|
|
assert loaded.document_id == source.document_id
|
|
assert loaded.provider == "openrouter"
|
|
assert loaded.model == "vendor/alternate"
|
|
assert loaded.user_prompt == "Transcribe verbatim."
|
|
assert [link.source_id for link in loaded.job_sources] == [source.id]
|