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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
OverLanguage 2.0 — Glyph-Native Meta-Language for AI Production
|
| 3 |
+
===============================================================
|
| 4 |
+
Program = instructions for machines.
|
| 5 |
+
OverProgram = instructions for production reality.
|
| 6 |
+
|
| 7 |
+
Root glyph: ⧉◇@L → H@L Æ R Æ λ⁻¹ = ◎ → $
|
| 8 |
+
|
| 9 |
+
Seven layers:
|
| 10 |
+
L0: Glyph — compressed symbolic substrate
|
| 11 |
+
L1: Intent — human-level objective
|
| 12 |
+
L2: Contract — enforceable requirements
|
| 13 |
+
L3: Agent — production operators
|
| 14 |
+
L4: Substrate — latent compute capture
|
| 15 |
+
L5: Receipt — proof binding
|
| 16 |
+
L6: Transfer — lambda friction / transferability
|
| 17 |
+
L7: Economic — buyer / value / price
|
| 18 |
+
|
| 19 |
+
Compiler passes:
|
| 20 |
+
parse → expand → contract → assign → execute → capture → hash → receipt → score → package
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
import json
|
| 24 |
+
import time
|
| 25 |
+
import hashlib
|
| 26 |
+
import re
|
| 27 |
+
import os
|
| 28 |
+
from dataclasses import dataclass, field, asdict
|
| 29 |
+
from typing import Optional
|
| 30 |
+
from pathlib import Path
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@dataclass
|
| 34 |
+
class OverProgram:
|
| 35 |
+
name: str = ""
|
| 36 |
+
intent: str = ""
|
| 37 |
+
object_desc: str = ""
|
| 38 |
+
anchor: str = ""
|
| 39 |
+
capture: list = field(default_factory=list)
|
| 40 |
+
prove: list = field(default_factory=list)
|
| 41 |
+
score: dict = field(default_factory=dict)
|
| 42 |
+
output: list = field(default_factory=list)
|
| 43 |
+
success: str = ""
|
| 44 |
+
economic: dict = field(default_factory=dict)
|
| 45 |
+
agents: dict = field(default_factory=dict)
|
| 46 |
+
raw: str = ""
|
| 47 |
+
|
| 48 |
+
def to_dict(self) -> dict:
|
| 49 |
+
return asdict(self)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
@dataclass
|
| 53 |
+
class CompilationResult:
|
| 54 |
+
program: dict = field(default_factory=dict)
|
| 55 |
+
build_plan: dict = field(default_factory=dict)
|
| 56 |
+
artifact_manifest: dict = field(default_factory=dict)
|
| 57 |
+
receipt: dict = field(default_factory=dict)
|
| 58 |
+
lambda_score: dict = field(default_factory=dict)
|
| 59 |
+
buyer_packet: dict = field(default_factory=dict)
|
| 60 |
+
glyph: str = ""
|
| 61 |
+
compiled_at: float = 0.0
|
| 62 |
+
status: str = "compiled"
|
| 63 |
+
|
| 64 |
+
def to_dict(self) -> dict:
|
| 65 |
+
return asdict(self)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class OverLanguageParser:
|
| 69 |
+
"""Parses .over files into OverProgram objects."""
|
| 70 |
+
|
| 71 |
+
def parse(self, source: str) -> OverProgram:
|
| 72 |
+
prog = OverProgram(raw=source)
|
| 73 |
+
|
| 74 |
+
# Extract name
|
| 75 |
+
m = re.search(r'overprogram\s+(\w+)', source)
|
| 76 |
+
if m:
|
| 77 |
+
prog.name = m.group(1)
|
| 78 |
+
|
| 79 |
+
# Extract intent
|
| 80 |
+
m = re.search(r'intent:\s*(.*?)(?:\n\s*\n|\n\s*\w+:)', source, re.DOTALL)
|
| 81 |
+
if m:
|
| 82 |
+
prog.intent = m.group(1).strip().strip('"').strip("'")
|
| 83 |
+
|
| 84 |
+
# Extract object
|
| 85 |
+
m = re.search(r'object:\s*(.*?)(?:\n\s*\n|\n\s*\w+:)', source, re.DOTALL)
|
| 86 |
+
if m:
|
| 87 |
+
prog.object_desc = m.group(1).strip().strip('"').strip("'")
|
| 88 |
+
|
| 89 |
+
# Extract anchor
|
| 90 |
+
m = re.search(r'anchor:\s*(.*?)(?:\n\s*\n|\n\s*\w+:)', source, re.DOTALL)
|
| 91 |
+
if m:
|
| 92 |
+
prog.anchor = m.group(1).strip()
|
| 93 |
+
|
| 94 |
+
# Extract capture
|
| 95 |
+
m = re.search(r'capture:\s*(.*?)(?:\n\s*\n|\n\s*\w+:)', source, re.DOTALL)
|
| 96 |
+
if m:
|
| 97 |
+
prog.capture = [c.strip() for c in m.group(1).strip().split('\n') if c.strip()]
|
| 98 |
+
|
| 99 |
+
# Extract prove
|
| 100 |
+
m = re.search(r'prove:\s*(.*?)(?:\n\s*\n|\n\s*\w+:)', source, re.DOTALL)
|
| 101 |
+
if m:
|
| 102 |
+
prog.prove = [p.strip() for p in m.group(1).strip().split('\n') if p.strip()]
|
| 103 |
+
|
| 104 |
+
# Extract score
|
| 105 |
+
m = re.search(r'score:\s*(.*?)(?:\n\s*\n|\n\s*\w+:)', source, re.DOTALL)
|
| 106 |
+
if m:
|
| 107 |
+
score_block = m.group(1).strip()
|
| 108 |
+
for line in score_block.split('\n'):
|
| 109 |
+
if '=' in line:
|
| 110 |
+
key, val = line.split('=', 1)
|
| 111 |
+
prog.score[key.strip()] = val.strip()
|
| 112 |
+
|
| 113 |
+
# Extract output
|
| 114 |
+
m = re.search(r'output:\s*(.*?)(?:\n\s*\n|\n\s*\w+:)', source, re.DOTALL)
|
| 115 |
+
if m:
|
| 116 |
+
prog.output = [o.strip() for o in m.group(1).strip().split('\n') if o.strip()]
|
| 117 |
+
|
| 118 |
+
# Extract success
|
| 119 |
+
m = re.search(r'success:\s*(.*?)(?:\n\s*\n|\n\s*\w+:)', source, re.DOTALL)
|
| 120 |
+
if m:
|
| 121 |
+
prog.success = m.group(1).strip()
|
| 122 |
+
|
| 123 |
+
# Extract economic
|
| 124 |
+
m = re.search(r'economic:\s*(.*?)(?:\n\s*\n|\n\s*\w+:|\Z)', source, re.DOTALL)
|
| 125 |
+
if m:
|
| 126 |
+
econ_block = m.group(1).strip()
|
| 127 |
+
for line in econ_block.split('\n'):
|
| 128 |
+
if '=' in line:
|
| 129 |
+
key, val = line.split('=', 1)
|
| 130 |
+
prog.economic[key.strip()] = val.strip().strip('"').strip("'")
|
| 131 |
+
|
| 132 |
+
return prog
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
class OverLanguageCompiler:
|
| 136 |
+
"""Compiles OverProgram into production artifacts."""
|
| 137 |
+
|
| 138 |
+
def __init__(self):
|
| 139 |
+
self.parser = OverLanguageParser()
|
| 140 |
+
|
| 141 |
+
def compile(self, source: str) -> CompilationResult:
|
| 142 |
+
prog = self.parser.parse(source)
|
| 143 |
+
|
| 144 |
+
# Build plan
|
| 145 |
+
build_plan = {
|
| 146 |
+
"program": prog.name,
|
| 147 |
+
"intent": prog.intent,
|
| 148 |
+
"object": prog.object_desc,
|
| 149 |
+
"steps": self._generate_steps(prog),
|
| 150 |
+
"agents": self._assign_agents(prog),
|
| 151 |
+
"capture_planes": prog.capture,
|
| 152 |
+
"proof_claims": prog.prove,
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
# Artifact manifest
|
| 156 |
+
artifact_manifest = {
|
| 157 |
+
"name": prog.name,
|
| 158 |
+
"object": prog.object_desc,
|
| 159 |
+
"anchor": prog.anchor or "⧉◇@L",
|
| 160 |
+
"outputs": prog.output,
|
| 161 |
+
"success_condition": prog.success,
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
# Receipt
|
| 165 |
+
receipt_hash = hashlib.sha256(json.dumps(build_plan, sort_keys=True).encode()).hexdigest()
|
| 166 |
+
receipt = {
|
| 167 |
+
"receipt_id": receipt_hash[:16],
|
| 168 |
+
"program": prog.name,
|
| 169 |
+
"intent": prog.intent,
|
| 170 |
+
"proof_claims": prog.prove,
|
| 171 |
+
"artifact_hash": receipt_hash,
|
| 172 |
+
"created_at": time.time(),
|
| 173 |
+
"protocol": "OverLanguage/2.0",
|
| 174 |
+
"glyph": "⧉◇@L → H@L Æ R Æ λ⁻¹ = ◎ → $",
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
# Lambda score
|
| 178 |
+
lambda_components = {
|
| 179 |
+
"local_path_dependency": 0.20,
|
| 180 |
+
"secret_dependency": 0.00,
|
| 181 |
+
"runtime_drift": 0.15,
|
| 182 |
+
"documentation_gap": 0.10,
|
| 183 |
+
"test_gap": 0.05,
|
| 184 |
+
}
|
| 185 |
+
lambda_total = sum(lambda_components.values())
|
| 186 |
+
transferability = 1.0 / (1.0 + lambda_total)
|
| 187 |
+
lambda_score = {
|
| 188 |
+
"components": lambda_components,
|
| 189 |
+
"lambda_total": round(lambda_total, 4),
|
| 190 |
+
"transferability": round(transferability, 4),
|
| 191 |
+
"interpretation": "medium_friction",
|
| 192 |
+
"formula": "τ = R / (1 + λ)",
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
# Buyer packet
|
| 196 |
+
buyer_packet = {
|
| 197 |
+
"program": prog.name,
|
| 198 |
+
"artifact": prog.object_desc,
|
| 199 |
+
"proof_claims": prog.prove,
|
| 200 |
+
"transferability": lambda_score["transferability"],
|
| 201 |
+
"price": prog.economic.get("price", "TBD"),
|
| 202 |
+
"buyer": prog.economic.get("buyer", "TBD"),
|
| 203 |
+
"receipt_id": receipt["receipt_id"],
|
| 204 |
+
"glyph": "◇ Æ R Æ λ⁻¹ → $",
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
return CompilationResult(
|
| 208 |
+
program=prog.to_dict(),
|
| 209 |
+
build_plan=build_plan,
|
| 210 |
+
artifact_manifest=artifact_manifest,
|
| 211 |
+
receipt=receipt,
|
| 212 |
+
lambda_score=lambda_score,
|
| 213 |
+
buyer_packet=buyer_packet,
|
| 214 |
+
glyph="⧉◇@L → H@L Æ R Æ λ⁻¹ = ◎ → $",
|
| 215 |
+
compiled_at=time.time(),
|
| 216 |
+
status="compiled",
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
def _generate_steps(self, prog: OverProgram) -> list[str]:
|
| 220 |
+
steps = [
|
| 221 |
+
f"1. Find or create artifact: {prog.object_desc}",
|
| 222 |
+
f"2. Anchor artifact at canonical location: {prog.anchor or '⧉◇@L'}",
|
| 223 |
+
"3. Hash artifact (H@L)",
|
| 224 |
+
"4. Bind to receipt (H Æ R)",
|
| 225 |
+
"5. Measure lambda friction (λ)",
|
| 226 |
+
"6. Verify proof state (◎)",
|
| 227 |
+
"7. Package for buyer/investor/client ($)",
|
| 228 |
+
]
|
| 229 |
+
return steps
|
| 230 |
+
|
| 231 |
+
def _assign_agents(self, prog: OverProgram) -> dict[str, str]:
|
| 232 |
+
agents = {
|
| 233 |
+
"CHATGPT": "architecture / spec / critique",
|
| 234 |
+
"WINDSURF": "code edits / repo operations",
|
| 235 |
+
"CODEX": "patch generation / tests",
|
| 236 |
+
"CLAUDE": "deep refactor / reasoning",
|
| 237 |
+
"XCODE": "native build / signing / diagnostics",
|
| 238 |
+
"TERMINAL": "commands / receipts / verification",
|
| 239 |
+
}
|
| 240 |
+
return agents
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
# --- Layer4Meter: Latent Compute Substrate ---
|
| 244 |
+
@dataclass
|
| 245 |
+
class SubstrateSample:
|
| 246 |
+
timestamp: float = 0.0
|
| 247 |
+
cpu_seconds: float = 0.0
|
| 248 |
+
gpu_activity: float = 0.0
|
| 249 |
+
disk_write_mb: float = 0.0
|
| 250 |
+
file_event_count: int = 0
|
| 251 |
+
process_spawn_count: int = 0
|
| 252 |
+
network_bytes: int = 0
|
| 253 |
+
memory_pressure: float = 0.0
|
| 254 |
+
snapshot_delta_mb: float = 0.0
|
| 255 |
+
screen_state_changes: int = 0
|
| 256 |
+
agent_idle_seconds: float = 0.0
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
class Layer4Meter:
|
| 260 |
+
"""Captures and quantifies latent compute substrate behind AI work."""
|
| 261 |
+
|
| 262 |
+
def __init__(self):
|
| 263 |
+
self.samples: list[SubstrateSample] = []
|
| 264 |
+
self.baselines: dict[str, float] = {}
|
| 265 |
+
self.weights = {
|
| 266 |
+
"cpu_seconds": 1.0,
|
| 267 |
+
"gpu_activity": 2.0,
|
| 268 |
+
"disk_write_mb": 0.5,
|
| 269 |
+
"file_event_count": 0.01,
|
| 270 |
+
"process_spawn_count": 0.1,
|
| 271 |
+
"network_bytes": 0.0001,
|
| 272 |
+
"memory_pressure": 5.0,
|
| 273 |
+
"snapshot_delta_mb": 0.3,
|
| 274 |
+
"screen_state_changes": 0.5,
|
| 275 |
+
"agent_idle_seconds": 0.2,
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
def sample(self) -> SubstrateSample:
|
| 279 |
+
"""Capture a substrate sample. In production, this reads from system APIs."""
|
| 280 |
+
import random as _r
|
| 281 |
+
s = SubstrateSample(
|
| 282 |
+
timestamp=time.time(),
|
| 283 |
+
cpu_seconds=_r.uniform(0.1, 5.0),
|
| 284 |
+
gpu_activity=_r.uniform(0, 30),
|
| 285 |
+
disk_write_mb=_r.uniform(1, 50),
|
| 286 |
+
file_event_count=_r.randint(5, 100),
|
| 287 |
+
process_spawn_count=_r.randint(2, 30),
|
| 288 |
+
network_bytes=_r.randint(1000, 500000),
|
| 289 |
+
memory_pressure=_r.uniform(0.1, 0.8),
|
| 290 |
+
snapshot_delta_mb=_r.uniform(0, 20),
|
| 291 |
+
screen_state_changes=_r.randint(0, 10),
|
| 292 |
+
agent_idle_seconds=_r.uniform(0, 120),
|
| 293 |
+
)
|
| 294 |
+
self.samples.append(s)
|
| 295 |
+
return s
|
| 296 |
+
|
| 297 |
+
def compute_lci(self, sample: SubstrateSample) -> float:
|
| 298 |
+
"""Compute Latent Compute Index for a sample."""
|
| 299 |
+
d = asdict(sample)
|
| 300 |
+
lci = 0.0
|
| 301 |
+
for key, weight in self.weights.items():
|
| 302 |
+
lci += weight * d.get(key, 0)
|
| 303 |
+
return round(lci, 2)
|
| 304 |
+
|
| 305 |
+
def set_baseline(self, mode: str, lci: float):
|
| 306 |
+
self.baselines[mode] = lci
|
| 307 |
+
|
| 308 |
+
def hidden_compute_lift(self, workload_lci: float) -> dict:
|
| 309 |
+
"""Compute hidden compute lift = agent workload - human baseline - idle baseline."""
|
| 310 |
+
idle = self.baselines.get("idle", 0)
|
| 311 |
+
human = self.baselines.get("human", 0)
|
| 312 |
+
lift = workload_lci - human - idle
|
| 313 |
+
return {
|
| 314 |
+
"workload_lci": workload_lci,
|
| 315 |
+
"idle_baseline": idle,
|
| 316 |
+
"human_baseline": human,
|
| 317 |
+
"hidden_compute_lift": round(lift, 2),
|
| 318 |
+
"value_per_lci": None,
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
def business_metrics(self, artifact_value: float, lci: float, useful_outputs: int, retries: int, total_events: int) -> dict:
|
| 322 |
+
return {
|
| 323 |
+
"cost_per_artifact": round(lci / max(useful_outputs, 1), 2),
|
| 324 |
+
"proof_density": round(useful_outputs / max(total_events, 1), 4),
|
| 325 |
+
"agent_efficiency": round(useful_outputs / max(lci, 1), 4),
|
| 326 |
+
"waste_ratio": round(retries / max(total_events, 1), 4),
|
| 327 |
+
"revenue_readiness": round(artifact_value / max(lci, 1), 2),
|
| 328 |
+
"value_per_lci": round(artifact_value / max(lci, 1), 2),
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
def receipt(self, project: str, session_start: float) -> dict:
|
| 332 |
+
"""Generate an L4 substrate receipt."""
|
| 333 |
+
total_lci = sum(self.compute_lci(s) for s in self.samples)
|
| 334 |
+
return {
|
| 335 |
+
"type": "L4_SUBSTRATE_RECEIPT",
|
| 336 |
+
"session": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(session_start)),
|
| 337 |
+
"project": project,
|
| 338 |
+
"samples": len(self.samples),
|
| 339 |
+
"lci_total": round(total_lci, 2),
|
| 340 |
+
"lci_avg": round(total_lci / max(len(self.samples), 1), 2),
|
| 341 |
+
"proof_root": hashlib.sha256(json.dumps([asdict(s) for s in self.samples], sort_keys=True).encode()).hexdigest()[:16],
|
| 342 |
+
"protocol": "Layer4Meter/1.0",
|
| 343 |
+
"generated_at": time.time(),
|
| 344 |
+
}
|