๐Ÿ“ฆ EqualifyEverything / equalify-reflow

๐Ÿ“„ job.py ยท 221 lines
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"""Core job models for document conversion tracking."""

from datetime import datetime
from typing import Literal

from pydantic import BaseModel, ConfigDict, Field, field_validator

from .approval import ApprovalRequest
from .pii import PIIFinding

# Valid state transitions for job status state machine
VALID_TRANSITIONS = {
    "pii_scanning": ["awaiting_approval", "processing", "failed"],
    "awaiting_approval": ["processing", "denied", "failed"],
    "processing": ["completed", "failed"],
    "completed": [],  # Terminal state
    "failed": [],     # Terminal state
    "denied": []      # Terminal state
}


class JobSubmission(BaseModel):
    """Initial job submission data for PDF conversion.

    Created when API receives a new PDF upload. Captures all
    metadata needed to track and process the document.

    Attributes:
        job_id: Unique job identifier (UUID format)
        s3_key: S3 object key for uploaded PDF
        created_at: UTC timestamp of submission
        file_size_bytes: PDF file size in bytes
        original_filename: User-provided filename

    Validation Rules:
        - job_id must be valid UUID format
        - file_size_bytes between 1 byte and 100MB
        - s3_key must start with 'temp/' prefix

    Example:
        >>> submission = JobSubmission(
        ...     job_id="550e8400-e29b-41d4-a716-446655440000",
        ...     s3_key="temp/550e8400.../syllabus.pdf",
        ...     created_at=datetime.now(timezone.utc),
        ...     file_size_bytes=2456789,
        ...     original_filename="CS101_Syllabus_Fall2024.pdf"
        ... )
    """
    job_id: str = Field(
        ...,
        pattern=r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$',
        description="UUID format job identifier"
    )
    s3_key: str = Field(
        ...,
        min_length=1,
        description="S3 object key for PDF"
    )
    created_at: datetime = Field(
        ...,
        description="UTC submission timestamp"
    )
    file_size_bytes: int = Field(
        ...,
        gt=0,
        le=100_000_000,  # 100MB max
        description="File size in bytes"
    )
    original_filename: str = Field(
        ...,
        min_length=1,
        max_length=255,
        description="Original uploaded filename"
    )

    @field_validator('s3_key')
    @classmethod
    def validate_s3_key(cls, v: str) -> str:
        """Validate S3 key starts with temp/ prefix."""
        if not v.startswith('temp/'):
            raise ValueError('Temporary uploads must use temp/ prefix')
        return v

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "job_id": "550e8400-e29b-41d4-a716-446655440000",
                "s3_key": "temp/550e8400-e29b-41d4-a716-446655440000/document.pdf",
                "created_at": "2024-01-15T10:30:00Z",
                "file_size_bytes": 2456789,
                "original_filename": "Course_Syllabus.pdf"
            }
        }
    )


class JobStatus(BaseModel):
    """Complete job status with workflow state and results.

    Central data model for tracking conversion jobs through the entire
    pipeline. Stored in Redis and updated as job progresses through states.

    State Machine:
        - pii_scanning โ†’ awaiting_approval | processing | failed
        - awaiting_approval โ†’ processing | denied | failed
        - processing โ†’ completed | failed
        - completed (terminal)
        - failed (terminal)
        - denied (terminal)

    Attributes:
        job_id: Unique job identifier (UUID)
        status: Current workflow state
        created_at: UTC timestamp of initial submission
        updated_at: UTC timestamp of last status change
        pii_findings: Optional list of detected PII entities
        approval_token: Optional token for approval workflow
        expires_at: Optional expiration for approval token
        markdown_url: Optional S3 URL for Markdown output
        confidence_score: Optional AI confidence score
        error_message: Optional error details
        approval_decision: Optional approval request details

    Example:
        >>> status = JobStatus(
        ...     job_id="550e8400-e29b-41d4-a716-446655440000",
        ...     status="processing",
        ...     created_at=datetime.now(timezone.utc),
        ...     updated_at=datetime.now(timezone.utc),
        ...     pii_findings=None,
        ...     approval_token=None
        ... )
    """
    job_id: str = Field(
        ...,
        pattern=r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$',
        description="UUID format job identifier"
    )
    status: Literal[
        "pii_scanning",
        "awaiting_approval",
        "processing",
        "completed",
        "failed",
        "denied"
    ] = Field(
        ...,
        description="Current workflow state"
    )
    created_at: datetime = Field(
        ...,
        description="UTC submission timestamp"
    )
    updated_at: datetime = Field(
        ...,
        description="UTC last update timestamp"
    )

    # Optional fields populated at different stages
    pii_findings: list[PIIFinding] | None = Field(
        default=None,
        description="PII entities detected (if any)"
    )
    approval_token: str | None = Field(
        default=None,
        description="Approval workflow token"
    )
    expires_at: datetime | None = Field(
        default=None,
        description="Approval expiration timestamp"
    )
    markdown_url: str | None = Field(
        default=None,
        description="S3 URL for Markdown output"
    )
    confidence_score: float | None = Field(
        default=None,
        ge=0.0,
        le=1.0,
        description="AI confidence score"
    )
    error_message: str | None = Field(
        default=None,
        max_length=2000,
        description="Error details if failed"
    )
    approval_decision: ApprovalRequest | None = Field(
        default=None,
        description="Approval decision details"
    )

    def can_transition_to(self, new_status: str) -> bool:
        """Check if transition to new status is valid.

        Args:
            new_status: Target status to transition to

        Returns:
            True if transition is allowed by state machine
        """
        valid_next = VALID_TRANSITIONS.get(self.status, [])
        return new_status in valid_next

    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "job_id": "550e8400-e29b-41d4-a716-446655440000",
                "status": "completed",
                "created_at": "2024-01-15T10:30:00Z",
                "updated_at": "2024-01-15T10:35:00Z",
                "pii_findings": None,
                "approval_token": None,
                "expires_at": None,
                "markdown_url": "https://equalify-output.s3.amazonaws.com/...",
                "confidence_score": 0.87,
                "error_message": None,
                "approval_decision": None
            }
        }
    )