Skip to content

refactor(intrinsics): promote _DictContract to shared adapters module; collapse guardian.py's factuality contracts onto it #1412

Closed

Description

@planetf1

Summary

PR #1357 (closes #1332) added four IOContract subclasses to mellea/stdlib/components/intrinsic/guardian.py. Two of them — _FactualityDetectionContract and _FactualityCorrectionContract — are functionally identical to a required-keys dict check: json.loads -> isinstance(dict) -> missing-key check -> raise AdapterSchemaMismatchError.

mellea/stdlib/components/intrinsic/rag.py already has exactly this as _DictContract(name, required_keys), established in PR #1321 (closes #1137). It's module-private to rag.py, so guardian.py couldn't reuse it and hand-rolled the same logic instead.

Scope

  • Promote _DictContract out of rag.py into a shared location in mellea/backends/adapters/ (alongside IOContract/AdapterSchemaMismatchError, both already defined there — no new circular import).
  • Update rag.py to import it from the shared location instead of defining it locally.
  • Refactor guardian.py's _FactualityDetectionContract and _FactualityCorrectionContract to use the shared _DictContract instead of bespoke subclasses.

Explicitly out of scope

Do not touch guardian.py's other two contracts — _PolicyGuardrailsContract (xor validation logic) and _GuardianCheckContract (nested-dict shape) — both have real bespoke validation that doesn't reduce to a required-keys check.

Why

Two competing patterns for the same job (bespoke per-file contract classes vs. one shared dict-contract) is exactly the kind of inconsistency Epic #929 exists to remove. Cheap to fix now, before more IOContract subclasses accumulate across other adapter modules.

Not a Phase 2 blocker

Checked against #1140/#1141/#1142: Phase 2 calls io_contract.parse() abstractly and doesn't depend on which contract implementation guardian.py uses internally. This is independent cleanup, not a prerequisite for Phase 2 work.

Epic: #929

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions