From 1981ce821c7f6b1a4fe71ca7a1f7309e624219a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Miguel=20P=C3=A9rez=20Canales?= Date: Sun, 8 Nov 2026 16:14:48 +0100 Subject: [PATCH] La fila nace con su job, que es lo que mi propia restriccion exigia MI PROPIA RESTRICCION CAZO MI PROPIA OPERACION, en la primera corrida real. `ck_query_set_corpus_is_declared` exige `job_id` para este tipo, y `_declare` construia la fila, hacia flush y asignaba `row.job_id` DESPUES: el INSERT iba sin el y la base rechazaba la fila entera. IntegrityError CheckViolation ck_query_set_corpus_is_declared job ea8dbc00, FAILED en segundos El arreglo es que `job_id` sea un argumento de `_declare` y no una asignacion posterior, asi que la fila llega a su flush ya completa. EL TEST QUE NO TENIA, y tiene dientes: comprueba el valor EN EL MOMENTO del flush y no al final de la funcion, porque al final el objeto parecia correcto en los dos casos. El defecto solo existe entre esas dos lineas. Reintroducido a proposito, el test falla; con el arreglo, pasa. Y la leccion es la de hoy otra vez, con otra cara: la restriccion es un objeto de la BASE, y mis doce tests eran de funciones puras y dobles, asi que ninguno la tocaba. Esta manana fue NULL + 1, que solo existe en la semantica de SQL; esto es un CHECK, que solo existe en el esquema. Las dos veces el codigo paso ruff, mypy y la suite entera. Dos cosas que NO cambio, y por que: - los bytes se publican antes de escribir la fila, asi que la corrida fallida dejo un objeto sin fila apuntandolo. No lo limpio y no lo cambio: la clave es por contenido, `corpus-population/corpus-.txt`, asi que la corrida buena publica exactamente los mismos bytes en la misma clave. Un huerfano idempotente no es basura, es el mismo fichero; - ruff vio el F821 de `uuid` que mypy no vio, por tercera vez hoy. Queda dicho aqui porque el orden en que los corro importa. --- .../operations/declare_corpus_population.py | 21 ++++- .../test_the_corpus_population_is_declared.py | 82 ++++++++++++++++++- 2 files changed, 97 insertions(+), 6 deletions(-) diff --git a/protea/core/operations/declare_corpus_population.py b/protea/core/operations/declare_corpus_population.py index 1386d0a1..30b6a1d3 100644 --- a/protea/core/operations/declare_corpus_population.py +++ b/protea/core/operations/declare_corpus_population.py @@ -39,6 +39,7 @@ from __future__ import annotations import hashlib +import uuid from pathlib import Path from typing import Any, NamedTuple @@ -205,9 +206,21 @@ def _coverage(session: Session, sequences: int) -> list[dict[str, Any]]: def _declare( - session: Session, p: DeclareCorpusPopulationPayload, got: _Extracted, pub: _Published + session: Session, + p: DeclareCorpusPopulationPayload, + got: _Extracted, + pub: _Published, + job_id: uuid.UUID | None, ) -> QuerySet: - """The row, built from what was actually written, and its entries.""" + """The row, built from what was actually written, and its entries. + + ``job_id`` IS AN ARGUMENT AND NOT ASSIGNED AFTERWARDS, which is the bug this + signature fixes. The first version built the row, flushed it, and set + ``row.job_id`` after: so the INSERT went in without it and + ``ck_query_set_corpus_is_declared`` refused the row, because that CHECK + requires the job for this kind. The constraint caught it on the first real + run, which is the constraint doing its job and a test failing to do mine. + """ sequences = len(set(got.sequence_of.values())) row = QuerySet( name=f"{p.output_name}-{pub.digest[:12]}", @@ -221,6 +234,7 @@ def _declare( ), artifact_uri=pub.uri, content_sha256=pub.digest, + job_id=job_id, code_revision=code_revision(), ) session.add(row) @@ -261,8 +275,7 @@ def execute( ) pub = _publish(_serialise(got.accessions)) _refuse_a_second_row(session, pub.digest) - row = _declare(session, p, got, pub) - row.job_id = job_id_from_payload(payload) + row = _declare(session, p, got, pub, job_id_from_payload(payload)) sequences = len(set(got.sequence_of.values())) result = { "query_set_id": str(row.id), diff --git a/tests/test_the_corpus_population_is_declared.py b/tests/test_the_corpus_population_is_declared.py index b7e0cbb7..ceee5592 100644 --- a/tests/test_the_corpus_population_is_declared.py +++ b/tests/test_the_corpus_population_is_declared.py @@ -15,18 +15,25 @@ from __future__ import annotations import hashlib +import uuid import pytest from protea.core.operations.declare_corpus_population import ( POPULATION_RULE, + DeclareCorpusPopulationPayload, + _declare, _Extracted, + _Published, _refuse_a_second_row, _serialise, population_key_for, ) -from protea.infrastructure.orm.models.query.query_set import QUERY_SET_SOURCES - +from protea.infrastructure.orm.models.query.query_set import ( + QUERY_SET_SOURCES, + QuerySet, + QuerySetEntry, +) # ------------------------------------------------- la serializacion y su huella @@ -172,3 +179,74 @@ def test_accessions_and_sequences_are_counted_apart(): assert len(got.accessions) == 3 assert len(set(got.sequence_of.values())) == 2 + + +# ------------------------------------------- la fila llega al flush con su job + + +class _SesionQueRecuerda: + """Records what the row looked like AT THE MOMENT of each flush. + + A snapshot and not the object, because assigning ``row.job_id`` after the + flush leaves the object looking correct at the end of the function while + the INSERT already went in without it. The defect only exists between those + two lines, so the test has to look there. + """ + + def __init__(self) -> None: + self.anadidos: list[object] = [] + self.al_flush: list[tuple[str, object]] = [] + + def add(self, obj: object) -> None: + self.anadidos.append(obj) + + def add_all(self, objs: object) -> None: + self.anadidos.extend(list(objs)) # type: ignore[arg-type] + + def flush(self) -> None: + for o in self.anadidos: + if isinstance(o, QuerySet): + o.id = o.id or uuid.uuid4() + self.al_flush.append((o.name, o.job_id)) + + +def test_the_row_reaches_its_flush_already_carrying_its_job(): + """What ``ck_query_set_corpus_is_declared`` refused on the first real run. + + The first version built the row, flushed it, and set ``row.job_id`` + afterwards. The INSERT therefore went in without it and the CHECK refused + the whole row, because that constraint requires the job for this kind. The + constraint caught it in production on the first run, which is the + constraint doing its job and this test failing to do mine until now. + + Asserted on the value AT FLUSH TIME, because at the end of the function the + object looked right either way. + """ + trabajo = uuid.uuid4() + sesion = _SesionQueRecuerda() + got = _Extracted(accessions=["P1", "P2"], sequence_of={"P1": 1, "P2": 2}) + pub = _Published(raw=b"P1\nP2\n", digest="d" * 64, key="k", uri="s3://b/k") + + _declare(sesion, DeclareCorpusPopulationPayload(), got, pub, trabajo) # type: ignore[arg-type] + + assert sesion.al_flush, "nada llego a un flush" + for nombre, job_id in sesion.al_flush: + assert job_id == trabajo, nombre + + +def test_an_entry_takes_its_sequence_from_the_protein_row(): + """Never re-hashed from the published bytes, which is the measured hazard. + + The exported bytes are canonicalised and the stored sequence is not, so + hashing them again can name a different sequence. That is how the + hand-registration route could point a declared set at sequences with no + embedding. + """ + sesion = _SesionQueRecuerda() + got = _Extracted(accessions=["P1", "P2"], sequence_of={"P1": 11, "P2": 22}) + pub = _Published(raw=b"P1\nP2\n", digest="e" * 64, key="k", uri="s3://b/k") + + _declare(sesion, DeclareCorpusPopulationPayload(), got, pub, uuid.uuid4()) # type: ignore[arg-type] + + entradas = {e.accession: e.sequence_id for e in sesion.anadidos if isinstance(e, QuerySetEntry)} + assert entradas == {"P1": 11, "P2": 22}