File size: 5,685 Bytes
fc3e70a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

"""Phase 1: Core types β€” all canonical IR types, enums, and base structures."""

from __future__ import annotations

import hashlib
import struct
import time
import uuid
from dataclasses import dataclass, field
from enum import Enum, auto
from typing import Any, Optional


# ── Enums ──────────────────────────────────────────────────────────────────────

class FSMState(Enum):
    INPUT               = auto()
    NORMALIZE           = auto()
    LEXICAL_ANALYSIS    = auto()
    INTENT_DETECTION    = auto()
    ENTITY_BINDING      = auto()
    SEMANTIC_BINDING    = auto()
    TEMPORAL_LOCK       = auto()
    GEOMETRY_BINDING    = auto()
    MATRIX_FORM         = auto()
    BOOLEAN_ROOT        = auto()
    BOOLEAN_NORMALIZATION = auto()
    TOKENIZATION        = auto()
    LATIN_CANONICALIZATION = auto()
    LATIN_LEXING        = auto()
    ROSETTA_MAPPING     = auto()
    SUBLEQ_IR           = auto()
    VERIFICATION        = auto()
    ARTIFACT_SELECTION  = auto()
    EMISSION            = auto()
    COMPLETE            = auto()
    ERROR               = auto()


class VerificationState(Enum):
    UNVERIFIED  = auto()
    VALIDATING  = auto()
    VERIFIED    = auto()
    REJECTED    = auto()


class ArtifactType(Enum):
    PROMPT      = "prompt"
    DOCUMENT    = "document"
    SOURCE      = "source"
    CONFIG      = "config"
    TEST        = "test"
    SCHEMA      = "schema"
    REPORT      = "report"
    BACKEND     = "backend"


class SemanticNodeType(Enum):
    OPERATION   = "operation"
    ENTITY      = "entity"
    CONSTRAINT  = "constraint"
    RELATION    = "relation"
    INTENT      = "intent"
    GEOMETRY    = "geometry"
    TEMPORAL    = "temporal"
    LITERAL     = "literal"


class BooleanOperator(Enum):
    AND         = "AND"
    OR          = "OR"
    NOT         = "NOT"
    XOR         = "XOR"
    IMPLIES     = "IMPLIES"
    EQUIVALENCE = "EQUIVALENCE"
    TRUE        = "TRUE"
    FALSE       = "FALSE"


class IntentClass(Enum):
    BUILD       = "build"
    TRANSFORM   = "transform"
    VALIDATE    = "validate"
    GENERATE    = "generate"
    PARSE       = "parse"
    COMPUTE     = "compute"
    RECONSTRUCT = "reconstruct"
    UNKNOWN     = "unknown"


class OutputMode(Enum):
    NATURAL_LANGUAGE = "natural_language"
    CODE             = "code"
    ARTIFACT         = "artifact"


# ── Input types ───────────────────────────────────────────────────────────────

@dataclass(frozen=True)
class RawInput:
    text: str
    source: str = "stdin"
    timestamp: float = field(default_factory=time.time)
    input_id: str = field(default_factory=lambda: str(uuid.uuid4()))

    @property
    def hash(self) -> str:
        return hashlib.sha256(self.text.encode()).hexdigest()


@dataclass(frozen=True)
class NormalizedInput:
    original: RawInput
    text: str
    tokens: tuple[str, ...]
    sentences: tuple[str, ...]

    @property
    def hash(self) -> str:
        return hashlib.sha256(self.text.encode()).hexdigest()


@dataclass
class SemanticCandidate:
    input_ref: str
    intent: IntentClass
    entities: list[str]
    operators: list[str]
    constraints: list[str]
    relationships: list[tuple[str, str, str]]
    confidence: float
    ambiguous: bool
    raw_embeddings: Optional[list[float]] = None

    def __post_init__(self):
        if not 0.0 <= self.confidence <= 1.0:
            raise ValueError(f"Confidence must be in [0,1], got {self.confidence}")


# ── Provenance ────────────────────────────────────────────────────────────────

@dataclass(frozen=True)
class Provenance:
    source_input_id: str
    stage: str
    timestamp: float = field(default_factory=time.time)
    parent_id: Optional[str] = None
    transform_hash: str = ""

    def chain_hash(self, data: str) -> str:
        return hashlib.sha256(f"{self.transform_hash}:{data}".encode()).hexdigest()


# ── FSM Transition record ─────────────────────────────────────────────────────

@dataclass(frozen=True)
class FSMTransition:
    source_state: FSMState
    input_class: str
    operation: str
    constraints: tuple[str, ...]
    destination_state: FSMState
    invariant: str

    def validate(self, current_state: FSMState, input_class: str) -> bool:
        return self.source_state == current_state and self.input_class == input_class


# ── Pipeline error ────────────────────────────────────────────────────────────

class PipelineError(Exception):
    def __init__(self, stage: FSMState, message: str, input_ref: str = ""):
        self.stage = stage
        self.input_ref = input_ref
        super().__init__(f"[{stage.name}] {message}")


class GeometryError(PipelineError):
    pass


class TemporalViolation(PipelineError):
    pass


class VerificationError(PipelineError):
    pass


class TokenError(PipelineError):
    pass


class RosettaError(PipelineError):
    pass