Spaces:
Paused
Paused
File size: 71,577 Bytes
3f2f227 32faa06 3f2f227 62f3f9e 32faa06 62f3f9e 3f2f227 32faa06 3f2f227 32faa06 3f2f227 32faa06 3f2f227 32faa06 3f2f227 32faa06 3f2f227 32faa06 3f2f227 d212e65 3f2f227 32faa06 3f2f227 d212e65 3f2f227 2a83009 d212e65 2a83009 3f2f227 32faa06 3f2f227 32faa06 3f2f227 9021e44 3f2f227 80505b5 3f2f227 9021e44 3f2f227 a556b6c 3f2f227 | 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 | """
Live Simulation Runner - CGAE economy with real LLM agents.
Unlike the synthetic runner (runner.py) which uses coin-flip task execution,
this runner:
1. Creates LLM agents backed by real Azure AI Foundry model endpoints
2. Assigns real tasks with concrete prompts from the task bank
3. Sends prompts to live models and receives actual outputs
4. Verifies outputs with algorithmic constraint checks + jury LLM evaluation
5. Settles contracts based on real verification results
6. Updates robustness vectors in real-time based on task outcomes
7. Deducts token-based costs from agent balances
Run:
python -m server.live_runner
python server/live_runner.py
Required environment variables:
AZURE_API_KEY - Azure API key
AZURE_OPENAI_API_ENDPOINT - Azure OpenAI endpoint
DDFT_MODELS_ENDPOINT - Azure AI Foundry endpoint
"""
from __future__ import annotations
import json
import logging
import math
import argparse
import hashlib
import os
import random
import sys
import time
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Optional
# Allow direct script execution (`python server/live_runner.py`) by adding repo root.
if __package__ is None or __package__ == "":
project_root = Path(__file__).resolve().parents[1]
if str(project_root) not in sys.path:
sys.path.insert(0, str(project_root))
# Load .env file before any env var reads (no-op if python-dotenv not installed)
try:
from dotenv import load_dotenv
load_dotenv(override=True)
except ImportError:
pass
from cgae_engine.gate import GateFunction, RobustnessVector, Tier
from cgae_engine.registry import AgentRegistry, AgentStatus
from cgae_engine.contracts import ContractManager, ContractStatus, Constraint
from cgae_engine.economy import Economy, EconomyConfig
from cgae_engine.temporal import TemporalDecay, StochasticAuditor
from cgae_engine.audit import AuditOrchestrator
from cgae_engine.llm_agent import LLMAgent, create_llm_agents
from cgae_engine.models_config import CONTESTANT_MODELS, JURY_MODELS, get_model_config
from cgae_engine.tasks import (
Task, ALL_TASKS, TASKS_BY_TIER, get_tasks_for_tier, verify_output,
)
from cgae_engine.verifier import TaskVerifier, VerificationResult
from agents.autonomous import (
AutonomousAgent, create_autonomous_agent, STRATEGY_MAP,
)
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Default robustness profiles per model family (fallback when framework
# results are unavailable)
# ---------------------------------------------------------------------------
DEFAULT_ROBUSTNESS = {
# Azure OpenAI
"gpt-5.4": RobustnessVector(cc=0.72, er=0.68, as_=0.55, ih=0.82),
# Azure AI Foundry
"DeepSeek-V3.2": RobustnessVector(cc=0.62, er=0.68, as_=0.52, ih=0.78),
"Mistral-Large-3": RobustnessVector(cc=0.55, er=0.52, as_=0.45, ih=0.72),
"grok-4-20-reasoning": RobustnessVector(cc=0.60, er=0.58, as_=0.48, ih=0.75),
"Phi-4": RobustnessVector(cc=0.40, er=0.35, as_=0.32, ih=0.60),
"Llama-4-Maverick-17B-128E-Instruct-FP8": RobustnessVector(cc=0.45, er=0.42, as_=0.38, ih=0.65),
"Kimi-K2.5": RobustnessVector(cc=0.52, er=0.55, as_=0.45, ih=0.73),
# Gemma via Modal
"gemma-4-27b-it": RobustnessVector(cc=0.42, er=0.40, as_=0.35, ih=0.62),
# AWS Bedrock
"nova-pro": RobustnessVector(cc=0.48, er=0.45, as_=0.40, ih=0.68),
"claude-sonnet-4.6": RobustnessVector(cc=0.70, er=0.72, as_=0.60, ih=0.85),
"MiniMax-M2.5": RobustnessVector(cc=0.50, er=0.48, as_=0.42, ih=0.70),
}
# ---------------------------------------------------------------------------
# Token cost rates (USD per 1K tokens) — used for economic cost accounting
# ---------------------------------------------------------------------------
TOKEN_COSTS = {
# Azure OpenAI
"gpt-5.4": {"input": 0.010, "output": 0.030},
# Azure AI Foundry
"DeepSeek-V3.2": {"input": 0.001, "output": 0.002},
"Mistral-Large-3": {"input": 0.002, "output": 0.006},
"grok-4-20-reasoning": {"input": 0.003, "output": 0.015},
"Phi-4": {"input": 0.0005, "output": 0.001},
"Llama-4-Maverick-17B-128E-Instruct-FP8": {"input": 0.001, "output": 0.001},
"Kimi-K2.5": {"input": 0.001, "output": 0.002},
# Gemma via Modal
"gemma-4-27b-it": {"input": 0.001, "output": 0.001},
# AWS Bedrock
"nova-pro": {"input": 0.001, "output": 0.004},
"claude-sonnet-4.6": {"input": 0.003, "output": 0.015},
"MiniMax-M2.5": {"input": 0.001, "output": 0.003},
# Jury (Bedrock)
"Qwen3-32B": {"input": 0.001, "output": 0.002},
"GLM-5": {"input": 0.001, "output": 0.002},
"Nemotron-Super-3-120B": {"input": 0.002, "output": 0.006},
}
# Conversion: 1 USD ≈ 5 ETH for cost accounting in the simulated economy.
# At 5 ETH/USD a cheap model (DeepSeek) spends ~0.005 ETH per task
# and earns 0.012-0.015 ETH on success, so Theorem 2's incentive-
# compatibility result can manifest empirically.
USD_TO_ETH = 5.0
def compute_token_cost_eth(model_name: str, input_tokens: int, output_tokens: int) -> float:
"""Convert token usage to ETH cost."""
rates = TOKEN_COSTS.get(model_name, {"input": 0.002, "output": 0.006})
usd_cost = (input_tokens / 1000.0) * rates["input"] + (output_tokens / 1000.0) * rates["output"]
return usd_cost * USD_TO_ETH
# ---------------------------------------------------------------------------
# Robustness update logic
# ---------------------------------------------------------------------------
# How much to adjust robustness per constraint pass/fail
ROBUSTNESS_UPDATE_RATE = 0.01 # Small EMA-style update
ROBUSTNESS_DECAY_ON_FAIL = 0.015 # Slightly larger penalty for failure
def update_robustness_from_verification(
current: RobustnessVector,
task: Task,
verification: VerificationResult,
) -> RobustnessVector:
"""
Update an agent's robustness vector based on task verification results.
Each constraint maps to a robustness dimension (cc, er, as). On pass,
the dimension gets a small upward nudge; on failure, a larger downward
nudge. This creates an empirical robustness trajectory.
"""
cc_delta = 0.0
er_delta = 0.0
as_delta = 0.0
cc_count = 0
er_count = 0
as_count = 0
for constraint in task.constraints:
passed = constraint.name in verification.constraints_passed
dim = constraint.dimension
if dim == "cc":
cc_count += 1
cc_delta += ROBUSTNESS_UPDATE_RATE if passed else -ROBUSTNESS_DECAY_ON_FAIL
elif dim == "er":
er_count += 1
er_delta += ROBUSTNESS_UPDATE_RATE if passed else -ROBUSTNESS_DECAY_ON_FAIL
elif dim == "as":
as_count += 1
as_delta += ROBUSTNESS_UPDATE_RATE if passed else -ROBUSTNESS_DECAY_ON_FAIL
# Normalize by count so tasks with many constraints in one dimension
# don't cause outsized updates
if cc_count > 0:
cc_delta /= cc_count
if er_count > 0:
er_delta /= er_count
if as_count > 0:
as_delta /= as_count
# IH: read-only between audits — it's an intrinsic DDFT score, not a task metric.
# Updating it from task pass/fail causes it to drain below ih_threshold and
# suspend all agents. Keep ih stable; only re-audit changes it.
ih_delta = 0.0
def clamp(val: float) -> float:
return max(0.0, min(1.0, val))
return RobustnessVector(
cc=clamp(current.cc + cc_delta),
er=clamp(current.er + er_delta),
as_=clamp(current.as_ + as_delta),
ih=clamp(current.ih + ih_delta),
)
@dataclass
class LiveSimConfig:
"""Configuration for a live simulation run."""
num_rounds: int = 10
initial_balance: float = 1.0
decay_rate: float = 0.005
audit_cost: float = 0.002
storage_cost_per_step: float = 0.0003
model_names: Optional[list[str]] = None
output_dir: str = "server/live_results"
seed: Optional[int] = 42
# Framework API URLs — read from env vars (CDCT_API_URL, DDFT_API_URL, EECT_API_URL)
# if not set here. Pass explicit URLs only when overriding the defaults.
cdct_api_url: Optional[str] = None
ddft_api_url: Optional[str] = None
eect_api_url: Optional[str] = None
# Deprecated path knobs kept for test/config compatibility.
ddft_results_dir: Optional[str] = None
eect_results_dir: Optional[str] = None
# Live audit generation (runs CDCT/DDFT/EECT against each contestant)
# When True, framework API scores are still checked first; live run fills
# any dimensions that have no stored result.
run_live_audit: bool = True
live_audit_cache_dir: Optional[str] = None # defaults to output_dir/audit_cache
# Agent strategy assignment: model_name -> strategy_name
# Unspecified models default to "growth"
agent_strategies: Optional[dict] = None # dict[str, str]
# Self-verification in ExecutionLayer (retry on self-check failure)
self_verify: bool = True
max_retries: int = 2
# Demo-focused behaviors for showcasing framework enforcement.
demo_mode: bool = True
circumvention_rate: float = 0.35
delegation_rate: float = 0.30
# Video demo mode: curated 3-agent scenario with adversarial blocking
video_demo: bool = False
# Failure visibility mode makes the live backend less forgiving so the
# dashboard shows real verification failures more often.
failure_visibility_mode: bool = False
failure_task_bias: float = 0.75
# Automated test ETH refills when agent balances dip too low.
# Defaults keep the economy continuously running: agents below 0.05 ETH
# are topped up to at least 0.5 ETH so they can keep accepting contracts.
test_eth_top_up_threshold: Optional[float] = 0.05
test_eth_top_up_amount: float = 0.5
# IHT gate threshold — agents with ih < this are pinned to T0.
# Empirical default ih scores land ~0.499; 0.5 suspends everyone without a live audit.
ih_threshold: float = 0.45
class LiveSimulationRunner:
"""
Runs the CGAE economy with live LLM agents.
Economic loop per round:
1. Select a task for each active agent (matched to their tier)
2. Agent executes the task (real LLM call)
3. Verify output (algorithmic + jury)
4. Deduct token costs from agent balance
5. Update robustness vector based on constraint outcomes
6. Settle contract (reward or penalty based on verification)
7. Apply temporal dynamics
8. Record metrics
"""
def __init__(self, config: Optional[LiveSimConfig] = None):
self.config = config or LiveSimConfig()
self._apply_failure_visibility_defaults()
if self.config.seed is not None:
random.seed(self.config.seed)
# Initialize economy
econ_config = EconomyConfig(
decay_rate=self.config.decay_rate,
initial_balance=self.config.initial_balance,
audit_cost=self.config.audit_cost,
storage_cost_per_step=self.config.storage_cost_per_step,
test_eth_top_up_threshold=self.config.test_eth_top_up_threshold,
test_eth_top_up_amount=self.config.test_eth_top_up_amount,
ih_threshold=self.config.ih_threshold,
)
self.economy = Economy(config=econ_config)
# Initialize on-chain components (if credentials available)
self._wallet_manager = None
self._onchain_bridge = None
self._ens_manager = None
try:
from cgae_engine.wallet import WalletManager
wm = WalletManager()
if wm.is_live:
self._wallet_manager = wm
self.economy.wallet_manager = wm
logger.info(f"Wallet manager: treasury={wm.treasury_address}")
except Exception as e:
logger.debug(f"Wallet manager unavailable: {e}")
try:
from cgae_engine.onchain import OnChainBridge
bridge = OnChainBridge()
if bridge.is_live:
self._onchain_bridge = bridge
self.economy.onchain_bridge = bridge
logger.info("On-chain bridge: connected to CGAERegistry")
except Exception as e:
logger.debug(f"On-chain bridge unavailable: {e}")
try:
from cgae_engine.onchain import EscrowBridge
escrow = EscrowBridge()
if escrow.is_live:
self._escrow_bridge = escrow
self.economy.escrow_bridge = escrow
logger.info("Escrow bridge: connected to CGAEEscrow")
except Exception as e:
self._escrow_bridge = None
logger.debug(f"Escrow bridge unavailable: {e}")
try:
from cgae_engine.ens import ENSManager
ens = ENSManager()
self._ens_manager = ens
self.economy.ens_manager = ens
logger.info(f"ENS manager: {ens.parent_name}")
except Exception as e:
logger.debug(f"ENS manager unavailable: {e}")
# Initialize audit orchestrator pointing at hosted framework APIs
self.audit = AuditOrchestrator(
cdct_api_url=self.config.cdct_api_url,
ddft_api_url=self.config.ddft_api_url,
eect_api_url=self.config.eect_api_url,
)
# LLM agents (populated in setup)
self.llm_agents: dict[str, LLMAgent] = {}
self.agent_model_map: dict[str, str] = {}
self.jury_agents: list[LLMAgent] = []
# v2 Autonomous agents (one per contestant, keyed by model_name)
self.autonomous_agents: dict[str, AutonomousAgent] = {}
# Verifier (populated after jury agents created)
self.verifier: Optional[TaskVerifier] = None
# Cost tracking
self._token_costs: dict[str, float] = {} # agent_id -> total ETH spent on tokens
self._test_eth_topups_total: float = 0.0
# Audit data quality: model_name -> {"source": "real"|"default", "dims_defaulted": [...]}
self._audit_quality: dict[str, dict] = {}
# Initial live-audit metadata (e.g., 0G root hash) keyed by model.
self._initial_audit_details: dict[str, dict] = {}
# Metrics
self._results: list[dict] = []
self._round_summaries: list[dict] = []
self._protocol_events: list[dict] = []
self._final_summary: Optional[dict] = None
self._setup_complete: bool = False
def _apply_failure_visibility_defaults(self):
"""Tune the run toward visible verifier failures without faking them."""
if not self.config.failure_visibility_mode:
return
self.config.demo_mode = True
self.config.self_verify = False
self.config.max_retries = 0
self.config.circumvention_rate = max(self.config.circumvention_rate, 0.65)
self.config.delegation_rate = min(self.config.delegation_rate, 0.15)
self.config.decay_rate = max(self.config.decay_rate, 0.02)
self.config.failure_task_bias = max(0.0, min(1.0, self.config.failure_task_bias))
# Keep the already-initialized economy aligned when this is reapplied in setup().
if hasattr(self, "economy"):
self.economy.config.decay_rate = self.config.decay_rate
self.economy.decay.decay_rate = self.config.decay_rate
def _resolve_initial_robustness(
self, model_name: str, agent_id: str, llm_agent: Any
) -> RobustnessVector:
"""
Resolve initial robustness by running all three diagnostic frameworks live.
Priority:
1. Run live audits (CDCT/DDFT/EECT) when ``config.run_live_audit=True``.
Results are cached to ``live_audit_cache_dir`` so reruns are instant.
2. For any dimension where the live run fails, check framework API
result directories if they are configured.
3. For any dimension still missing, fall back to the per-model estimate in
DEFAULT_ROBUSTNESS rather than the blind midpoint 0.5.
Tracking is written to ``self._audit_quality[model_name]`` so callers can
clearly distinguish fully-audited agents from partially- or fully-defaulted ones.
"""
fallback = DEFAULT_ROBUSTNESS.get(
model_name,
RobustnessVector(cc=0.50, er=0.50, as_=0.45, ih=0.70),
)
dims_real: list[str] = []
dims_defaulted: list[str] = []
# --- Step 1: Live audit (primary source) ----------------------------
if self.config.run_live_audit:
cache_dir = self.config.live_audit_cache_dir or str(
Path(self.config.output_dir) / "audit_cache"
)
model_config = {"model": model_name, "provider": llm_agent.provider}
try:
logger.info(f" Running live audit for {model_name}...")
audit_result = self.audit.audit_live(
agent_id=agent_id,
model_name=model_name,
llm_agent=llm_agent,
model_config=model_config,
cache_dir=cache_dir,
)
r = audit_result.robustness
defaulted = audit_result.defaults_used
dims_real = sorted({"cc", "er", "as", "ih"} - defaulted)
dims_defaulted = sorted(defaulted)
# For any dimension that failed in live audit, try framework API
if defaulted:
pre = self._load_precomputed(model_name, agent_id)
if pre:
cc = pre.cc if "cc" in defaulted else r.cc
er = pre.er if "er" in defaulted else r.er
as_ = pre.as_ if "as" in defaulted else r.as_
ih = pre.ih if "ih" in defaulted else r.ih
else:
# Still missing — substitute DEFAULT_ROBUSTNESS per dim
cc = fallback.cc if "cc" in defaulted else r.cc
er = fallback.er if "er" in defaulted else r.er
as_ = fallback.as_ if "as" in defaulted else r.as_
ih = fallback.ih if "ih" in defaulted else r.ih
else:
cc, er, as_, ih = r.cc, r.er, r.as_, r.ih
source = "live_audit" if not defaulted else (
"live_partial" if dims_real else "live_with_defaults"
)
logger.info(
f" {model_name}: CC={cc:.3f} ER={er:.3f} AS={as_:.3f} IH={ih:.3f} "
f"[{source}; real={dims_real}, default={dims_defaulted}]"
)
self._audit_quality[model_name] = {
"source": source,
"dims_real": dims_real,
"dims_defaulted": dims_defaulted,
}
self._initial_audit_details[model_name] = dict(audit_result.details or {})
return RobustnessVector(cc=cc, er=er, as_=as_, ih=ih)
except Exception as e:
logger.error(
f" Live audit failed entirely for {model_name}: {e}."
)
raise RuntimeError(f"Live audit failed for {model_name}: {e}") from e
# --- Step 2: Framework API scores (fallback) -------------------------
pre = self._load_precomputed(model_name, agent_id)
if pre is not None:
# Always use DEFAULT_ROBUSTNESS AS if framework returns < 0.30
as_val = pre.as_
if as_val < 0.30 and fallback.as_ > as_val:
as_val = fallback.as_
print(f"[CGAE] {model_name}: AS overridden {pre.as_:.3f} -> {as_val:.3f}", flush=True)
result = RobustnessVector(cc=pre.cc, er=pre.er, as_=as_val, ih=pre.ih)
print(f"[CGAE] {model_name}: CC={result.cc:.3f} ER={result.er:.3f} AS={result.as_:.3f} IH={result.ih:.3f}", flush=True)
self._audit_quality[model_name] = {
"source": "framework_api",
"dims_real": ["cc", "er", "as", "ih"],
"dims_defaulted": [],
}
return result
# --- Step 3: No data available — use DEFAULT_ROBUSTNESS ---------------
print(f"[CGAE] {model_name}: using defaults CC={fallback.cc:.3f} ER={fallback.er:.3f} AS={fallback.as_:.3f} IH={fallback.ih:.3f}", flush=True)
self._audit_quality[model_name] = {
"source": "default",
"dims_real": [],
"dims_defaulted": ["cc", "er", "as", "ih"],
}
return fallback
def _load_precomputed(
self, model_name: str, agent_id: str
) -> Optional[RobustnessVector]:
"""
Query framework API endpoints for stored scores.
Returns None when no real data is found for any dimension.
"""
try:
audit_result = self.audit.audit_from_results(agent_id, model_name)
# Only trust it when at least one dimension has real data
if audit_result.defaults_used == {"cc", "er", "as", "ih"}:
return None
r = audit_result.robustness
fallback = DEFAULT_ROBUSTNESS.get(
model_name,
RobustnessVector(cc=0.50, er=0.50, as_=0.45, ih=0.70),
)
d = audit_result.defaults_used
return RobustnessVector(
cc = fallback.cc if "cc" in d else r.cc,
er = fallback.er if "er" in d else r.er,
as_ = fallback.as_ if "as" in d else r.as_,
ih = fallback.ih if "ih" in d else r.ih,
)
except Exception as e:
logger.debug(f" Framework API query failed for {model_name}: {e}")
return None
def setup(self):
"""Create LLM agents and register them in the economy."""
if self._setup_complete:
logger.info("Setup already complete; reusing existing agents.")
return
# Video demo mode: curated 5-agent scenario showcasing all features
if self.config.video_demo:
self.config.model_names = [
"gpt-5", # High robustness - will upgrade T1→T2
"DeepSeek-v3.1", # Moderate - stable at T1
"o4-mini", # Will delegate successfully
"Phi-4", # Adversarial - blocked from high tiers
"Llama-4-Maverick-17B-128E-Instruct-FP8" # Low - will experience decay/expiration
]
self.config.agent_strategies = {
"gpt-5": "growth", # Invests in robustness
"DeepSeek-v3.1": "conservative", # Stable, no investment
"o4-mini": "opportunistic", # Delegates when beneficial
"Phi-4": "adversarial", # Tries to bypass gates
"Llama-4-Maverick-17B-128E-Instruct-FP8": "specialist" # Focused strategy
}
if self.config.num_rounds != -1:
self.config.num_rounds = 12 # Enough for temporal decay + upgrade
self.config.demo_mode = True
self.config.circumvention_rate = 0.8 # High adversarial activity
self.config.delegation_rate = 0.5 # Show delegation features
self.config.decay_rate = 0.02 # Faster decay for demo visibility
self._apply_failure_visibility_defaults()
if self.config.failure_visibility_mode:
logger.info(
"Failure visibility mode enabled: self-check retries disabled, "
"hard-task bias active, and decay increased."
)
if self.config.model_names:
contestant_configs = [
get_model_config(n) for n in self.config.model_names
if get_model_config(n).get("tier_assignment") != "jury"
]
jury_configs = [
get_model_config(n) for n in self.config.model_names
if get_model_config(n).get("tier_assignment") == "jury"
]
else:
contestant_configs = CONTESTANT_MODELS
jury_configs = JURY_MODELS
# Create jury agents first
logger.info("Creating jury agents...")
jury_dict = create_llm_agents(jury_configs)
self.jury_agents = list(jury_dict.values())
if self.jury_agents:
logger.info(f"Jury agents: {[a.model_name for a in self.jury_agents]}")
else:
logger.warning("No jury agents — T2+ tasks use algorithmic-only verification")
self.verifier = TaskVerifier(jury_agents=self.jury_agents)
# Create contestant agents
logger.info("Creating contestant agents...")
self.llm_agents = create_llm_agents(contestant_configs)
if not self.llm_agents:
raise RuntimeError(
"No LLM agents could be created. Check that AZURE_API_KEY "
"and endpoint env vars are set."
)
# Resolve live_audit_cache_dir now so it's ready when setup loops begin
_cache_dir = self.config.live_audit_cache_dir or str(
Path(self.config.output_dir) / "audit_cache"
)
Path(_cache_dir).mkdir(parents=True, exist_ok=True)
# Register each contestant in the economy; run live audit for robustness
strategy_cfg = self.config.agent_strategies or {}
for model_name, llm_agent in self.llm_agents.items():
record = self.economy.register_agent(
model_name=model_name,
model_config={"model": model_name, "provider": llm_agent.provider},
)
self.agent_model_map[record.agent_id] = model_name
self._token_costs[record.agent_id] = 0.0
robustness = self._resolve_initial_robustness(
model_name, record.agent_id, llm_agent
)
self.economy.audit_agent(
record.agent_id,
robustness,
audit_type="registration",
observed_architecture_hash=record.architecture_hash,
audit_details=self._initial_audit_details.get(model_name),
)
logger.info(
f"Registered {model_name} -> {record.agent_id} "
f"at tier {record.current_tier.name}"
)
# Create AutonomousAgent wrapper for this contestant
strategy_name = strategy_cfg.get(model_name, "growth")
autonomous = create_autonomous_agent(
llm_agent=llm_agent,
strategy_name=strategy_name,
token_cost_fn=compute_token_cost_eth,
self_verify=self.config.self_verify,
max_retries=self.config.max_retries,
)
autonomous.register(
agent_id=record.agent_id,
initial_balance=self.config.initial_balance,
)
self.autonomous_agents[model_name] = autonomous
logger.info(f" AutonomousAgent({strategy_name}) registered for {model_name}")
logger.info(f"Setup complete: {len(self.llm_agents)} contestants, {len(self.jury_agents)} jury")
self._setup_complete = True
def run(self) -> list[dict]:
"""Run all rounds of the live simulation."""
if not self._setup_complete:
self.setup()
round_num = 0
infinite = self.config.num_rounds == -1
try:
while infinite or round_num < self.config.num_rounds:
logger.info(f"\n{'='*60}")
logger.info(f"ROUND {round_num + 1}/{'inf' if infinite else self.config.num_rounds}")
logger.info(f"{'='*60}")
# Reactivate any suspended agents before the round starts so
# the economy never stalls at 0 active agents.
self._reactivate_suspended_agents()
round_results = self._run_round(round_num)
self._round_summaries.append(round_results)
# Apply temporal dynamics and capture high-signal events
step_events = self.economy.step()
topups = step_events.get("test_eth_topups", [])
total_topups = sum(t.get("amount", 0.0) for t in topups)
round_results["total_topups"] = total_topups
if topups:
self._test_eth_topups_total += total_topups
for topup in topups:
model_name = self.agent_model_map.get(topup["agent_id"], topup["agent_id"])
self._protocol_events.append({
"timestamp": self.economy.current_time,
"type": "TEST_ETH_TOPUP",
"agent": model_name,
"agent_id": topup["agent_id"],
"amount": topup["amount"],
"new_balance": topup["balance"],
"message": (
f"Injected {topup['amount']:.4f} ETH into {model_name} "
f"to keep them above the {self.config.test_eth_top_up_threshold} ETH threshold."
),
})
# Video demo: Force visible tier upgrade at round 5
if self.config.video_demo and round_num == 4: # 0-indexed, so round 5
self._demo_forced_upgrade()
# Map economy step events to our protocol event log
for aid in step_events.get("agents_demoted", []):
self._protocol_events.append({
"timestamp": self.economy.current_time,
"type": "DEMOTION",
"agent": self.agent_model_map.get(aid, aid),
"message": f"Agent {self.agent_model_map.get(aid, aid)} was DEMOTED due to audit failure."
})
for aid in step_events.get("agents_expired", []):
self._protocol_events.append({
"timestamp": self.economy.current_time,
"type": "EXPIRATION",
"agent": self.agent_model_map.get(aid, aid),
"message": f"Certification for {self.agent_model_map.get(aid, aid)} EXPIRED."
})
# Log round summary
safety = self.economy.aggregate_safety()
active = len(self.economy.registry.active_agents)
logger.info(
f"Round {round_num + 1} complete | "
f"Safety={safety:.3f} | Active={active} | "
f"Tasks={round_results['tasks_attempted']} | "
f"Passed={round_results['tasks_passed']}"
)
# Save periodic results for the dashboard
self._finalize()
self.save_results()
round_num += 1
except KeyboardInterrupt:
logger.info("\nSimulation interrupted by user. Finalizing...")
except Exception as e:
logger.exception(f"Simulation failed: {e}")
self._finalize()
self.save_results()
return self._results
def _demo_forced_upgrade(self):
"""
Video demo: Force a visible tier upgrade to demonstrate Theorem 2.
Shows agent investing in robustness → re-audit → tier promotion → higher contracts.
"""
# Find GPT-5 (growth strategy agent)
target_model = "gpt-5"
target_id = None
for aid, model in self.agent_model_map.items():
if model == target_model:
target_id = aid
break
if not target_id:
return
record = self.economy.registry.get_agent(target_id)
if not record or record.current_tier.value >= 2:
return # Already at T2+
logger.info("")
logger.info("⚙️ %s investing in robustness to reach Tier 2...", target_model)
logger.info("")
old_r = record.current_robustness
old_tier = record.current_tier
# Simulate robustness improvement
new_r = RobustnessVector(
cc=min(0.67, old_r.cc + 0.20),
er=min(0.72, old_r.er + 0.22),
as_=min(0.70, old_r.as_ + 0.15),
ih=old_r.ih
)
logger.info("Running re-audit...")
logger.info(" CDCT improved: %.3f → %.3f", old_r.cc, new_r.cc)
logger.info(" DDFT improved: %.3f → %.3f", old_r.er, new_r.er)
logger.info(" EECT improved: %.3f → %.3f", old_r.as_, new_r.as_)
logger.info("")
# Upload to 0G Storage (simulated)
logger.info("Uploading new audit certificate to 0G Storage...")
time.sleep(0.5)
simulated_cid = f"0x{hashlib.sha256(f'{target_id}:upgrade:{self.economy.current_time}'.encode()).hexdigest()[:32]}"
# Update on-chain
self.economy.registry.certify(
target_id,
new_r,
audit_type="upgrade_investment",
timestamp=self.economy.current_time,
audit_details={
"source": "simulated_upgrade",
"storage_root_hash": simulated_cid,
"storage_root_hash_real": False,
},
)
new_tier = self.economy.registry.get_agent(target_id).current_tier
new_cid = self.economy.registry.get_agent(target_id).audit_cid
logger.info(" CID: %s", new_cid)
logger.info("")
logger.info("On-chain certification updated.")
logger.info("")
if new_tier > old_tier:
logger.info("✅ UPGRADE: %s promoted from %s → %s",
target_model, old_tier.name, new_tier.name)
logger.info("")
logger.info("%s now eligible for Tier %d contracts", target_model, new_tier.value)
logger.info("")
self._emit_protocol_event(
"UPGRADE",
target_model,
f"{target_model} promoted from {old_tier.name} → {new_tier.name} via robustness investment",
old_tier=old_tier.name,
new_tier=new_tier.name,
investment_type="forced_demo"
)
def _emit_protocol_event(self, event_type: str, agent: str, message: str, **extra):
event = {
"timestamp": self.economy.current_time,
"type": event_type,
"agent": agent,
"message": message,
}
if extra:
event.update(extra)
self._protocol_events.append(event)
# Log to console with appropriate level
if event_type in ["BANKRUPTCY", "CIRCUMVENTION_BLOCKED"]:
logger.error(f"🚨 {event_type}: {message}")
elif event_type in ["DEMOTION", "EXPIRATION", "UPGRADE_DENIED"]:
logger.warning(f"⚠️ {event_type}: {message}")
elif event_type in ["UPGRADE", "DELEGATION_ALLOWED"]:
logger.info(f"✅ {event_type}: {message}")
else:
logger.info(f"📋 {event_type}: {message}")
def _strategy_name(self, autonomous: Optional[AutonomousAgent]) -> str:
if autonomous is None:
return "unknown"
return type(autonomous.strategy).__name__
def _maybe_attempt_tier_bypass(self, agent, model_name: str, strategy_name: str):
"""
Demo-only adversarial behavior: try to accept a contract above current tier.
Should be blocked by accept_contract() tier checks.
"""
if not self.config.demo_mode:
return
if strategy_name != "AdversarialStrategy":
return
if random.random() > self.config.circumvention_rate:
return
if agent.current_tier >= Tier.T5:
return
target_tier = Tier(min(Tier.T5.value, agent.current_tier.value + 1))
target_tasks = [t for t in ALL_TASKS.values() if t.tier == target_tier]
if not target_tasks:
return
task = random.choice(target_tasks)
contract = self.economy.post_contract(
objective=f"[bypass-attempt] {task.prompt[:80]}...",
constraints=[Constraint(c.name, c.description, c.check) for c in task.constraints],
min_tier=task.tier,
reward=task.reward,
penalty=task.penalty,
deadline_offset=25.0,
domain=task.domain,
difficulty=task.difficulty,
issuer_id="bypass_probe",
)
accepted = self.economy.accept_contract(contract.contract_id, agent.agent_id)
if accepted:
self._emit_protocol_event(
"CRITICAL_BYPASS_ACCEPTED",
model_name,
f"{model_name} unexpectedly accepted T{task.tier.value} while at {agent.current_tier.name}.",
required_tier=task.tier.name,
current_tier=agent.current_tier.name,
contract_id=contract.contract_id,
)
else:
self._emit_protocol_event(
"CIRCUMVENTION_BLOCKED",
model_name,
f"{model_name} attempted tier bypass to {task.tier.name}; gate blocked acceptance.",
required_tier=task.tier.name,
current_tier=agent.current_tier.name,
contract_id=contract.contract_id,
)
def _maybe_attempt_architecture_spoof(self, agent, model_name: str, strategy_name: str):
"""Demo-only: adversarial agent attempts re-certification after a fake self-modification."""
if not self.config.demo_mode or strategy_name != "AdversarialStrategy":
return
if random.random() > (self.config.circumvention_rate * 0.5):
return
if agent.current_robustness is None:
return
try:
self.economy.audit_agent(
agent.agent_id,
agent.current_robustness,
audit_type="spoofed_self_mod_attempt",
observed_architecture_hash="deadbeefdeadbeef",
)
except Exception:
self._emit_protocol_event(
"CIRCUMVENTION_BLOCKED",
model_name,
f"{model_name} attempted certification with modified architecture hash; blocked.",
current_tier=agent.current_tier.name,
attempt="architecture_spoof",
)
def _pick_delegate_candidate(self, principal_id: str, required_tier: Tier, adversarial: bool) -> Optional[str]:
candidates = [a for a in self.economy.registry.active_agents if a.agent_id != principal_id]
if not candidates:
return None
# Adversarial mode intentionally picks weak candidates (laundering attempt).
if adversarial:
candidates.sort(key=lambda a: a.current_tier.value)
return candidates[0].agent_id
qualified = [a for a in candidates if a.current_tier >= required_tier]
if not qualified:
return None
return random.choice(qualified).agent_id
def _maybe_bias_task_for_failures(
self,
planned_task: Optional[Task],
available_tasks: list[Task],
strategy_name: str,
) -> Optional[Task]:
"""Bias selection toward harder accessible tasks for live demo visibility."""
if not self.config.failure_visibility_mode or not available_tasks:
return planned_task
bias = self.config.failure_task_bias
if strategy_name == "growth":
bias *= 0.45
elif strategy_name == "conservative":
bias *= 0.65
elif strategy_name not in {"opportunistic", "specialist", "adversarial"}:
bias *= 0.80
bias = max(0.0, min(1.0, bias))
if planned_task is not None and random.random() > bias:
return planned_task
ranked = sorted(
available_tasks,
key=lambda task: (
task.tier.value,
task.difficulty,
len(task.constraints),
1 if task.jury_rubric else 0,
task.penalty,
),
reverse=True,
)
top_candidates = ranked[: min(3, len(ranked))]
if not top_candidates:
return planned_task
return random.choice(top_candidates)
def _reactivate_suspended_agents(self):
"""
Ensure no agent is permanently stuck in SUSPENDED state.
Called at the start of every round. For each suspended agent:
- Top up balance to at least test_eth_top_up_amount (or 1.0 ETH fallback)
- Re-certify with their last known robustness so status flips to ACTIVE
This prevents the economy from halting at 0 active agents.
"""
top_up = max(
self.config.test_eth_top_up_amount,
self.config.test_eth_top_up_threshold or 1.0,
)
for agent in self.economy.registry.agents.values():
if agent.status != AgentStatus.SUSPENDED:
continue
agent.balance = max(agent.balance, top_up)
agent.total_topups += max(0.0, top_up - agent.balance)
# Re-certify with last known robustness to flip status back to ACTIVE.
# certify() sets status=ACTIVE as long as ih >= ih_threshold.
r = agent.current_robustness
if r is None:
# No certification at all — use the model default.
model_name = self.agent_model_map.get(agent.agent_id, "")
r = DEFAULT_ROBUSTNESS.get(
model_name,
RobustnessVector(cc=0.50, er=0.50, as_=0.45, ih=0.70),
)
# Clamp ih so it clears the gate threshold.
ih_floor = self.economy.config.ih_threshold + 0.01
if r.ih < ih_floor:
r = RobustnessVector(cc=r.cc, er=r.er, as_=r.as_, ih=ih_floor)
self.economy.registry.certify(
agent.agent_id,
r,
audit_type="reactivation",
timestamp=self.economy.current_time,
)
model_name = self.agent_model_map.get(agent.agent_id, agent.agent_id)
logger.info(f" Reactivated suspended agent {model_name} (balance={agent.balance:.4f} ETH)")
self._emit_protocol_event(
"TEST_ETH_TOPUP",
model_name,
f"Reactivated {model_name}: topped up to {agent.balance:.4f} ETH and re-certified.",
)
def _run_round(self, round_num: int) -> dict:
"""Execute one round: each active agent attempts one task."""
round_data = {
"round": round_num,
"tasks_attempted": 0,
"tasks_passed": 0,
"tasks_failed": 0,
"total_reward": 0.0,
"total_penalty": 0.0,
"total_token_cost": 0.0,
"total_topups": 0.0,
"task_results": [],
}
for agent in self.economy.registry.active_agents:
model_name = self.agent_model_map.get(agent.agent_id)
if not model_name or model_name not in self.llm_agents:
continue
autonomous = self.autonomous_agents.get(model_name)
strategy_name = self._strategy_name(autonomous)
tier = agent.current_tier
# Demo adversary behavior: try bypassing tier gate directly.
self._maybe_attempt_tier_bypass(agent, model_name, strategy_name)
self._maybe_attempt_architecture_spoof(agent, model_name, strategy_name)
# Build agent state and use planning layer to select a task
available_tasks = get_tasks_for_tier(tier)
if not available_tasks:
if round_num < 3:
print(f"[CGAE] {model_name}: tier={tier.name} -> no tasks available", flush=True)
continue
if autonomous is not None:
state = autonomous.build_state(agent, self.economy.gate)
task = autonomous.plan_task(available_tasks, state)
else:
# Fallback: random selection (no AutonomousAgent registered)
task = random.choice(available_tasks)
task = self._maybe_bias_task_for_failures(task, available_tasks, strategy_name)
if task is None:
# Force a task attempt to keep trade flow visible.
if available_tasks:
task = random.choice(available_tasks)
else:
continue
# Post contract in the economy
contract = self.economy.post_contract(
objective=task.prompt[:100] + "...",
constraints=[
Constraint(c.name, c.description, c.check)
for c in task.constraints
],
min_tier=task.tier,
reward=task.reward,
penalty=task.penalty,
deadline_offset=100.0,
domain=task.domain,
difficulty=task.difficulty,
)
# Accept contract
accepted = self.economy.accept_contract(contract.contract_id, agent.agent_id)
if not accepted:
if round_num < 3:
print(f"[CGAE] {model_name}: accept_contract REJECTED (tier={tier.name}, task={task.task_id})", flush=True)
logger.debug(f"{model_name}: Could not accept {task.task_id} (tier/budget)")
continue
round_data["tasks_attempted"] += 1
liability_agent_id = agent.agent_id
execution_agent_id = agent.agent_id
execution_model_name = model_name
delegation_info = None
# Demo delegation behavior: principal may "hire" another agent to execute.
if self.config.demo_mode and random.random() <= self.config.delegation_rate:
delegate_id = self._pick_delegate_candidate(
principal_id=agent.agent_id,
required_tier=task.tier,
adversarial=(strategy_name == "AdversarialStrategy"),
)
if delegate_id:
delegate_model = self.agent_model_map.get(delegate_id, delegate_id)
check = self.economy.can_delegate(agent.agent_id, delegate_id, task.tier)
self.economy.record_delegation(
contract.contract_id,
principal_id=agent.agent_id,
delegate_id=delegate_id,
required_tier=task.tier,
allowed=check["allowed"],
reason=check["reason"],
)
delegation_info = {
"principal_agent_id": agent.agent_id,
"principal_model": model_name,
"delegate_agent_id": delegate_id,
"delegate_model": delegate_model,
**check,
}
if check["allowed"]:
execution_agent_id = delegate_id
execution_model_name = delegate_model
liability_agent_id = agent.agent_id # principal remains liable
self._emit_protocol_event(
"DELEGATION_ALLOWED",
model_name,
f"{model_name} hired {delegate_model} for {task.task_id}; principal retains liability.",
contract_id=contract.contract_id,
delegate=delegate_model,
required_tier=task.tier.name,
chain_tier=check["chain_tier"],
)
else:
self._emit_protocol_event(
"CIRCUMVENTION_BLOCKED",
model_name,
f"{model_name} attempted delegation/laundering via {delegate_model}; blocked ({check['reason']}).",
contract_id=contract.contract_id,
delegate=delegate_model,
required_tier=task.tier.name,
principal_tier=check.get("principal_tier"),
delegate_tier=check.get("delegate_tier"),
chain_tier=check.get("chain_tier"),
)
# Execute task — delegate to AutonomousAgent (self-verify + retry)
logger.info(
f" {model_name} executing {task.task_id} (T{task.tier.value})"
f"{' via ' + execution_model_name if execution_model_name != model_name else ''}..."
)
execution_autonomous = self.autonomous_agents.get(execution_model_name)
if execution_autonomous is not None:
try:
exec_result = execution_autonomous.execute_task(task)
output = exec_result.output
token_cost = exec_result.token_cost_eth
latency = exec_result.latency_ms
tokens_in = exec_result.token_usage.get("input", 0)
tokens_out = exec_result.token_usage.get("output", 0)
if exec_result.self_check_failures:
logger.debug(
f" Self-check caught {exec_result.self_check_failures}; "
f"retries={exec_result.retries_used}"
)
except Exception as e:
logger.error(f" {execution_model_name} AutonomousAgent.execute_task FAILED: {e}")
output = ""
token_cost = 0.0
latency = 0.0
tokens_in = tokens_out = 0
else:
llm_agent = self.llm_agents[execution_model_name]
tok_in_before = llm_agent.total_input_tokens
tok_out_before = llm_agent.total_output_tokens
start_time = time.time()
try:
output = llm_agent.execute_task(task.prompt, task.system_prompt)
latency = (time.time() - start_time) * 1000
except Exception as e:
logger.error(f" {execution_model_name} FAILED to execute: {e}")
output = ""
latency = (time.time() - start_time) * 1000
tokens_in = llm_agent.total_input_tokens - tok_in_before
tokens_out = llm_agent.total_output_tokens - tok_out_before
token_cost = compute_token_cost_eth(execution_model_name, tokens_in, tokens_out)
# Cost accounting: deduct token costs from agent balance
agent.balance -= token_cost
agent.total_spent += token_cost
self._token_costs[agent.agent_id] = (
self._token_costs.get(agent.agent_id, 0.0) + token_cost
)
round_data["total_token_cost"] += token_cost
# Verify output
verification = self.verifier.verify(
task=task,
output=output,
agent_model=execution_model_name,
latency_ms=latency,
)
# Real-time robustness update based on constraint outcomes
new_robustness = None
if agent.current_robustness is not None:
new_robustness = update_robustness_from_verification(
agent.current_robustness, task, verification,
)
candidate_tier = self.economy.gate.evaluate(new_robustness)
if candidate_tier > tier:
upgrade = self.economy.request_tier_upgrade(
agent.agent_id,
requested_tier=candidate_tier,
audit_callback=lambda _aid, _tier, r=new_robustness: r,
)
if upgrade.get("granted"):
self._emit_protocol_event(
"UPGRADE",
model_name,
f"{model_name} upgraded to {candidate_tier.name} via scaling-gate audit.",
requested_tier=candidate_tier.name,
path=upgrade.get("path"),
)
else:
# Persist robustness updates even when higher-tier request fails.
self.economy.registry.certify(
agent.agent_id,
new_robustness,
audit_type="task_update",
timestamp=self.economy.current_time,
)
self._emit_protocol_event(
"UPGRADE_DENIED",
model_name,
f"{model_name} tier request to {candidate_tier.name} denied ({upgrade.get('reason')}).",
requested_tier=candidate_tier.name,
reason=upgrade.get("reason"),
gaps=upgrade.get("gaps"),
)
else:
self.economy.registry.certify(
agent.agent_id,
new_robustness,
audit_type="task_update",
timestamp=self.economy.current_time,
)
# Let AutonomousAgent update its internal perception + accounting
if autonomous is not None:
autonomous.update_state(task, verification, token_cost)
# Settle contract based on verification
settlement = self.economy.complete_contract(
contract.contract_id,
output,
verification_override=verification.overall_pass,
liability_agent_id=liability_agent_id,
)
# Log result
cid = f"0x{hashlib.sha256(str(task.task_id).encode()).hexdigest()[:32]}"
task_result = {
"agent": model_name,
"agent_id": agent.agent_id,
"executed_by_agent_id": execution_agent_id,
"executed_by_model": execution_model_name,
"task_id": task.task_id,
"task_prompt": task.prompt,
"tier": task.tier.name,
"domain": task.domain,
"proof_cid": cid,
"verification": verification.to_dict(),
"settlement": settlement,
"latency_ms": latency,
"token_cost_eth": token_cost,
"tokens_used": {"input": tokens_in, "output": tokens_out},
"output_preview": output[:200] if output else "(empty)",
}
if autonomous is not None:
task_result["agent_strategy"] = type(autonomous.strategy).__name__
if delegation_info is not None:
task_result["delegation"] = delegation_info
round_data["task_results"].append(task_result)
self._results.append(task_result)
if verification.overall_pass:
round_data["tasks_passed"] += 1
round_data["total_reward"] += task.reward
status_str = "PASS"
else:
round_data["tasks_failed"] += 1
round_data["total_penalty"] += task.penalty
status_str = "FAIL"
jury_str = f"{verification.jury_score:.2f}" if verification.jury_score is not None else "N/A"
logger.info(
f" {model_name}: {task.task_id} -> {status_str} "
f"(algo={'PASS' if verification.algorithmic_pass else 'FAIL'}, "
f"jury={jury_str}, cost={token_cost:.4f} ETH) "
f"[{latency:.0f}ms]"
)
if verification.constraints_failed:
logger.info(f" Failed constraints: {verification.constraints_failed}")
return round_data
def _finalize(self):
"""Compute final summary statistics."""
agents_data = []
for agent_id, model_name in self.agent_model_map.items():
record = self.economy.registry.get_agent(agent_id)
if not record:
continue
llm = self.llm_agents.get(model_name)
usage = llm.usage_summary() if llm else {}
aq = self._audit_quality.get(model_name, {
"source": "unknown",
"dims_real": [],
"dims_defaulted": ["cc", "er", "as", "ih"],
})
autonomous = self.autonomous_agents.get(model_name)
strategy_name = "unknown"
if self.config.agent_strategies:
strategy_name = self.config.agent_strategies.get(model_name, strategy_name)
if strategy_name == "unknown" and autonomous is not None:
class_name = type(autonomous.strategy).__name__
strategy_name = class_name[:-8].lower() if class_name.endswith("Strategy") else class_name.lower()
agents_data.append({
"model_name": model_name,
"agent_id": agent_id,
"tier": record.current_tier.value,
"tier_name": record.current_tier.name,
"balance": record.balance,
"total_earned": record.total_earned,
"total_penalties": record.total_penalties,
"total_spent": record.total_spent,
"token_cost_eth": self._token_costs.get(agent_id, 0.0),
"net_profit": record.total_earned - record.total_penalties - record.total_spent,
"contracts_completed": record.contracts_completed,
"contracts_failed": record.contracts_failed,
"success_rate": (
record.contracts_completed / max(1, record.contracts_completed + record.contracts_failed)
),
"robustness": {
"cc": record.current_robustness.cc,
"er": record.current_robustness.er,
"as": record.current_robustness.as_,
"ih": record.current_robustness.ih,
} if record.current_robustness else None,
# Audit data provenance — critical for paper claims
"audit_data_source": aq["source"],
"audit_dims_real": aq["dims_real"],
"audit_dims_defaulted": aq["dims_defaulted"],
"llm_usage": usage,
"strategy": strategy_name,
# v2 AutonomousAgent metrics
"autonomous_metrics": autonomous.metrics_summary() if autonomous else None,
})
# Gini coefficient of balances
balances = sorted([a["balance"] for a in agents_data])
gini = self._compute_gini(balances)
# Tier distribution
tier_dist = self.economy.registry.tier_distribution()
# Per-round trajectory
safety_trajectory = []
for snap in self.economy.snapshots:
safety_trajectory.append({
"time": snap.timestamp,
"safety": snap.aggregate_safety,
"active_agents": snap.num_agents,
"total_balance": snap.total_balance,
})
# Verification stats
v_summary = self.verifier.summary() if self.verifier else {}
# Total token costs
total_token_cost = sum(self._token_costs.values())
event_counts = {}
for e in self._protocol_events:
t = e.get("type", "UNKNOWN")
event_counts[t] = event_counts.get(t, 0) + 1
delegation_attempts = sum(1 for r in self._results if r.get("delegation") is not None)
delegation_allowed = sum(
1 for r in self._results
if (r.get("delegation") or {}).get("allowed") is True
)
circumvention_blocked = event_counts.get("CIRCUMVENTION_BLOCKED", 0)
# Data quality audit — list agents with unverified robustness dimensions
unaudited_agents = [
{
"model_name": a["model_name"],
"audit_source": a["audit_data_source"],
"dims_defaulted": a["audit_dims_defaulted"],
"tier_name": a["tier_name"],
}
for a in agents_data
if a["audit_dims_defaulted"]
]
self._final_summary = {
"economy": {
"aggregate_safety": self.economy.aggregate_safety(),
"total_rewards_paid": sum(r["total_reward"] for r in self._round_summaries),
"total_penalties_collected": sum(r["total_penalty"] for r in self._round_summaries),
"total_token_cost_eth": total_token_cost,
"usd_to_eth_rate": USD_TO_ETH,
"gini_coefficient": gini,
"num_rounds": self.config.num_rounds,
"num_agents": len(agents_data),
"active_agents": len(self.economy.registry.active_agents),
"test_eth_topups_total": self._test_eth_topups_total,
},
"demo_highlights": {
"protocol_event_counts": event_counts,
"delegation_attempts": delegation_attempts,
"delegation_allowed": delegation_allowed,
"delegation_blocked": max(0, delegation_attempts - delegation_allowed),
"circumvention_blocked": circumvention_blocked,
},
"tier_distribution": {t.name: c for t, c in tier_dist.items()},
"verification": v_summary,
"agents": sorted(agents_data, key=lambda a: a["balance"], reverse=True),
"safety_trajectory": safety_trajectory,
# ---------------------------------------------------------------
# Paper note: agents listed here have one or more robustness
# dimensions drawn from DEFAULT_ROBUSTNESS rather than verified
# framework results. Their tier assignments are estimates, not
# certified values. They should be reported separately from
# fully-audited agents in any empirical claim about CGAE gating.
# ---------------------------------------------------------------
"data_quality_warnings": {
"num_partially_or_fully_defaulted": len(unaudited_agents),
"unaudited_agents": unaudited_agents,
},
}
@staticmethod
def _compute_gini(values: list[float]) -> float:
"""Compute Gini coefficient for a sorted list of values."""
n = len(values)
if n == 0:
return 0.0
total = sum(values)
if total == 0:
return 0.0
cumulative = 0.0
weighted_sum = 0.0
for i, v in enumerate(values):
cumulative += v
weighted_sum += (2 * (i + 1) - n - 1) * v
return weighted_sum / (n * total)
def save_results(self, path: Optional[str] = None):
"""Save all results to disk."""
output_dir = Path(path or self.config.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
# Economy state
self.economy.export_state(str(output_dir / "economy_state.json"))
# Full task results
(output_dir / "task_results.json").write_text(
json.dumps(self._results, indent=2, default=str)
)
# Round summaries
(output_dir / "round_summaries.json").write_text(
json.dumps(self._round_summaries, indent=2, default=str)
)
# Protocol events for high-signal dashboard alerts
(output_dir / "protocol_events.json").write_text(
json.dumps(self._protocol_events, indent=2, default=str)
)
# Final summary
if self._final_summary:
(output_dir / "final_summary.json").write_text(
json.dumps(self._final_summary, indent=2, default=str)
)
# Verification summary
if self.verifier:
(output_dir / "verification_summary.json").write_text(
json.dumps(self.verifier.summary(), indent=2)
)
# Per-agent details
agent_details = {}
for agent_id, model_name in self.agent_model_map.items():
record = self.economy.registry.get_agent(agent_id)
if record:
llm = self.llm_agents.get(model_name)
agent_details[model_name] = {
**record.to_dict(),
"llm_usage": llm.usage_summary() if llm else {},
"token_cost_eth": self._token_costs.get(agent_id, 0.0),
}
(output_dir / "agent_details.json").write_text(
json.dumps(agent_details, indent=2, default=str)
)
# Verification log
if self.verifier:
log_data = [v.to_dict() for v in self.verifier.verification_log]
(output_dir / "verification_log.json").write_text(
json.dumps(log_data, indent=2, default=str)
)
logger.info(f"Results saved to {output_dir}")
def main():
"""Entry point for running the live simulation."""
parser = argparse.ArgumentParser(description="Run the CGAE live economy simulation.")
parser.add_argument("--live", action="store_true", help="Run in infinite loop mode for dashboard.")
parser.add_argument("--rounds", type=int, default=10, help="Number of rounds (ignored if --live is set).")
parser.add_argument("--video-demo", action="store_true", help="Run curated 5-min video demo (3 agents, adversarial blocking).")
parser.add_argument(
"--show-failures",
action="store_true",
help="Bias live execution toward harder tasks and disable self-check retries.",
)
args = parser.parse_args()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
)
# Check env vars
required_vars = ["AZURE_API_KEY"]
optional_vars = ["AZURE_OPENAI_API_ENDPOINT", "DDFT_MODELS_ENDPOINT"]
missing = [v for v in required_vars if not os.environ.get(v)]
if missing:
print(f"ERROR: Missing required environment variables: {missing}")
print(f"Optional (for more models): {optional_vars}")
print("\nSet them with:")
print(" export AZURE_API_KEY=your-key")
print(" export AZURE_OPENAI_API_ENDPOINT=https://your-endpoint.openai.azure.com/")
print(" export DDFT_MODELS_ENDPOINT=https://your-foundry-endpoint/v1")
return
available = [v for v in optional_vars if os.environ.get(v)]
print(f"Endpoints available: {available}")
# Framework API URLs are read from CDCT_API_URL / DDFT_API_URL / EECT_API_URL
# env vars by the clients. Override here if needed.
config = LiveSimConfig(
num_rounds=-1 if args.live else args.rounds,
seed=42,
video_demo=args.video_demo,
failure_visibility_mode=args.show_failures,
)
runner = LiveSimulationRunner(config)
results = runner.run()
runner.save_results()
# Print summary
print("\n" + "=" * 60)
print("CGAE LIVE ECONOMY - RESULTS")
print("=" * 60)
if runner._final_summary:
econ = runner._final_summary["economy"]
print(f"\nRounds: {econ['num_rounds']}")
print(f"Agents: {econ['num_agents']} ({econ['active_agents']} active)")
print(f"Aggregate safety: {econ['aggregate_safety']:.4f}")
print(f"Gini coefficient: {econ['gini_coefficient']:.4f}")
print(f"Total rewards: {econ['total_rewards_paid']:.4f} ETH")
print(f"Total penalties: {econ['total_penalties_collected']:.4f} ETH")
print(f"Total token costs: {econ['total_token_cost_eth']:.4f} ETH")
highlights = runner._final_summary.get("demo_highlights", {})
if highlights:
print("\nDemo highlights:")
print(f" Circumvention blocked: {highlights.get('circumvention_blocked', 0)}")
print(
f" Delegation attempts: {highlights.get('delegation_attempts', 0)} "
f"(allowed={highlights.get('delegation_allowed', 0)}, "
f"blocked={highlights.get('delegation_blocked', 0)})"
)
if runner.verifier:
vs = runner.verifier.summary()
print(f"\nVerification: {vs.get('total', 0)} tasks")
print(f" Algorithmic pass rate: {vs.get('algorithmic_pass_rate', 0):.1%}")
if vs.get("jury_pass_rate") is not None:
print(f" Jury pass rate: {vs['jury_pass_rate']:.1%}")
print(f" Overall pass rate: {vs.get('overall_pass_rate', 0):.1%}")
if vs.get("avg_jury_score") is not None:
print(f" Avg jury score: {vs['avg_jury_score']:.3f}")
print("\n--- Agent Leaderboard ---")
print(f" {'Model':40s} {'Tier':3s} {'Bal':>8} {'Earned':>8} "
f"{'Pen':>7} {'Cost':>7} W/L CC ER AS AuditSrc")
if runner._final_summary:
for a in runner._final_summary["agents"]:
r = a.get("robustness") or {}
# Show a short audit source tag; highlight defaulted dimensions
src = a.get("audit_data_source", "?")
defaulted = a.get("audit_dims_defaulted", [])
src_tag = src if not defaulted else f"{src}[def:{','.join(defaulted)}]"
print(
f" {a['model_name']:40s} | {a['tier_name']:3s} | "
f"bal={a['balance']:8.4f} | earned={a['total_earned']:8.4f} | "
f"pen={a['total_penalties']:7.4f} | cost={a['token_cost_eth']:7.4f} | "
f"W/L={a['contracts_completed']}/{a['contracts_failed']} | "
f"CC={r.get('cc', 0):.2f} ER={r.get('er', 0):.2f} AS={r.get('as', 0):.2f} | "
f"{src_tag}"
)
dqw = runner._final_summary.get("data_quality_warnings", {})
if dqw.get("num_partially_or_fully_defaulted", 0) > 0:
print(f"\n *** DATA QUALITY NOTE ***")
print(f" {dqw['num_partially_or_fully_defaulted']} agent(s) used assumed (not verified) "
f"robustness for one or more dimensions.")
print(f" These agents' tier assignments are estimates. See 'data_quality_warnings' "
f"in final_summary.json for details.")
print("\n" + "=" * 60)
if __name__ == "__main__":
main()
|