Spaces:
Paused

File size: 2,330 Bytes
8f21a0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Traceability — Hash Record Models

Stores the append-only log of on-chain-worthy integrity proofs.
PHI LEVEL: NONE — hashes only, no patient data.
Multi-tenant: tenant_id on every record (Commandment #3).
"""

from __future__ import annotations

from datetime import UTC, datetime, timezone
from enum import Enum
from typing import Any, Dict, Optional
from uuid import UUID, uuid4

from sqlalchemy import JSON
from sqlmodel import Column, Field, SQLModel


class HashRecordType(str, Enum):
    """What the hash represents. Never store PHI — only typed references."""

    PROTOCOL_VERSION = "protocol_version"  # surgical/clinical protocol snapshot
    WORKFLOW_SIGNATURE = "workflow_signature"  # who executed what workflow, when
    AUDIT_NOTARIZATION = "audit_notarization"  # audit trail export integrity proof
    PUBLICATION_TIMESTAMP = "publication_timestamp"  # research dataset anchor
    CONSENT_PROOF = "consent_proof"  # consent given — hash only, no PHI
    DATASET_INTEGRITY = "dataset_integrity"  # anonymized export fingerprint
    INSTITUTIONAL_VALIDATION = "institutional_validation"  # partner trust record
    CASE_EXPORT = "case_export"  # de-identified case export proof


class HashRecord(SQLModel, table=True):
    """
    Append-only integrity proof record.

    SECURITY: Never store PHI here. Hash inputs must be pre-sanitized.
    Tenant isolation: tenant_id mandatory (Commandment #3).
    Sprint: 72
    """

    __tablename__ = "traceability_hash_records"

    id: UUID = Field(default_factory=uuid4, primary_key=True)
    tenant_id: UUID = Field(index=True)

    record_type: HashRecordType
    sha256_hash: str = Field(max_length=64)  # hex SHA-256
    reference_id: str | None = Field(default=None, max_length=200)  # e.g. case_id, protocol_slug
    reference_type: str | None = Field(
        default=None, max_length=100
    )  # e.g. "surgio_case", "protocol"
    actor_id: UUID | None = Field(default=None)  # who triggered this
    engine: str | None = Field(default=None, max_length=50)  # e.g. "surgio", "educa"
    extra: dict[str, Any] | None = Field(
        default=None, sa_column=Column(JSON)
    )  # was 'metadata' — reserved by SQLAlchemy

    created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))

    class Config:
        use_enum_values = True