Skip to content

Cross-Document Consistency Memory

director_ai.core.memory.consistency.CrossDocumentConsistencyMemory

CrossDocumentConsistencyMemory(db_path: str | Path = ':memory:', *, score_fn: ConsistencyScoreFn | None = None, use_builtin_similarity: bool = False, warn_threshold: float = 0.65, contradiction_threshold: float = 0.85, max_documents_per_tenant: int = 1000)

SQLite-backed tenant memory for cross-document consistency checks.

Initialise SQLite storage and retention policy settings.

check_document

check_document(tenant_id: str, document_id: str, text: str) -> CrossDocumentConsistencyReport

Evaluate an incoming document against tenant memory.

record_document

record_document(tenant_id: str, document_id: str, text: str, metadata: Mapping[str, Any] | None = None) -> CrossDocumentConsistencyReport

Record a document unless consistency checks block it.

get_document

get_document(tenant_id: str, document_id: str) -> StoredDocument | None

Return one stored tenant document by identifier.

list_documents

list_documents(tenant_id: str) -> tuple[StoredDocument, ...]

Return stored documents for a tenant in retention order.

count

count(*, tenant_id: str | None = None) -> int

Return total document count globally or for one tenant.

delete_tenant

delete_tenant(tenant_id: str) -> int

Delete all documents for a tenant and return the removed count.

close

close() -> None

Close the backing SQLite connection.

director_ai.core.memory.consistency.CrossDocumentConsistencyReport dataclass

CrossDocumentConsistencyReport(decision: str, tenant_id: str, document_id: str, incoming_hash: str, checked_documents: int, conflicts: tuple[CrossDocumentConflict, ...] = tuple())

Decision for an incoming document against tenant memory.

blocked property

blocked: bool

Return whether the incoming document must not be recorded.

__post_init__

__post_init__() -> None

Validate decision state and freeze conflict ordering.

to_dict

to_dict(*, include_text: bool = False) -> dict[str, Any]

Serialise the consistency decision for API or audit output.

director_ai.core.memory.consistency.CrossDocumentConflict dataclass

CrossDocumentConflict(tenant_id: str, incoming_document_id: str, existing_document_id: str, incoming_hash: str, existing_hash: str, score: float, existing_text: str = '', incoming_text: str = '')

Tenant-safe report for one cross-document contradiction.

__post_init__

__post_init__() -> None

Validate conflict score after dataclass construction.

to_dict

to_dict(*, include_text: bool = False) -> dict[str, Any]

Serialise the conflict with optional raw document text.

director_ai.core.memory.consistency.StoredDocument dataclass

StoredDocument(tenant_id: str, document_id: str, text: str, content_hash: str, created_at: float, metadata: Mapping[str, Any] = dict())

One tenant-scoped document retained for consistency checks.

to_dict

to_dict(*, include_text: bool = False) -> dict[str, Any]

Serialise the stored document with optional raw text.

Tenant and Privacy Model

CrossDocumentConsistencyMemory is a SQLite-backed memory for checking whether a new generated document contradicts earlier documents from the same tenant. It is intended for long-running agents, support bots, and regulated workflows where yesterday's answer must stay consistent with today's answer.

  • all reads and comparisons are tenant-scoped
  • tenant ids are validated against ^[A-Za-z0-9_-]{1,64}$
  • blocked reports do not persist the incoming document
  • report serialization omits raw text unless include_text=True
  • delete_tenant() removes retained tenant documents for right-to-delete workflows
  • max_documents_per_tenant enforces bounded retention
from director_ai.core import CrossDocumentConsistencyMemory

memory = CrossDocumentConsistencyMemory(
    "consistency.sqlite",
    score_fn=contradiction_score,
    contradiction_threshold=0.85,
)

report = memory.record_document(
    tenant_id="tenant-a",
    document_id="answer-2026-05-13",
    text=response,
)
if report.blocked:
    escalate(report.to_dict())

Use an NLI, verified-scorer, or domain verifier as score_fn(previous, incoming). The memory layer owns storage, tenant isolation, reporting, and retention; it does not pretend to be a contradiction model by itself.