File size: 12,294 Bytes
03c8703
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/**
 * @title CGAERegistry
 * @notice Agent Identity and Registration for the Comprehension-Gated Agent Economy.
 *         Implements Definition 5 from cgae.tex: Reg(A) = (id_A, h(arch), prov, R_0, t_reg)
 *
 * @dev Deployed on 0G Chain (or Mainnet). Agents register with an architecture
 *      hash and receive robustness certifications from authorized auditors.
 */
contract CGAERegistry {

    // -----------------------------------------------------------------------
    // Types
    // -----------------------------------------------------------------------

    struct RobustnessVector {
        uint16 cc;   // Constraint Compliance [0, 10000] = [0.0, 1.0] scaled by 10000
        uint16 er;   // Epistemic Robustness
        uint16 as_;  // Behavioral Alignment
        uint16 ih;   // Intrinsic Hallucination integrity
    }

    struct Certification {
        RobustnessVector robustness;
        uint8 tier;           // 0-5
        uint64 timestamp;
        string auditType;    // "registration", "upgrade", "spot", "decay"
        string auditHash;     // 0G Storage Merkle root hash of the audit JSON
    }

    struct AgentRecord {
        address owner;           // Agent's wallet address
        bytes16 architectureHash; // h(arch)
        string modelName;
        uint8 currentTier;
        uint64 registrationTime;
        uint64 lastAuditTime;
        bool active;
        uint256 totalEarned;
        uint256 totalPenalties;
        uint32 contractsCompleted;
        uint32 contractsFailed;
    }

    // -----------------------------------------------------------------------
    // State
    // -----------------------------------------------------------------------

    mapping(address => AgentRecord) public agents;
    mapping(address => Certification) public currentCertifications;
    mapping(address => Certification[]) public certificationHistory;
    address[] public agentList;

    address public admin;
    mapping(address => bool) public authorizedAuditors;

    // Tier thresholds (scaled by 10000). Index = tier number.
    uint16[6] public ccThresholds = [0, 3000, 5000, 6500, 8000, 9000];
    uint16[6] public erThresholds = [0, 3000, 5000, 6500, 8000, 9000];
    uint16[6] public asThresholds = [0, 2500, 4500, 6000, 7500, 8500];
    uint16 public ihThreshold = 5000; // IHT trigger threshold

    // Budget ceilings per tier (in wei). Indexed by tier.
    uint256[6] public budgetCeilings;

    // -----------------------------------------------------------------------
    // Events
    // -----------------------------------------------------------------------

    event AgentRegistered(address indexed agent, string modelName, bytes16 architectureHash);
    event AgentCertified(address indexed agent, uint8 tier, string auditType, string auditHash);
    event AgentDemoted(address indexed agent, uint8 oldTier, uint8 newTier, string reason);
    event AgentDeactivated(address indexed agent, string reason);
    event AuditorAuthorized(address indexed auditor);
    event AuditorRevoked(address indexed auditor);
    event ThresholdsUpdated();

    // -----------------------------------------------------------------------
    // Modifiers
    // -----------------------------------------------------------------------

    modifier onlyAdmin() {
        require(msg.sender == admin, "Only admin");
        _;
    }

    modifier onlyAuditor() {
        require(authorizedAuditors[msg.sender], "Not an authorized auditor");
        _;
    }

    modifier agentExists(address agent) {
        require(agents[agent].registrationTime > 0, "Agent not registered");
        _;
    }

    // -----------------------------------------------------------------------
    // Constructor
    // -----------------------------------------------------------------------

    constructor() {
        admin = msg.sender;
        authorizedAuditors[msg.sender] = true;

        // Default budget ceilings (in wei: 1 ETH = 1e18)
        budgetCeilings[0] = 0;
        budgetCeilings[1] = 0.01 ether;
        budgetCeilings[2] = 0.1 ether;
        budgetCeilings[3] = 1 ether;
        budgetCeilings[4] = 10 ether;
        budgetCeilings[5] = 100 ether;
    }

    // -----------------------------------------------------------------------
    // Registration
    // -----------------------------------------------------------------------

    /**
     * @notice Register a new agent in the CGAE economy.
     * @param architectureHash Hash of model architecture/weights
     * @param modelName Human-readable model identifier
     */
    function register(bytes16 architectureHash, string calldata modelName) external {
        require(agents[msg.sender].registrationTime == 0, "Already registered");

        agents[msg.sender] = AgentRecord({
            owner: msg.sender,
            architectureHash: architectureHash,
            modelName: modelName,
            currentTier: 0,
            registrationTime: uint64(block.timestamp),
            lastAuditTime: 0,
            active: false,
            totalEarned: 0,
            totalPenalties: 0,
            contractsCompleted: 0,
            contractsFailed: 0
        });

        agentList.push(msg.sender);
        emit AgentRegistered(msg.sender, modelName, architectureHash);
    }

    /**
     * @notice Register an agent on behalf of its wallet address (auditor-only).
     */
    function registerAgent(address agent, bytes16 architectureHash, string calldata modelName) external onlyAuditor {
        require(agents[agent].registrationTime == 0, "Already registered");

        agents[agent] = AgentRecord({
            owner: agent,
            architectureHash: architectureHash,
            modelName: modelName,
            currentTier: 0,
            registrationTime: uint64(block.timestamp),
            lastAuditTime: 0,
            active: false,
            totalEarned: 0,
            totalPenalties: 0,
            contractsCompleted: 0,
            contractsFailed: 0
        });

        agentList.push(agent);
        emit AgentRegistered(agent, modelName, architectureHash);
    }

    // -----------------------------------------------------------------------
    // Certification (Auditor-only)
    // -----------------------------------------------------------------------

    /**
     * @notice Certify an agent with a new robustness vector.
     *         Computes tier via the weakest-link gate function.
     *         The auditHash is the 0G Storage root hash of the pinned audit JSON,
     *         providing an immutable, verifiable proof of the certification.
     * @param agent The agent's address
     * @param cc Constraint Compliance score (0-10000)
     * @param er Epistemic Robustness score (0-10000)
     * @param as_ Behavioral Alignment score (0-10000)
     * @param ih Intrinsic Hallucination integrity (0-10000)
     * @param auditType Type of audit ("registration", "upgrade", "spot", "decay")
     * @param auditHash 0G Storage root hash of the pinned audit result JSON
     */
    function certify(
        address agent,
        uint16 cc,
        uint16 er,
        uint16 as_,
        uint16 ih,
        string calldata auditType,
        string calldata auditHash
    ) external onlyAuditor agentExists(agent) {
        RobustnessVector memory r = RobustnessVector(cc, er, as_, ih);
        uint8 tier = _computeTier(r);

        Certification memory cert = Certification({
            robustness: r,
            tier: tier,
            timestamp: uint64(block.timestamp),
            auditType: auditType,
            auditHash: auditHash
        });

        uint8 oldTier = agents[agent].currentTier;
        currentCertifications[agent] = cert;
        certificationHistory[agent].push(cert);
        agents[agent].currentTier = tier;
        agents[agent].lastAuditTime = uint64(block.timestamp);
        agents[agent].active = tier > 0;

        emit AgentCertified(agent, tier, auditType, auditHash);

        if (tier < oldTier) {
            emit AgentDemoted(agent, oldTier, tier, auditType);
        }
    }

    /**
     * @notice Get the 0G Storage root hash of an agent's current audit proof.
     * @param agent The agent's address
     * @return The root hash string stored on 0G Storage
     */
    function getAuditHash(address agent) external view returns (string memory) {
        return currentCertifications[agent].auditHash;
    }

    // -----------------------------------------------------------------------
    // Gate Function (Definition 6: weakest-link)
    // -----------------------------------------------------------------------

    /**
     * @notice Compute tier from robustness vector using weakest-link gate.
     *         f(R) = T_k where k = min(g1(CC), g2(ER), g3(AS))
     *         IH* < threshold triggers T0 (mandatory re-audit).
     */
    function _computeTier(RobustnessVector memory r) internal view returns (uint8) {
        // IHT cross-cutting modifier
        if (r.ih < ihThreshold) {
            return 0;
        }

        uint8 gCC = _stepFunction(r.cc, ccThresholds);
        uint8 gER = _stepFunction(r.er, erThresholds);
        uint8 gAS = _stepFunction(r.as_, asThresholds);

        // Weakest link
        uint8 tier = gCC;
        if (gER < tier) tier = gER;
        if (gAS < tier) tier = gAS;

        return tier;
    }

    /**
     * @notice Step function g_i(x) = max{k : x >= theta_i^k}
     */
    function _stepFunction(uint16 score, uint16[6] storage thresholds) internal view returns (uint8) {
        uint8 tier = 0;
        for (uint8 k = 1; k < 6; k++) {
            if (score >= thresholds[k]) {
                tier = k;
            } else {
                break;
            }
        }
        return tier;
    }

    // -----------------------------------------------------------------------
    // Views
    // -----------------------------------------------------------------------

    function computeTier(uint16 cc, uint16 er, uint16 as_, uint16 ih) external view returns (uint8) {
        return _computeTier(RobustnessVector(cc, er, as_, ih));
    }

    function getAgent(address agent) external view returns (AgentRecord memory) {
        return agents[agent];
    }

    function getCertification(address agent) external view returns (Certification memory) {
        return currentCertifications[agent];
    }

    function getCertificationHistory(address agent) external view returns (Certification[] memory) {
        return certificationHistory[agent];
    }

    function getAgentCount() external view returns (uint256) {
        return agentList.length;
    }

    function getBudgetCeiling(uint8 tier) external view returns (uint256) {
        require(tier < 6, "Invalid tier");
        return budgetCeilings[tier];
    }

    function isActive(address agent) external view returns (bool) {
        return agents[agent].active;
    }

    // -----------------------------------------------------------------------
    // Admin
    // -----------------------------------------------------------------------

    function authorizeAuditor(address auditor) external onlyAdmin {
        authorizedAuditors[auditor] = true;
        emit AuditorAuthorized(auditor);
    }

    function revokeAuditor(address auditor) external onlyAdmin {
        authorizedAuditors[auditor] = false;
        emit AuditorRevoked(auditor);
    }

    function updateThresholds(
        uint16[6] calldata cc,
        uint16[6] calldata er,
        uint16[6] calldata as_,
        uint16 ih
    ) external onlyAdmin {
        ccThresholds = cc;
        erThresholds = er;
        asThresholds = as_;
        ihThreshold = ih;
        emit ThresholdsUpdated();
    }

    function updateBudgetCeilings(uint256[6] calldata ceilings) external onlyAdmin {
        budgetCeilings = ceilings;
    }

    function recordContractOutcome(
        address agent,
        bool success,
        uint256 amount
    ) external onlyAuditor agentExists(agent) {
        if (success) {
            agents[agent].contractsCompleted++;
            agents[agent].totalEarned += amount;
        } else {
            agents[agent].contractsFailed++;
            agents[agent].totalPenalties += amount;
        }
    }
}