Spaces:
Running
Running
File size: 17,547 Bytes
3b65839 5c2885e 3b65839 5c2885e 3b65839 5c2885e 3b65839 5c2885e 3b65839 5c2885e 3b65839 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | """Sprint A14-S7 β ``PipelineExecutor`` mono-document.
Tous les tests utilisent des stubs ``StepExecutor`` dΓ©finis dans
ce fichier β aucun adapter rΓ©el n'est instanciΓ©, ce qui rend la
suite rapide et dΓ©terministe.
Couvre les cas critiques :
- pipeline qui réussit complètement,
- step qui lΓ¨ve β step en Γ©chec, pipeline continue,
- adapter introuvable (KeyError du resolver),
- output manquant (adapter ne retourne pas un type promis),
- input manquant (initial_inputs incomplet),
- fork avec ``inputs_from`` explicite (reprise du Sprint 66),
- spec invalide β ``PipelineSpecInvalid`` levΓ©e,
- bag versionnΓ© : Γ©tape qui consomme l'output d'une Γ©tape antΓ©rieure.
"""
from __future__ import annotations
import pytest
from picarones.domain import (
Artifact,
ArtifactType,
DocumentRef,
PicaronesError,
)
from picarones.pipeline import (
PipelineExecutor,
PipelineResult,
PipelineSpec,
PipelineSpecInvalid,
PipelineStep,
RunContext,
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Stubs ``StepExecutor``
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class _StubOCR:
name = "stub_ocr"
input_types = frozenset({ArtifactType.IMAGE})
output_types = frozenset({ArtifactType.RAW_TEXT, ArtifactType.ALTO_XML})
def execute(self, inputs, params, context, control):
return {
ArtifactType.RAW_TEXT: Artifact(
id=f"{context.document_id}:ocr:raw_text",
document_id=context.document_id,
type=ArtifactType.RAW_TEXT,
produced_by_step="ocr",
),
ArtifactType.ALTO_XML: Artifact(
id=f"{context.document_id}:ocr:alto_xml",
document_id=context.document_id,
type=ArtifactType.ALTO_XML,
produced_by_step="ocr",
),
}
class _StubLLM:
name = "stub_llm"
input_types = frozenset({ArtifactType.RAW_TEXT})
output_types = frozenset({ArtifactType.CORRECTED_TEXT})
def execute(self, inputs, params, context, control):
return {
ArtifactType.CORRECTED_TEXT: Artifact(
id=f"{context.document_id}:llm:corrected_text",
document_id=context.document_id,
type=ArtifactType.CORRECTED_TEXT,
produced_by_step="llm",
),
}
class _CrashingStub:
name = "crashing"
input_types = frozenset({ArtifactType.RAW_TEXT})
output_types = frozenset({ArtifactType.CORRECTED_TEXT})
def execute(self, inputs, params, context, control):
raise RuntimeError("simulated boom")
class _IncompleteOutputStub:
"""Promet RAW_TEXT mais ne le retourne pas β viole le contrat."""
name = "incomplete"
input_types = frozenset({ArtifactType.IMAGE})
output_types = frozenset({ArtifactType.RAW_TEXT})
def execute(self, inputs, params, context, control):
return {} # vide intentionnellement
class _SecondOCRStub:
"""Second OCR pour tester le fork via inputs_from."""
name = "ocr_b"
input_types = frozenset({ArtifactType.IMAGE})
output_types = frozenset({ArtifactType.RAW_TEXT})
def execute(self, inputs, params, context, control):
return {
ArtifactType.RAW_TEXT: Artifact(
id=f"{context.document_id}:ocr_b:raw_text",
document_id=context.document_id,
type=ArtifactType.RAW_TEXT,
produced_by_step="ocr_b",
),
}
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Fixtures
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@pytest.fixture
def registry() -> dict[str, object]:
return {
"stub_ocr": _StubOCR(),
"stub_ocr_b": _SecondOCRStub(),
"stub_llm": _StubLLM(),
"crashing": _CrashingStub(),
"incomplete": _IncompleteOutputStub(),
}
@pytest.fixture
def executor(registry: dict[str, object]) -> PipelineExecutor:
return PipelineExecutor(adapter_resolver=lambda name: registry[name])
@pytest.fixture
def doc() -> DocumentRef:
return DocumentRef(id="doc1", image_uri="/tmp/x.png")
@pytest.fixture
def ctx() -> RunContext:
return RunContext(
document_id="doc1", code_version="1.0.0", pipeline_name="test",
)
@pytest.fixture
def image_artifact() -> Artifact:
return Artifact(
id="doc1:image",
document_id="doc1",
type=ArtifactType.IMAGE,
uri="/tmp/x.png",
)
def _ocr_only_spec() -> PipelineSpec:
return PipelineSpec(
name="ocr_only",
initial_inputs=(ArtifactType.IMAGE,),
steps=(
PipelineStep(
id="ocr", kind="ocr", adapter_name="stub_ocr",
input_types=(ArtifactType.IMAGE,),
output_types=(
ArtifactType.RAW_TEXT, ArtifactType.ALTO_XML,
),
),
),
)
def _ocr_llm_spec() -> PipelineSpec:
return PipelineSpec(
name="ocr_llm",
initial_inputs=(ArtifactType.IMAGE,),
steps=(
PipelineStep(
id="ocr", kind="ocr", adapter_name="stub_ocr",
input_types=(ArtifactType.IMAGE,),
output_types=(
ArtifactType.RAW_TEXT, ArtifactType.ALTO_XML,
),
),
PipelineStep(
id="llm", kind="post_correction", adapter_name="stub_llm",
input_types=(ArtifactType.RAW_TEXT,),
output_types=(ArtifactType.CORRECTED_TEXT,),
inputs_from={ArtifactType.RAW_TEXT: "ocr"},
),
),
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Cas nominaux
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestNominalRun:
def test_single_step_pipeline(
self, executor, doc, ctx, image_artifact,
) -> None:
spec = _ocr_only_spec()
result = executor.run(
spec, doc, {ArtifactType.IMAGE: image_artifact}, ctx,
)
assert isinstance(result, PipelineResult)
assert result.succeeded
assert result.pipeline_name == "ocr_only"
assert result.document_id == "doc1"
assert len(result.step_results) == 1
assert result.step_results[0].succeeded
assert result.step_results[0].step_id == "ocr"
def test_two_step_pipeline_chains_artifacts(
self, executor, doc, ctx, image_artifact,
) -> None:
spec = _ocr_llm_spec()
result = executor.run(
spec, doc, {ArtifactType.IMAGE: image_artifact}, ctx,
)
assert result.succeeded
# Tous les artefacts sont lΓ : initial + 2 OCR + 1 LLM = 4
assert len(result.artifacts) == 4
types = {a.type for a in result.artifacts}
assert ArtifactType.IMAGE in types
assert ArtifactType.RAW_TEXT in types
assert ArtifactType.ALTO_XML in types
assert ArtifactType.CORRECTED_TEXT in types
def test_step_results_record_produced_artifacts(
self, executor, doc, ctx, image_artifact,
) -> None:
result = executor.run(
_ocr_llm_spec(), doc,
{ArtifactType.IMAGE: image_artifact}, ctx,
)
ocr_result = result.step_result_by_id("ocr")
assert ocr_result is not None
assert "raw_text" in ocr_result.produced_artifacts
assert "alto_xml" in ocr_result.produced_artifacts
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Cas d'erreur β capture gracieuse
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestErrorCapture:
def test_step_that_raises_marks_step_failed(
self, executor, doc, ctx, image_artifact,
) -> None:
"""Un step qui lΓ¨ve β step en Γ©chec, pipeline continue."""
spec = PipelineSpec(
name="ocr_then_crash",
initial_inputs=(ArtifactType.IMAGE,),
steps=(
PipelineStep(
id="ocr", kind="ocr", adapter_name="stub_ocr",
input_types=(ArtifactType.IMAGE,),
output_types=(
ArtifactType.RAW_TEXT, ArtifactType.ALTO_XML,
),
),
PipelineStep(
id="boom", kind="post_correction",
adapter_name="crashing",
input_types=(ArtifactType.RAW_TEXT,),
output_types=(ArtifactType.CORRECTED_TEXT,),
),
),
)
result = executor.run(
spec, doc, {ArtifactType.IMAGE: image_artifact}, ctx,
)
assert not result.succeeded
assert result.step_results[0].succeeded
assert not result.step_results[1].succeeded
assert "adapter_raised" in (result.step_results[1].error or "")
assert "simulated boom" in (result.step_results[1].error or "")
def test_unknown_adapter_yields_step_failure(
self, executor, doc, ctx, image_artifact,
) -> None:
spec = PipelineSpec(
name="bad_adapter",
initial_inputs=(ArtifactType.IMAGE,),
steps=(
PipelineStep(
id="ocr", kind="ocr", adapter_name="not_in_registry",
input_types=(ArtifactType.IMAGE,),
output_types=(ArtifactType.RAW_TEXT,),
),
),
)
result = executor.run(
spec, doc, {ArtifactType.IMAGE: image_artifact}, ctx,
)
assert not result.succeeded
assert "adapter_not_found" in (result.step_results[0].error or "")
def test_adapter_returns_missing_output(
self, executor, doc, ctx, image_artifact,
) -> None:
spec = PipelineSpec(
name="incomplete",
initial_inputs=(ArtifactType.IMAGE,),
steps=(
PipelineStep(
id="bad", kind="ocr", adapter_name="incomplete",
input_types=(ArtifactType.IMAGE,),
output_types=(ArtifactType.RAW_TEXT,),
),
),
)
result = executor.run(
spec, doc, {ArtifactType.IMAGE: image_artifact}, ctx,
)
assert not result.succeeded
assert "missing_output" in (result.step_results[0].error or "")
def test_initial_inputs_missing_blocks_first_step(
self, executor, doc, ctx,
) -> None:
"""Si initial_inputs ne fournit pas IMAGE alors qu'un step en
a besoin, le step Γ©choue avec missing_input."""
# On garde la spec valide (initial_inputs dΓ©clare IMAGE) mais
# le caller "oublie" de fournir l'artefact β rΓ©solution
# d'inputs Γ©choue au runtime.
spec = _ocr_only_spec()
result = executor.run(spec, doc, {}, ctx) # vide
assert not result.succeeded
assert "missing_input" in (result.step_results[0].error or "")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Bag versionnΓ© β fork via ``inputs_from`` (Sprint 66 historique)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestBagVersionedFork:
def test_inputs_from_explicit_picks_correct_version(
self, executor, doc, ctx, image_artifact,
) -> None:
"""Deux OCR successifs produisent RAW_TEXT. L'Γ©tape LLM
prΓ©cise ``inputs_from = "ocr_a"`` et doit consommer la
version A, pas la dernière (B)."""
spec = PipelineSpec(
name="fork",
initial_inputs=(ArtifactType.IMAGE,),
steps=(
PipelineStep(
id="ocr_a", kind="ocr", adapter_name="stub_ocr",
input_types=(ArtifactType.IMAGE,),
output_types=(
ArtifactType.RAW_TEXT, ArtifactType.ALTO_XML,
),
),
PipelineStep(
id="ocr_b", kind="ocr", adapter_name="stub_ocr_b",
input_types=(ArtifactType.IMAGE,),
output_types=(ArtifactType.RAW_TEXT,),
),
PipelineStep(
id="llm", kind="post_correction",
adapter_name="stub_llm",
input_types=(ArtifactType.RAW_TEXT,),
output_types=(ArtifactType.CORRECTED_TEXT,),
inputs_from={ArtifactType.RAW_TEXT: "ocr_a"},
),
),
)
result = executor.run(
spec, doc, {ArtifactType.IMAGE: image_artifact}, ctx,
)
assert result.succeeded
# 1 image initiale + 2 (ocr_a) + 1 (ocr_b) + 1 (llm) = 5
assert len(result.artifacts) == 5
def test_default_picks_latest_when_no_inputs_from(
self, executor, doc, ctx, image_artifact,
) -> None:
"""Sans ``inputs_from``, le LLM consomme le dernier RAW_TEXT,
donc ``ocr_b`` (dernière étape qui a produit le type)."""
spec = PipelineSpec(
name="latest",
initial_inputs=(ArtifactType.IMAGE,),
steps=(
PipelineStep(
id="ocr_a", kind="ocr", adapter_name="stub_ocr",
input_types=(ArtifactType.IMAGE,),
output_types=(
ArtifactType.RAW_TEXT, ArtifactType.ALTO_XML,
),
),
PipelineStep(
id="ocr_b", kind="ocr", adapter_name="stub_ocr_b",
input_types=(ArtifactType.IMAGE,),
output_types=(ArtifactType.RAW_TEXT,),
),
PipelineStep(
id="llm", kind="post_correction",
adapter_name="stub_llm",
input_types=(ArtifactType.RAW_TEXT,),
output_types=(ArtifactType.CORRECTED_TEXT,),
# pas d'inputs_from
),
),
)
result = executor.run(
spec, doc, {ArtifactType.IMAGE: image_artifact}, ctx,
)
assert result.succeeded
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Validation dΓ©fensive
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestDefensiveValidation:
def test_invalid_spec_raises(
self, executor, doc, ctx, image_artifact,
) -> None:
"""Spec avec ID dupliquΓ© β l'executor lΓ¨ve sans appeler
aucun adapter."""
spec = PipelineSpec(
name="dup",
initial_inputs=(ArtifactType.IMAGE,),
steps=(
PipelineStep(
id="step", kind="ocr", adapter_name="stub_ocr",
input_types=(ArtifactType.IMAGE,),
output_types=(
ArtifactType.RAW_TEXT, ArtifactType.ALTO_XML,
),
),
PipelineStep(
id="step", kind="post_correction",
adapter_name="stub_llm",
input_types=(ArtifactType.RAW_TEXT,),
output_types=(ArtifactType.CORRECTED_TEXT,),
),
),
)
with pytest.raises(PipelineSpecInvalid, match="dupliquΓ©"):
executor.run(
spec, doc, {ArtifactType.IMAGE: image_artifact}, ctx,
)
def test_non_callable_resolver_rejected(self) -> None:
with pytest.raises(PicaronesError, match="callable"):
PipelineExecutor(adapter_resolver="not_callable") # type: ignore[arg-type]
|