File size: 10,500 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | # 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/.
"""Full pipeline β orchestrates all stages from RawInput to Artifact."""
from __future__ import annotations
import hashlib
import time
from dataclasses import dataclass, field
from typing import Optional
from .types import (
ArtifactType, FSMState, IntentClass, OutputMode,
PipelineError, Provenance, RawInput, SemanticNodeType, VerificationState,
)
from .ir import (
Artifact, Constraint, Geometry, Operand, SemanticIR, SemanticNode,
SemanticOperation, TemporalLock, TemporalState,
)
from .nlp import BertDecoder, normalize_input
from .fsm import FSMContext, validate_constraints
from .temporal import apply_temporal, stamp
from .geometry import enforce_geometry
from .matrix import SemanticMatrixBuilder
from .boolean_engine import semantic_constraints_to_boolean, normalize as bool_normalize
from .token_engine import BoolEncoder, encode_binary
from .latin import LatinCanonicalizer
from .lexer import LatinLexer
from .rosetta import RosettaMapper
from .subleq import build_program_from_rosetta, SubleqVM
from .verifier import Verifier
from .artifacts import ArtifactEngine
@dataclass
class PipelineResult:
ir: Optional[SemanticIR] = None
artifact: Optional[Artifact] = None
subleq_listing: str = ""
trace: list[str] = field(default_factory=list)
hashes: dict[str, str] = field(default_factory=dict)
verification_state: VerificationState = VerificationState.UNVERIFIED
error: Optional[str] = None
@property
def ok(self) -> bool:
return self.error is None and self.verification_state == VerificationState.VERIFIED
class SemanticPipeline:
def __init__(self, bert_model: Optional[str] = None, output_mode: OutputMode = OutputMode.NATURAL_LANGUAGE):
self._bert = BertDecoder(bert_model)
self._matrix = SemanticMatrixBuilder()
self._latin = LatinCanonicalizer()
self._lexer = LatinLexer()
self._rosetta = RosettaMapper()
self._verifier = Verifier()
self._artifacts = ArtifactEngine()
self._output_mode = output_mode
def run(self, raw: RawInput, output_mode: Optional[OutputMode] = None) -> PipelineResult:
mode = output_mode or self._output_mode
ctx = FSMContext()
result = PipelineResult()
result.trace.append(f"INPUT: {raw.input_id}")
result.hashes["input"] = raw.hash
try:
# PHASE: INPUT β NORMALIZE
ctx.transition("raw_input")
normalized = normalize_input(raw)
ctx.set("raw_text", normalized.text)
ctx.set("tokens", normalized.tokens)
result.trace.append(f"NORMALIZE: {len(normalized.tokens)} tokens")
# PHASE: NORMALIZE β LEXICAL_ANALYSIS
ctx.transition("normalized_input")
result.trace.append(f"LEXICAL_ANALYSIS: {len(normalized.sentences)} sentences")
# PHASE: LEXICAL_ANALYSIS β INTENT_DETECTION
ctx.transition("token_stream")
candidate = self._bert.decode(normalized)
ctx.set("candidate", candidate)
ctx.set("intent", candidate.intent.value)
result.trace.append(f"INTENT_DETECTION: {candidate.intent.value} ({candidate.confidence:.2f})")
if candidate.ambiguous:
result.trace.append("WARNING: ambiguous input detected")
# PHASE: INTENT_DETECTION β ENTITY_BINDING
ctx.transition("intent_class")
result.trace.append(f"ENTITY_BINDING: {candidate.entities}")
# PHASE: ENTITY_BINDING β SEMANTIC_BINDING
ctx.transition("entities")
provenance = Provenance(
source_input_id=raw.input_id,
stage="semantic_binding",
transform_hash=raw.hash,
)
t0 = TemporalState(tau=0, label="initial")
lock = TemporalLock()
# Build semantic nodes from candidate
operands = [Operand(name=e, type_tag="entity") for e in candidate.entities]
constraints = [Constraint.make(c, c) for c in candidate.constraints]
op = None
if candidate.operators:
op = SemanticOperation(name=candidate.operators[0], arity=len(operands))
node = SemanticNode.make(
node_type=SemanticNodeType.OPERATION if op else SemanticNodeType.INTENT,
operation=op,
operands=operands,
constraints=constraints,
geometry=None,
temporal_state=t0,
provenance=provenance,
)
ir = SemanticIR.create(
intent=candidate.intent,
root=node,
nodes=[node],
temporal_lock=lock,
provenance=provenance,
)
ctx.set("candidate", candidate)
result.trace.append(f"SEMANTIC_BINDING: IR {ir.ir_id[:8]}")
result.hashes["semantic_ir"] = ir.hash()
# PHASE: SEMANTIC_BINDING β TEMPORAL_LOCK
ctx.transition("semantic_candidate")
_, t1 = apply_temporal(lambda: None, t0, lock, "temporal_lock")
result.trace.append(f"TEMPORAL_LOCK: Ο{t0.tau}βΟ{t1.tau}")
# PHASE: TEMPORAL_LOCK β GEOMETRY_BINDING
ctx.transition("temporal_state")
for n in ir.nodes:
geom = enforce_geometry(n)
n.geometry = geom
result.trace.append("GEOMETRY_BINDING: dimensions validated")
# PHASE: GEOMETRY_BINDING β MATRIX_FORM
ctx.transition("geometry")
sem_matrix = self._matrix.build(ir.root)
result.trace.append(f"MATRIX_FORM: {sem_matrix.rows}x{sem_matrix.cols}")
# PHASE: MATRIX_FORM β BOOLEAN_ROOT
ctx.transition("semantic_matrix")
bnf = semantic_constraints_to_boolean(ir.root.constraints)
result.trace.append(f"BOOLEAN_ROOT: {bnf.normalized}")
# PHASE: BOOLEAN_ROOT β BOOLEAN_NORMALIZATION
ctx.transition("boolean_expr")
norm = bool_normalize(bnf.normalized)
bnf_report = self._verifier.verify_boolean(bnf)
self._verifier.reject_if_failed(bnf_report, FSMState.BOOLEAN_NORMALIZATION)
result.trace.append(f"BOOLEAN_NORMALIZATION: stable={bnf.stable}")
# PHASE: BOOLEAN_NORMALIZATION β TOKENIZATION
ctx.transition("normalized_boolean")
encoder = BoolEncoder()
token_stream = encoder.encode_expr(bnf.normalized)
binary = encode_binary(token_stream)
tok_report = self._verifier.verify_tokens(token_stream)
self._verifier.reject_if_failed(tok_report, FSMState.TOKENIZATION)
result.trace.append(f"TOKENIZATION: {len(token_stream)} tokens, {len(binary)} bytes")
# PHASE: TOKENIZATION β LATIN_CANONICALIZATION
ctx.transition("token_stream")
latin_form = self._latin.canonicalize(token_stream)
lat_report = self._verifier.verify_latin(latin_form)
self._verifier.reject_if_failed(lat_report, FSMState.LATIN_CANONICALIZATION)
result.trace.append(f"LATIN_CANONICALIZATION: {latin_form.text()[:60]}")
# PHASE: LATIN_CANONICALIZATION β LATIN_LEXING
ctx.transition("latin_form")
lex_result = self._lexer.lex_canonical(latin_form)
result.trace.append(f"LATIN_LEXING: {len(lex_result.tokens)} lex tokens")
# PHASE: LATIN_LEXING β ROSETTA_MAPPING
ctx.transition("latin_tokens")
mapping = self._rosetta.map(latin_form)
ros_report = self._verifier.verify_rosetta(mapping)
self._verifier.reject_if_failed(ros_report, FSMState.ROSETTA_MAPPING)
result.trace.append(f"ROSETTA_MAPPING: {len(mapping.entries)} entries")
# PHASE: ROSETTA_MAPPING β SUBLEQ_IR
ctx.transition("rosetta_mapping")
prog = build_program_from_rosetta(mapping)
sub_report = self._verifier.verify_subleq(prog)
self._verifier.reject_if_failed(sub_report, FSMState.SUBLEQ_IR)
result.subleq_listing = prog.disassemble_text()
result.hashes["subleq"] = hashlib.sha256(prog.assemble()).hexdigest()
result.trace.append(f"SUBLEQ_IR: {len(prog.instructions)} instructions")
# PHASE: SUBLEQ_IR β VERIFICATION
ctx.transition("subleq_program")
ir_report = self._verifier.verify_ir(ir)
self._verifier.reject_if_failed(ir_report, FSMState.VERIFICATION)
ir.verification_state = VerificationState.VERIFIED
ctx.set("verified", True)
result.trace.append("VERIFICATION: PASSED")
# PHASE: VERIFICATION β ARTIFACT_SELECTION
ctx.transition("verified_ir")
ctx.set("template", "prompt.mustache")
result.trace.append(f"ARTIFACT_SELECTION: mode={mode.value}")
# PHASE: ARTIFACT_SELECTION β EMISSION
ctx.transition("artifact_type")
if mode == OutputMode.NATURAL_LANGUAGE:
artifact = self._artifacts.build_document(ir)
elif mode == OutputMode.CODE:
artifact = self._artifacts.build_backend(ir)
else:
artifact = self._artifacts.build_prompt(ir)
art_report = self._verifier.verify_artifact(artifact, ir)
self._verifier.reject_if_failed(art_report, FSMState.EMISSION)
artifact.verification_state = VerificationState.VERIFIED
ctx.set("artifact", artifact)
result.hashes["artifact"] = artifact.hash()
result.trace.append(f"EMISSION: artifact {artifact.id[:8]}")
# PHASE: EMISSION β COMPLETE
ctx.transition("artifact")
result.ir = ir
result.artifact = artifact
result.verification_state = VerificationState.VERIFIED
result.trace.append("COMPLETE")
except PipelineError as e:
result.error = str(e)
result.verification_state = VerificationState.REJECTED
result.trace.append(f"ERROR: {e}")
return result
|