| |
| """ |
| V7.4: Typed SignedStep โ Domain-Aware Validation Contract. |
| |
| Replaces the raw dict `{"id", "hash", "expression"}` with a typed dataclass |
| that carries a StepType so the ConsistencyGate validates by semantic domain, |
| not by string shape. |
| |
| CTO directive: |
| - expression โ display string only (for renderer injection) |
| - payload โ semantic truth (list[str] for geometry, str for algebraic) |
| - step_type โ routing key for ConsistencyGate dispatch |
| |
| Backwards compatibility: SignedStep emulates a dict fully so ALL existing |
| dict-access code (step["id"], step.get("hash"), "id" in step, step.items()) |
| in pedagogical_renderer.py / semantic_bank.py continues to work unchanged. |
| """ |
| from enum import Enum |
| from dataclasses import dataclass, asdict |
| from typing import Any, Optional |
|
|
|
|
| class StepType(Enum): |
| ALGEBRAIC = "algebraic" |
| GEOMETRY = "geometry" |
| NUMERIC = "numeric" |
|
|
|
|
| @dataclass |
| class SignedStep: |
| """ |
| V7.4: Typed, SHA256-signed computation step. |
| |
| Fields: |
| id โ step identifier used as {{id}} placeholder in renderer |
| expression โ human-readable display string (injected into pedagogy text) |
| payload โ semantic truth: list[str] for geometry, str for algebraic |
| step_type โ StepType enum โ routes ConsistencyGate validation |
| hash โ SHA256 digest seeded with canonical form + problem_id + step_id |
| """ |
| id: str |
| expression: str |
| payload: Any |
| step_type: StepType |
| hash: str |
|
|
| |
| |
| |
|
|
| _FIELDS = ("id", "expression", "payload", "step_type", "hash") |
|
|
| def __getitem__(self, key: str): |
| if key not in self._FIELDS: |
| raise KeyError(key) |
| return getattr(self, key) |
|
|
| def get(self, key: str, default=None): |
| try: |
| return self[key] |
| except KeyError: |
| return default |
|
|
| def keys(self): |
| return list(self._FIELDS) |
|
|
| def items(self): |
| return [(k, self[k]) for k in self._FIELDS] |
|
|
| def __contains__(self, key: object) -> bool: |
| return key in self._FIELDS |
|
|