Initial commit: 2125_GCE project
This commit is contained in:
Executable
+23
@@ -0,0 +1,23 @@
|
||||
"""
|
||||
execute_compressed — Substrate execution subsystems for compressed GX binaries.
|
||||
|
||||
Provides the five missing components required to execute compressed binaries
|
||||
inside the GlyphOS ecosystem:
|
||||
|
||||
1. SEE — Symbolic Execution Envelope: wraps code in symbolic cognition context
|
||||
2. GAML — Glyph-Aligned Memory Layout: deterministic memory map by glyph offsets
|
||||
3. TDS — Temporal Decompression Scheduler: segment lifecycle management
|
||||
4. IEL — Integrity Echo Layer: resonance-based integrity verification
|
||||
5. SAJT — Substrate-Aware Jump Table: safe transitions across compression zones
|
||||
|
||||
Each subsystem integrates with the existing XIC VM, LAIN engine, glyph registry,
|
||||
and FedMart telemetry systems.
|
||||
"""
|
||||
|
||||
from .see import SymbolicExecutionEnvelope
|
||||
from .gaml import GlyphAlignedMemoryLayout
|
||||
|
||||
__all__ = [
|
||||
"SymbolicExecutionEnvelope",
|
||||
"GlyphAlignedMemoryLayout",
|
||||
]
|
||||
Executable
+355
@@ -0,0 +1,355 @@
|
||||
"""
|
||||
GAML — Glyph-Aligned Memory Layout
|
||||
|
||||
Deterministic memory layout aligned to glyph offsets for compressed GX execution.
|
||||
|
||||
Maps glyph IDs to memory regions based on:
|
||||
- Glyph priority (higher priority = lower address offset)
|
||||
- Glyph band (A/B/C/D determines segment size class)
|
||||
- Glyph score (determines capacity within the region)
|
||||
- Specialized type (aether_node, monument_grade, etc. get reserved spans)
|
||||
|
||||
The layout is fully deterministic — same glyph set always produces the same memory map,
|
||||
guaranteeing reproducible execution across runs.
|
||||
|
||||
Integration points:
|
||||
- Glyph registry (glyphs/super_registry.py): reads glyph data for layout calculations
|
||||
- Specialized types (glyphs/specialized_types.py): type-specific memory constraints
|
||||
- XIC VM context (xic_ops.py): XICContext._state stores the active memory layout
|
||||
- Segment runtime (xic_extensions/segment_runtime.py): segments are loaded into layout
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Layout constants
|
||||
PAGE_SIZE = 256
|
||||
RESERVED_BASE = 0x0000
|
||||
AETHER_NODE_BASE = 0x0100
|
||||
MONUMENT_BASE = 0x1000
|
||||
STANDARD_BASE = 0x4000
|
||||
STACK_BASE = 0xF000
|
||||
MAX_ADDRESS = 0xFFFF
|
||||
|
||||
BAND_SIZE_MULTIPLIERS = {
|
||||
"A": 16,
|
||||
"B": 8,
|
||||
"C": 4,
|
||||
"D": 2,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class MemoryRegion:
|
||||
"""A contiguous memory region assigned to a glyph.
|
||||
|
||||
Attributes:
|
||||
glyph_id: The glyph this region belongs to.
|
||||
base: Base address (16-bit).
|
||||
size: Size in bytes.
|
||||
band: The glyph's band ("A"–"D").
|
||||
priority: Glyph priority (higher = more favorable placement).
|
||||
label: Human-readable label for debugging.
|
||||
type: Region type ("code", "data", "stack", "reserved").
|
||||
permissions: Access permissions ("rw", "rx", "r").
|
||||
"""
|
||||
glyph_id: str
|
||||
base: int
|
||||
size: int
|
||||
band: str
|
||||
priority: float
|
||||
label: str = ""
|
||||
type: str = "code"
|
||||
permissions: str = "rx"
|
||||
|
||||
|
||||
@dataclass
|
||||
class GlyphAlignedMemoryLayout:
|
||||
"""
|
||||
Deterministic memory layout built from a set of glyph IDs.
|
||||
|
||||
Layout algorithm:
|
||||
1. Sort glyphs by priority descending
|
||||
2. Allocate regions: AETHER_NODE_BASE → MONUMENT_BASE → STANDARD_BASE
|
||||
3. Within each tier, allocate in band order (A→D), then by priority
|
||||
4. Each region is PAGE_SIZE * band_multiplier bytes
|
||||
5. Stack region at STACK_BASE with reserved span
|
||||
6. Result is fully deterministic for the same input set
|
||||
"""
|
||||
|
||||
regions: List[MemoryRegion] = field(default_factory=list)
|
||||
glyph_map: Dict[str, MemoryRegion] = field(default_factory=dict)
|
||||
total_size: int = 0
|
||||
|
||||
@classmethod
|
||||
def build(
|
||||
cls,
|
||||
glyph_ids: List[str],
|
||||
glyph_data: Optional[Dict[str, Any]] = None,
|
||||
) -> "GlyphAlignedMemoryLayout":
|
||||
"""
|
||||
Construct a memory layout for the given glyph IDs.
|
||||
|
||||
Args:
|
||||
glyph_ids: List of glyph IDs to lay out (e.g. ["G001", "G015", "G042"]).
|
||||
glyph_data: Optional dict of glyph_id → glyph dict with
|
||||
priority, band, score, specialized_type fields.
|
||||
If None, loads from super_registry.
|
||||
|
||||
Returns:
|
||||
GlyphAlignedMemoryLayout with regions allocated.
|
||||
"""
|
||||
if glyph_data is None:
|
||||
glyph_data = cls._load_glyph_data(glyph_ids)
|
||||
|
||||
tiered: Dict[str, List[Tuple[str, Dict[str, Any]]]] = {
|
||||
"aether": [],
|
||||
"monument": [],
|
||||
"standard": [],
|
||||
}
|
||||
|
||||
for gid in glyph_ids:
|
||||
data = glyph_data.get(gid, {})
|
||||
stype = data.get("specialized_type", "")
|
||||
if stype == "aether_node" or gid == "G001":
|
||||
tiered["aether"].append((gid, data))
|
||||
elif stype == "monument_grade_equilibrium":
|
||||
tiered["monument"].append((gid, data))
|
||||
else:
|
||||
tiered["standard"].append((gid, data))
|
||||
|
||||
def sort_key(item: Tuple[str, Dict[str, Any]]) -> Tuple[float, str]:
|
||||
gid, data = item
|
||||
priority = float(data.get("priority", 1))
|
||||
band = data.get("band", "C")
|
||||
band_order = {"A": 0, "B": 1, "C": 2, "D": 3}.get(band, 4)
|
||||
return (-priority, band_order, gid)
|
||||
|
||||
for tier_name in tiered:
|
||||
tiered[tier_name].sort(key=sort_key)
|
||||
|
||||
regions: List[MemoryRegion] = []
|
||||
cursor = RESERVED_BASE
|
||||
|
||||
reserved_region = MemoryRegion(
|
||||
glyph_id="__reserved__",
|
||||
base=cursor,
|
||||
size=AETHER_NODE_BASE - RESERVED_BASE,
|
||||
band="",
|
||||
priority=0,
|
||||
label="System reserved",
|
||||
type="reserved",
|
||||
permissions="r",
|
||||
)
|
||||
regions.append(reserved_region)
|
||||
cursor = AETHER_NODE_BASE
|
||||
|
||||
for gid, data in tiered["aether"]:
|
||||
region = cls._allocate_region(gid, data, cursor, "aether")
|
||||
regions.append(region)
|
||||
cursor = region.base + region.size
|
||||
|
||||
cursor = max(cursor, MONUMENT_BASE)
|
||||
|
||||
for gid, data in tiered["monument"]:
|
||||
region = cls._allocate_region(gid, data, cursor, "monument")
|
||||
regions.append(region)
|
||||
cursor = region.base + region.size
|
||||
|
||||
cursor = max(cursor, STANDARD_BASE)
|
||||
|
||||
for gid, data in tiered["standard"]:
|
||||
region = cls._allocate_region(gid, data, cursor, "standard")
|
||||
regions.append(region)
|
||||
cursor = region.base + region.size
|
||||
|
||||
cursor = max(cursor, STACK_BASE)
|
||||
stack_region = MemoryRegion(
|
||||
glyph_id="__stack__",
|
||||
base=cursor,
|
||||
size=MAX_ADDRESS - cursor + 1,
|
||||
band="",
|
||||
priority=0,
|
||||
label="Execution stack",
|
||||
type="stack",
|
||||
permissions="rw",
|
||||
)
|
||||
regions.append(stack_region)
|
||||
|
||||
glyph_map: Dict[str, MemoryRegion] = {}
|
||||
for r in regions:
|
||||
if not r.glyph_id.startswith("__"):
|
||||
glyph_map[r.glyph_id] = r
|
||||
|
||||
return cls(regions=regions, glyph_map=glyph_map, total_size=MAX_ADDRESS + 1)
|
||||
|
||||
def get_offset(self, glyph_id: str) -> Optional[int]:
|
||||
"""Get the base address for a glyph.
|
||||
|
||||
Args:
|
||||
glyph_id: The glyph to look up.
|
||||
|
||||
Returns:
|
||||
Base address as int, or None if glyph not in layout.
|
||||
"""
|
||||
region = self.glyph_map.get(glyph_id)
|
||||
if region:
|
||||
return region.base
|
||||
return None
|
||||
|
||||
def get_region(self, glyph_id: str) -> Optional[MemoryRegion]:
|
||||
"""Get the full region descriptor for a glyph."""
|
||||
return self.glyph_map.get(glyph_id)
|
||||
|
||||
def get_region_for_address(self, address: int) -> Optional[MemoryRegion]:
|
||||
"""Find which region an address falls in.
|
||||
|
||||
Args:
|
||||
address: 16-bit address.
|
||||
|
||||
Returns:
|
||||
MemoryRegion containing the address, or None.
|
||||
"""
|
||||
for region in self.regions:
|
||||
if region.base <= address < region.base + region.size:
|
||||
return region
|
||||
return None
|
||||
|
||||
def map_segments(
|
||||
self,
|
||||
segments: List[Dict[str, Any]],
|
||||
) -> List[Dict[str, int]]:
|
||||
"""Map code segments to concrete addresses in the layout.
|
||||
|
||||
Each segment gets assigned to the region of its associated glyph
|
||||
(or the first available region if no glyph match).
|
||||
|
||||
Args:
|
||||
segments: List of segment dicts with keys: id, glyph_id, size.
|
||||
|
||||
Returns:
|
||||
List of segment mappings: {segment_id, glyph_id, address, size}.
|
||||
"""
|
||||
mappings: List[Dict[str, int]] = []
|
||||
region_cursors: Dict[str, int] = {}
|
||||
|
||||
for seg in segments:
|
||||
seg_id = seg.get("id", "unknown")
|
||||
gid = seg.get("glyph_id", "")
|
||||
seg_size = seg.get("size", PAGE_SIZE)
|
||||
|
||||
region = self.glyph_map.get(gid)
|
||||
if region is None:
|
||||
region = self.regions[0] if self.regions else None
|
||||
if region is None:
|
||||
continue
|
||||
|
||||
if gid not in region_cursors:
|
||||
region_cursors[gid] = region.base
|
||||
cursor = region_cursors[gid]
|
||||
|
||||
max_size = region.size - (cursor - region.base)
|
||||
actual_size = min(seg_size, max_size)
|
||||
|
||||
mappings.append({
|
||||
"segment_id": seg_id,
|
||||
"glyph_id": gid,
|
||||
"address": cursor,
|
||||
"size": actual_size,
|
||||
})
|
||||
|
||||
region_cursors[gid] = cursor + actual_size
|
||||
|
||||
return mappings
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Serialize layout to a dict for telemetry or inspection."""
|
||||
return {
|
||||
"total_size": self.total_size,
|
||||
"region_count": len(self.regions),
|
||||
"glyph_count": len(self.glyph_map),
|
||||
"regions": [
|
||||
{
|
||||
"glyph_id": r.glyph_id,
|
||||
"base": f"0x{r.base:04X}",
|
||||
"size": r.size,
|
||||
"band": r.band,
|
||||
"type": r.type,
|
||||
"permissions": r.permissions,
|
||||
"label": r.label,
|
||||
}
|
||||
for r in self.regions
|
||||
],
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _allocate_region(
|
||||
glyph_id: str,
|
||||
data: Dict[str, Any],
|
||||
base: int,
|
||||
tier: str,
|
||||
) -> MemoryRegion:
|
||||
"""Allocate a region for a single glyph."""
|
||||
band = data.get("band", "C") if tier != "aether" else "A"
|
||||
priority = float(data.get("priority", 1))
|
||||
score = float(data.get("score", 100))
|
||||
|
||||
band_mult = BAND_SIZE_MULTIPLIERS.get(band, 4)
|
||||
size = PAGE_SIZE * band_mult
|
||||
|
||||
if tier == "aether":
|
||||
size = PAGE_SIZE * 32
|
||||
|
||||
name = data.get("name", glyph_id)
|
||||
label = f"[{tier}] {name} ({glyph_id})"
|
||||
|
||||
return MemoryRegion(
|
||||
glyph_id=glyph_id,
|
||||
base=base,
|
||||
size=size,
|
||||
band=band,
|
||||
priority=priority,
|
||||
label=label,
|
||||
type="code",
|
||||
permissions="rx",
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _load_glyph_data(
|
||||
glyph_ids: List[str],
|
||||
) -> Dict[str, Dict[str, Any]]:
|
||||
"""Load glyph data from the super_registry."""
|
||||
try:
|
||||
from glyphs.super_registry import get_super
|
||||
result: Dict[str, Dict[str, Any]] = {}
|
||||
for gid in glyph_ids:
|
||||
glyph = get_super(gid)
|
||||
if glyph:
|
||||
result[gid] = glyph
|
||||
else:
|
||||
result[gid] = {"name": gid, "priority": 1, "band": "C", "score": 50}
|
||||
return result
|
||||
except ImportError:
|
||||
logger.warning("[GAML] super_registry not available, using defaults")
|
||||
return {}
|
||||
|
||||
|
||||
def build_layout(
|
||||
glyph_ids: List[str],
|
||||
glyph_data: Optional[Dict[str, Any]] = None,
|
||||
) -> GlyphAlignedMemoryLayout:
|
||||
"""Convenience: build a layout for the given glyph IDs."""
|
||||
return GlyphAlignedMemoryLayout.build(glyph_ids, glyph_data)
|
||||
|
||||
|
||||
def get_glyph_address(
|
||||
layout: GlyphAlignedMemoryLayout,
|
||||
glyph_id: str,
|
||||
) -> Optional[int]:
|
||||
"""Get a glyph's base address from the layout."""
|
||||
return layout.get_offset(glyph_id)
|
||||
Executable
+324
@@ -0,0 +1,324 @@
|
||||
"""
|
||||
SEE — Symbolic Execution Envelope
|
||||
|
||||
Wraps decompressed GX code in a symbolic context envelope that bridges
|
||||
the XIC virtual machine with the LAIN 8-lane cognition engine.
|
||||
|
||||
The envelope serves as an immutable container that carries:
|
||||
- Decompressed code bytes + manifest
|
||||
- Glyph context (resonance data, superpowers, specialized types)
|
||||
- Execution metadata (mode, epoch, invocation chain)
|
||||
- Integrity hash for verification
|
||||
|
||||
Integration points:
|
||||
- XIC VM (xic_vm.py): run_xic_program consumes SEE envelopes
|
||||
- LAIN runtime (gx_lain/runtime.py): execute_with_lain works within envelopes
|
||||
- Symbolic pipeline (glyphos/symbolic_pipeline.py): run_symbolic_pipeline feeds envelopes
|
||||
- GSZ3 decompressor (xic_extensions/gsz3_decompressor.py): decompresses payloads
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
import uuid
|
||||
import logging
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class SymbolicExecutionEnvelope:
|
||||
"""
|
||||
Immutable envelope wrapping decompressed code with symbolic cognition context.
|
||||
|
||||
Once constructed via build(), the envelope is read-only — LAIN and the XIC VM
|
||||
consume it without mutation. This guarantees deterministic execution.
|
||||
"""
|
||||
|
||||
code: bytes
|
||||
manifest: Dict[str, Any]
|
||||
glyph_context: Dict[str, Any]
|
||||
glyph_ids: List[str]
|
||||
resonance_map: Dict[str, float]
|
||||
mode: str
|
||||
epoch: Optional[str]
|
||||
invocation_id: str
|
||||
chain_label: Optional[str]
|
||||
integrity_hash: str
|
||||
built_at: float
|
||||
metadata: Dict[str, Any] = field(default_factory=dict)
|
||||
|
||||
@classmethod
|
||||
def build(
|
||||
cls,
|
||||
code: bytes,
|
||||
manifest: Optional[Dict[str, Any]] = None,
|
||||
glyph_context: Optional[Dict[str, Any]] = None,
|
||||
glyph_ids: Optional[List[str]] = None,
|
||||
mode: str = "symbolic",
|
||||
epoch: Optional[str] = None,
|
||||
chain_label: Optional[str] = None,
|
||||
metadata: Optional[Dict[str, Any]] = None,
|
||||
) -> "SymbolicExecutionEnvelope":
|
||||
"""
|
||||
Construct a new envelope from raw components.
|
||||
|
||||
Args:
|
||||
code: Decompressed code bytes.
|
||||
manifest: Optional GX manifest dict.
|
||||
glyph_context: Optional glyph cognition context.
|
||||
glyph_ids: Optional list of glyph IDs for multi-glyph resonance.
|
||||
mode: Execution mode ("symbolic", "analyze", "execute").
|
||||
epoch: Optional epoch identifier for time-aligned execution.
|
||||
chain_label: Optional chain label for jump-table routing.
|
||||
metadata: Optional extra metadata to embed.
|
||||
|
||||
Returns:
|
||||
Fully constructed SymbolicExecutionEnvelope.
|
||||
"""
|
||||
if manifest is None:
|
||||
manifest = {}
|
||||
if glyph_context is None:
|
||||
glyph_context = {}
|
||||
if glyph_ids is None:
|
||||
glyph_ids = []
|
||||
if metadata is None:
|
||||
metadata = {}
|
||||
|
||||
glyph_resonance = cls._compute_glyph_resonance_map(glyph_context, glyph_ids)
|
||||
|
||||
payload = {
|
||||
"code_len": len(code),
|
||||
"manifest_version": manifest.get("version", "unknown"),
|
||||
"glyph_ids": glyph_ids,
|
||||
"mode": mode,
|
||||
}
|
||||
integrity_hash = cls._hash_envelope(code, payload)
|
||||
|
||||
return cls(
|
||||
code=code,
|
||||
manifest=manifest,
|
||||
glyph_context=glyph_context,
|
||||
glyph_ids=glyph_ids,
|
||||
resonance_map=glyph_resonance,
|
||||
mode=mode,
|
||||
epoch=epoch,
|
||||
invocation_id=metadata.get("invocation_id", str(uuid.uuid4())),
|
||||
chain_label=chain_label,
|
||||
integrity_hash=integrity_hash,
|
||||
built_at=time.time(),
|
||||
metadata=metadata,
|
||||
)
|
||||
|
||||
def verify_integrity(self) -> bool:
|
||||
"""Verify the envelope's integrity hash matches its contents."""
|
||||
payload = {
|
||||
"code_len": len(self.code),
|
||||
"manifest_version": self.manifest.get("version", "unknown"),
|
||||
"glyph_ids": self.glyph_ids,
|
||||
"mode": self.mode,
|
||||
}
|
||||
expected = self._hash_envelope(self.code, payload)
|
||||
return expected == self.integrity_hash
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Serialize envelope to a dict (for telemetry, logging, transport)."""
|
||||
return {
|
||||
"code_size": len(self.code),
|
||||
"code_preview": self.code[:120].decode("utf-8", errors="replace"),
|
||||
"manifest_version": self.manifest.get("version", ""),
|
||||
"glyph_ids": self.glyph_ids,
|
||||
"glyph_count": len(self.glyph_ids),
|
||||
"resonance": self.resonance_map,
|
||||
"mode": self.mode,
|
||||
"epoch": self.epoch,
|
||||
"invocation_id": self.invocation_id,
|
||||
"chain_label": self.chain_label,
|
||||
"integrity_hash": self.integrity_hash,
|
||||
"built_at": self.built_at,
|
||||
}
|
||||
|
||||
def resolve_glyph_context(
|
||||
self, glyph_id: str
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
"""Resolve a single glyph's context data from the envelope.
|
||||
|
||||
Args:
|
||||
glyph_id: The glyph identifier to look up.
|
||||
|
||||
Returns:
|
||||
Glyph context dict or None if not found.
|
||||
"""
|
||||
glyph_data = self.glyph_context.get(glyph_id)
|
||||
if glyph_data:
|
||||
return {
|
||||
"glyph_id": glyph_id,
|
||||
"data": glyph_data,
|
||||
"resonance_weight": self.resonance_map.get(glyph_id, 0.0),
|
||||
}
|
||||
raw_glyphs = self.glyph_context.get("glyphs", {})
|
||||
glyph_data = raw_glyphs.get(glyph_id)
|
||||
if glyph_data:
|
||||
return {
|
||||
"glyph_id": glyph_id,
|
||||
"data": glyph_data,
|
||||
"resonance_weight": self.resonance_map.get(glyph_id, 0.0),
|
||||
}
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _compute_glyph_resonance_map(
|
||||
glyph_context: Dict[str, Any],
|
||||
glyph_ids: List[str],
|
||||
) -> Dict[str, float]:
|
||||
"""Compute a flat glyph_id → resonance_weight map.
|
||||
|
||||
Extracts weights from glyph_context and supplements with
|
||||
even distribution for glyph_ids missing explicit weights.
|
||||
"""
|
||||
resonance: Dict[str, float] = {}
|
||||
|
||||
raw_glyphs: Dict[str, Any] = glyph_context.get("glyphs", {})
|
||||
for gid, data in raw_glyphs.items():
|
||||
if isinstance(data, dict):
|
||||
weight = data.get("resonance_weight") or data.get("weight") or data.get("score", 0)
|
||||
resonance[gid] = float(weight)
|
||||
|
||||
for gid in glyph_ids:
|
||||
if gid not in resonance:
|
||||
direct = glyph_context.get(gid)
|
||||
if isinstance(direct, dict):
|
||||
weight = direct.get("resonance_weight") or direct.get("weight") or direct.get("score", 0)
|
||||
resonance[gid] = float(weight)
|
||||
else:
|
||||
resonance[gid] = 0.0
|
||||
|
||||
if resonance and not any(v > 0 for v in resonance.values()):
|
||||
fallback = 1.0 / max(len(resonance), 1)
|
||||
for gid in resonance:
|
||||
resonance[gid] = fallback
|
||||
|
||||
return resonance
|
||||
|
||||
@staticmethod
|
||||
def _hash_envelope(code: bytes, payload: Dict[str, Any]) -> str:
|
||||
"""SHA-256 integrity hash covering code + metadata."""
|
||||
hasher = hashlib.sha256()
|
||||
hasher.update(code)
|
||||
hasher.update(json.dumps(payload, sort_keys=True).encode())
|
||||
return hasher.hexdigest()[:32]
|
||||
|
||||
|
||||
def wrap_code(
|
||||
code_bytes: bytes,
|
||||
glyph_ids: Optional[List[str]] = None,
|
||||
mode: str = "symbolic",
|
||||
manifest: Optional[Dict[str, Any]] = None,
|
||||
glyph_context: Optional[Dict[str, Any]] = None,
|
||||
chain_label: Optional[str] = None,
|
||||
) -> SymbolicExecutionEnvelope:
|
||||
"""Convenience function: wrap raw decompressed code in an envelope.
|
||||
|
||||
Args:
|
||||
code_bytes: Decompressed code bytes.
|
||||
glyph_ids: Optional glyph IDs for resonance.
|
||||
mode: Execution mode.
|
||||
manifest: Optional manifest dict.
|
||||
glyph_context: Optional glyph cognition context.
|
||||
chain_label: Optional chain label.
|
||||
|
||||
Returns:
|
||||
SymbolicExecutionEnvelope ready for execution.
|
||||
"""
|
||||
return SymbolicExecutionEnvelope.build(
|
||||
code=code_bytes,
|
||||
manifest=manifest,
|
||||
glyph_context=glyph_context,
|
||||
glyph_ids=glyph_ids,
|
||||
mode=mode,
|
||||
chain_label=chain_label,
|
||||
)
|
||||
|
||||
|
||||
def unwrap_envelope(
|
||||
envelope: SymbolicExecutionEnvelope,
|
||||
) -> Tuple[bytes, Dict[str, Any], List[str]]:
|
||||
"""Extract the core execution components from an envelope.
|
||||
|
||||
Returns (code_bytes, context_dict, glyph_ids).
|
||||
|
||||
The context dict includes mode, epoch, invocation_id, chain_label,
|
||||
and the full resonance map for symbolic processing.
|
||||
"""
|
||||
context = {
|
||||
"mode": envelope.mode,
|
||||
"epoch": envelope.epoch,
|
||||
"invocation_id": envelope.invocation_id,
|
||||
"chain_label": envelope.chain_label,
|
||||
"resonance_map": envelope.resonance_map,
|
||||
"manifest": envelope.manifest,
|
||||
"glyph_context": envelope.glyph_context,
|
||||
}
|
||||
return envelope.code, context, envelope.glyph_ids
|
||||
|
||||
|
||||
def execute_with_envelope(
|
||||
envelope: SymbolicExecutionEnvelope,
|
||||
) -> Dict[str, Any]:
|
||||
"""Execute decompressed code through the full symbolic pipeline within the envelope.
|
||||
|
||||
Pipeline:
|
||||
1. Verify envelope integrity
|
||||
2. Unwrap code + context
|
||||
3. Route through run_symbolic_pipeline with glyph data
|
||||
4. Return structured result
|
||||
|
||||
Args:
|
||||
envelope: The execution envelope.
|
||||
|
||||
Returns:
|
||||
Dict with keys: output_text, fused_symbol, steps, diagnostics.
|
||||
"""
|
||||
if not envelope.verify_integrity():
|
||||
return {
|
||||
"output_text": "[SEE] Integrity verification failed — envelope tampered",
|
||||
"fused_symbol": None,
|
||||
"steps": [],
|
||||
"diagnostics": {"error": "integrity_check_failed"},
|
||||
}
|
||||
|
||||
code, context, glyph_ids = unwrap_envelope(envelope)
|
||||
|
||||
prompt = code.decode("utf-8", errors="replace")
|
||||
|
||||
try:
|
||||
from glyphos.symbolic_pipeline import run_symbolic_pipeline
|
||||
|
||||
result = run_symbolic_pipeline(
|
||||
prompt=prompt,
|
||||
context=context,
|
||||
glyph_ids=glyph_ids or None,
|
||||
)
|
||||
|
||||
return {
|
||||
"output_text": result.output_text,
|
||||
"fused_symbol": result.fused_symbol,
|
||||
"steps": result.steps,
|
||||
"diagnostics": {
|
||||
"step_count": len(result.steps),
|
||||
"mode": envelope.mode,
|
||||
"integrity": "verified",
|
||||
},
|
||||
}
|
||||
except Exception as e:
|
||||
logger.exception(f"[SEE] Pipeline execution failed: {e}")
|
||||
return {
|
||||
"output_text": f"[SEE] Execution error: {e}",
|
||||
"fused_symbol": None,
|
||||
"steps": [],
|
||||
"diagnostics": {"error": str(e)},
|
||||
}
|
||||
Executable
Executable
+156
@@ -0,0 +1,156 @@
|
||||
"""
|
||||
Tests for GAML — Glyph-Aligned Memory Layout.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from execute_compressed.gaml import (
|
||||
GlyphAlignedMemoryLayout,
|
||||
MemoryRegion,
|
||||
build_layout,
|
||||
get_glyph_address,
|
||||
PAGE_SIZE,
|
||||
)
|
||||
|
||||
passed = 0
|
||||
failed = 0
|
||||
|
||||
|
||||
def test(name: str, ok: bool):
|
||||
global passed, failed
|
||||
if ok:
|
||||
passed += 1
|
||||
print(f" ✅ PASS: {name}")
|
||||
else:
|
||||
failed += 1
|
||||
print(f" ❌ FAIL: {name}")
|
||||
|
||||
|
||||
print("=" * 60)
|
||||
print("GAML — Glyph-Aligned Memory Layout Tests")
|
||||
print("=" * 60)
|
||||
|
||||
# Test 1: Build layout with single glyph
|
||||
layout = GlyphAlignedMemoryLayout.build(["G001"])
|
||||
test("Layout builds with G001", len(layout.regions) > 0)
|
||||
test("Layout has glyph_map", "G001" in layout.glyph_map)
|
||||
test("Layout total_size is 65536", layout.total_size == 65536)
|
||||
|
||||
# Test 2: G001 gets aether tier placement
|
||||
g001_region = layout.get_region("G001")
|
||||
test("G001 region exists", g001_region is not None)
|
||||
test("G001 base is AETHER_NODE_BASE (0x0100)",
|
||||
g001_region is not None and g001_region.base == 0x0100)
|
||||
test("G001 has rx permissions",
|
||||
g001_region is not None and g001_region.permissions == "rx")
|
||||
|
||||
# Test 3: Layout with standard glyphs
|
||||
layout2 = GlyphAlignedMemoryLayout.build(["G015", "G042", "G100"])
|
||||
test("Layout with standard glyphs", len(layout2.glyph_map) == 3)
|
||||
test("Standard glyphs at >= STANDARD_BASE",
|
||||
all(r.base >= 0x4000 for gid, r in layout2.glyph_map.items()))
|
||||
|
||||
# Test 4: Mixed tiers
|
||||
layout3 = GlyphAlignedMemoryLayout.build(["G001", "G050", "G200"])
|
||||
test("Mixed tier layout", "G001" in layout3.glyph_map)
|
||||
test("G050 in layout", "G050" in layout3.glyph_map)
|
||||
test("G200 in layout", "G200" in layout3.glyph_map)
|
||||
g001 = layout3.get_region("G001")
|
||||
g050 = layout3.get_region("G050")
|
||||
g200 = layout3.get_region("G200")
|
||||
test("G001 before G050",
|
||||
g001 is not None and g050 is not None and g001.base < g050.base)
|
||||
test("G050 before G200",
|
||||
g050 is not None and g200 is not None and g050.base < g200.base)
|
||||
|
||||
# Test 5: get_offset
|
||||
offset = layout3.get_offset("G001")
|
||||
test("get_offset returns int for G001", isinstance(offset, int))
|
||||
test("get_offset returns None for unknown", layout3.get_offset("G999") is None)
|
||||
|
||||
# Test 6: get_region_for_address
|
||||
reserved = layout3.get_region_for_address(0x0050)
|
||||
test("Address 0x0050 is in reserved region",
|
||||
reserved is not None and reserved.glyph_id == "__reserved__")
|
||||
|
||||
g001_region_check = layout3.get_region_for_address(0x0100)
|
||||
test("Address 0x0100 is in G001 region",
|
||||
g001_region_check is not None and g001_region_check.glyph_id == "G001")
|
||||
|
||||
stack = layout3.get_region_for_address(0xF000)
|
||||
test("Address 0xF000 is in stack region",
|
||||
stack is not None and stack.glyph_id == "__stack__")
|
||||
|
||||
# Test 7: map_segments
|
||||
segments_data = [
|
||||
{"id": "seg_0", "glyph_id": "G001", "size": 512},
|
||||
{"id": "seg_1", "glyph_id": "G050", "size": 256},
|
||||
{"id": "seg_2", "glyph_id": "G200", "size": 128},
|
||||
]
|
||||
mappings = layout3.map_segments(segments_data)
|
||||
test("map_segments returns all segments", len(mappings) == 3)
|
||||
test("Segment seg_0 maps to G001 region",
|
||||
mappings[0]["glyph_id"] == "G001" and mappings[0]["address"] == 0x0100)
|
||||
test("Segment seg_1 maps to G050 region",
|
||||
mappings[1]["glyph_id"] == "G050")
|
||||
test("Segment addresses are in order",
|
||||
mappings[0]["address"] < mappings[1]["address"] < mappings[2]["address"])
|
||||
|
||||
# Test 8: map_segments respects region bounds
|
||||
segments_big = [
|
||||
{"id": "seg_big", "glyph_id": "G001", "size": 100000},
|
||||
]
|
||||
mappings_big = layout3.map_segments(segments_big)
|
||||
test("map_segments caps size to region max",
|
||||
mappings_big[0]["size"] <= g001_region.size if g001_region else False)
|
||||
|
||||
# Test 9: Determinism — same input = same output
|
||||
layout4a = GlyphAlignedMemoryLayout.build(["G001", "G015", "G042"])
|
||||
layout4b = GlyphAlignedMemoryLayout.build(["G001", "G015", "G042"])
|
||||
test("Deterministic layout: same region count",
|
||||
len(layout4a.regions) == len(layout4b.regions))
|
||||
test("Deterministic layout: same addresses",
|
||||
all(
|
||||
r1.base == r2.base and r1.size == r2.size
|
||||
for r1, r2 in zip(layout4a.regions, layout4b.regions)
|
||||
))
|
||||
|
||||
# Test 10: build_layout convenience function
|
||||
layout5 = build_layout(["G001"])
|
||||
test("build_layout returns GlyphAlignedMemoryLayout",
|
||||
isinstance(layout5, GlyphAlignedMemoryLayout))
|
||||
|
||||
# Test 11: get_glyph_address convenience function
|
||||
addr = get_glyph_address(layout5, "G001")
|
||||
test("get_glyph_address returns int", isinstance(addr, int))
|
||||
|
||||
# Test 12: to_dict serialization
|
||||
d = layout5.to_dict()
|
||||
test("to_dict has total_size", d["total_size"] == 65536)
|
||||
test("to_dict has region_count", d["region_count"] > 0)
|
||||
test("to_dict has glyph_count", d["glyph_count"] > 0)
|
||||
test("to_dict regions have hex base",
|
||||
all(r["base"].startswith("0x") for r in d["regions"]))
|
||||
|
||||
# Test 13: With explicit glyph_data override
|
||||
custom_data = {
|
||||
"G001": {"name": "Ledo", "priority": 10, "band": "A", "score": 300,
|
||||
"specialized_type": "aether_node"},
|
||||
"G050": {"name": "TestGlyph", "priority": 5, "band": "B", "score": 150},
|
||||
}
|
||||
layout6 = GlyphAlignedMemoryLayout.build(["G001", "G050"], glyph_data=custom_data)
|
||||
test("Custom glyph_data layout", "G001" in layout6.glyph_map)
|
||||
test("G001 has large size from aether tier",
|
||||
layout6.get_region("G001").size == PAGE_SIZE * 32)
|
||||
|
||||
# Summary
|
||||
print()
|
||||
print("=" * 60)
|
||||
print(f"Results: {passed} passed, {failed} failed, {passed + failed} total")
|
||||
if failed == 0:
|
||||
print("✅ ALL GAML TESTS PASSED")
|
||||
else:
|
||||
print(f"❌ {failed} TEST(S) FAILED")
|
||||
sys.exit(0 if failed == 0 else 1)
|
||||
Executable
+155
@@ -0,0 +1,155 @@
|
||||
"""
|
||||
Tests for SEE — Symbolic Execution Envelope.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from execute_compressed.see import (
|
||||
SymbolicExecutionEnvelope,
|
||||
wrap_code,
|
||||
unwrap_envelope,
|
||||
execute_with_envelope,
|
||||
)
|
||||
|
||||
passed = 0
|
||||
failed = 0
|
||||
|
||||
|
||||
def test(name: str, ok: bool):
|
||||
global passed, failed
|
||||
if ok:
|
||||
passed += 1
|
||||
print(f" ✅ PASS: {name}")
|
||||
else:
|
||||
failed += 1
|
||||
print(f" ❌ FAIL: {name}")
|
||||
|
||||
|
||||
print("=" * 60)
|
||||
print("SEE — Symbolic Execution Envelope Tests")
|
||||
print("=" * 60)
|
||||
|
||||
# Test 1: Build envelope
|
||||
code = b"print('hello world')"
|
||||
envelope = SymbolicExecutionEnvelope.build(code=code, glyph_ids=["G001"])
|
||||
test("Build envelope", envelope.code == code)
|
||||
test("Envelope has glyph_ids", envelope.glyph_ids == ["G001"])
|
||||
test("Envelope has integrity_hash", len(envelope.integrity_hash) == 32)
|
||||
test("Envelope has invocation_id", len(envelope.invocation_id) > 0)
|
||||
test("Envelope built_at > 0", envelope.built_at > 0)
|
||||
|
||||
# Test 2: Default values
|
||||
env2 = SymbolicExecutionEnvelope.build(code=b"test")
|
||||
test("Default glyph_ids is empty list", env2.glyph_ids == [])
|
||||
test("Default manifest is empty dict", env2.manifest == {})
|
||||
test("Default mode is symbolic", env2.mode == "symbolic")
|
||||
|
||||
# Test 3: Integrity verification
|
||||
test("Integrity passes for unmodified envelope", envelope.verify_integrity())
|
||||
env2.code = b"tampered"
|
||||
test("Integrity fails for tampered envelope", not env2.verify_integrity())
|
||||
|
||||
# Test 4: Resonance map from glyph_context top-level keys
|
||||
env3 = SymbolicExecutionEnvelope.build(
|
||||
code=b"test",
|
||||
glyph_ids=["G001"],
|
||||
glyph_context={"G001": {"resonance_weight": 0.85}},
|
||||
)
|
||||
test("Resonance map has G001", "G001" in env3.resonance_map)
|
||||
test("Resonance weight from top-level context", abs(env3.resonance_map["G001"] - 0.85) < 0.001)
|
||||
|
||||
# Test 4b: Resonance map from nested glyphs key
|
||||
env3b = SymbolicExecutionEnvelope.build(
|
||||
code=b"test",
|
||||
glyph_ids=["G001"],
|
||||
glyph_context={"glyphs": {"G001": {"resonance_weight": 0.75}}},
|
||||
)
|
||||
test("Resonance map from nested glyphs", "G001" in env3b.resonance_map)
|
||||
test("Resonance weight from nested", abs(env3b.resonance_map["G001"] - 0.75) < 0.001)
|
||||
|
||||
# Test 5: wrap_code convenience
|
||||
env4 = wrap_code(b"hello", glyph_ids=["G015", "G042"])
|
||||
test("wrap_code returns SymbolicExecutionEnvelope", isinstance(env4, SymbolicExecutionEnvelope))
|
||||
test("wrap_code preserves code", env4.code == b"hello")
|
||||
test("wrap_code preserves glyph_ids", env4.glyph_ids == ["G015", "G042"])
|
||||
|
||||
# Test 6: unwrap_envelope
|
||||
code_out, context_out, glyph_ids_out = unwrap_envelope(envelope)
|
||||
test("unwrap returns code bytes", code_out == b"print('hello world')")
|
||||
test("unwrap returns glyph_ids", glyph_ids_out == ["G001"])
|
||||
test("unwrap context has mode", context_out["mode"] == "symbolic")
|
||||
test("unwrap context has resonance_map", "resonance_map" in context_out)
|
||||
|
||||
# Test 7: resolve_glyph_context
|
||||
test("resolve_glyph_context returns None when no context set",
|
||||
envelope.resolve_glyph_context("G001") is None)
|
||||
test("resolve_glyph_context returns None for unknown",
|
||||
envelope.resolve_glyph_context("G999") is None)
|
||||
# Test with explicit glyph context
|
||||
ctx_with_data = env3.resolve_glyph_context("G001")
|
||||
test("resolve_glyph_context with glyph_context data",
|
||||
ctx_with_data is not None and ctx_with_data["glyph_id"] == "G001")
|
||||
|
||||
# Test 8: Glyph context from nested structure
|
||||
env5 = SymbolicExecutionEnvelope.build(
|
||||
code=b"test",
|
||||
glyph_ids=["G001"],
|
||||
glyph_context={
|
||||
"glyphs": {
|
||||
"G001": {"resonance_weight": 0.9, "name": "Ledo"},
|
||||
}
|
||||
},
|
||||
)
|
||||
resolved = env5.resolve_glyph_context("G001")
|
||||
test("resolve_glyph_context works with nested glyphs",
|
||||
resolved is not None and resolved["glyph_id"] == "G001")
|
||||
|
||||
# Test 9: to_dict serialization
|
||||
d = envelope.to_dict()
|
||||
test("to_dict has code_size", d["code_size"] == len(code))
|
||||
test("to_dict has glyph_ids", d["glyph_ids"] == ["G001"])
|
||||
test("to_dict has integrity_hash", d["integrity_hash"] == envelope.integrity_hash)
|
||||
|
||||
# Test 10: execute_with_envelope - integrity failure
|
||||
tampered = SymbolicExecutionEnvelope(
|
||||
code=b"tampered",
|
||||
manifest={},
|
||||
glyph_context={},
|
||||
glyph_ids=[],
|
||||
resonance_map={},
|
||||
mode="symbolic",
|
||||
epoch=None,
|
||||
invocation_id="bad",
|
||||
chain_label=None,
|
||||
integrity_hash="00000000000000000000000000000000",
|
||||
built_at=0.0,
|
||||
metadata={},
|
||||
)
|
||||
result = execute_with_envelope(tampered)
|
||||
test("execute_with_envelope detects tampering",
|
||||
"integrity" in result.get("diagnostics", {}).get("error", ""))
|
||||
|
||||
# Test 11: execute_with_envelope with valid code
|
||||
try:
|
||||
env_valid = SymbolicExecutionEnvelope.build(
|
||||
code=b"Hello from SEE envelope test",
|
||||
glyph_ids=["G001"],
|
||||
metadata={"invocation_id": "test-001"},
|
||||
)
|
||||
result = execute_with_envelope(env_valid)
|
||||
has_output = bool(result.get("output_text"))
|
||||
test("execute_with_envelope returns output", has_output)
|
||||
except Exception as e:
|
||||
test(f"execute_with_envelope did not crash ({e})", False)
|
||||
|
||||
# Summary
|
||||
print()
|
||||
print("=" * 60)
|
||||
print(f"Results: {passed} passed, {failed} failed, {passed + failed} total")
|
||||
if failed == 0:
|
||||
print("✅ ALL SEE TESTS PASSED")
|
||||
else:
|
||||
print(f"❌ {failed} TEST(S) FAILED")
|
||||
sys.exit(0 if failed == 0 else 1)
|
||||
Reference in New Issue
Block a user