File size: 6,738 Bytes
a98a7f5
c5e904d
a98a7f5
 
c5e904d
 
a98a7f5
 
 
 
 
 
 
c5e904d
 
 
 
 
 
 
 
 
a98a7f5
 
 
 
c5e904d
a98a7f5
c5e904d
 
a98a7f5
 
 
 
 
 
 
 
 
 
c5e904d
a98a7f5
c5e904d
 
 
a98a7f5
c5e904d
 
a98a7f5
 
 
 
 
 
 
 
c5e904d
a98a7f5
 
 
c5e904d
a98a7f5
 
 
 
c5e904d
a98a7f5
 
 
 
 
 
 
c5e904d
a98a7f5
c5e904d
 
 
 
a98a7f5
c5e904d
a98a7f5
c5e904d
a98a7f5
c5e904d
a98a7f5
 
 
 
 
 
 
c5e904d
a98a7f5
c5e904d
 
 
a98a7f5
 
 
 
c5e904d
 
a98a7f5
 
 
 
 
 
c5e904d
a98a7f5
 
c5e904d
a98a7f5
 
 
 
 
c5e904d
a98a7f5
 
 
c5e904d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a98a7f5
c5e904d
 
 
a98a7f5
 
c5e904d
 
a98a7f5
 
 
 
c5e904d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a98a7f5
 
 
 
c5e904d
 
 
a98a7f5
c5e904d
 
a98a7f5
c5e904d
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Credit Ledger - non-transferable, decaying credits with full provenance.
"""
import time
from dataclasses import dataclass, field
from typing import Dict, List, Optional


@dataclass
class LedgerEntry:
    agent_id: str
    task_id: str
    action_id: str
    earned_credit: float
    spent_credit: float
    decayed_credit: float
    remaining_credit: float
    reason: str
    oracle_score: float
    compute_cost: float
    timestamp: float
    capability_scope: str = "global"
    task_scope: str = "global"


class CreditLedger:
    def __init__(self, decay_lambda: float = 0.05):
        self.entries: List[LedgerEntry] = []
        self.agent_balances: Dict[str, Dict[str, Dict[str, float]]] = {}
        self.decay_lambda = decay_lambda

    def earn(
        self,
        agent_id: str,
        task_id: str,
        action_id: str,
        amount: float,
        oracle_score: float,
        compute_cost: float,
        reason: str,
        capability_scope: str = "global",
        task_scope: str = "global",
    ) -> None:
        now = time.time()
        self._apply_decay(agent_id, now, capability_scope, task_scope)

        current = self._get_balance(agent_id, capability_scope, task_scope)
        new_balance = current + amount

        entry = LedgerEntry(
            agent_id=agent_id,
            task_id=task_id,
            action_id=action_id,
            earned_credit=amount,
            spent_credit=0.0,
            decayed_credit=0.0,
            remaining_credit=new_balance,
            reason=reason,
            oracle_score=oracle_score,
            compute_cost=compute_cost,
            timestamp=now,
            capability_scope=capability_scope,
            task_scope=task_scope,
        )
        self.entries.append(entry)
        self._set_balance(agent_id, capability_scope, task_scope, new_balance)

    def spend(
        self,
        agent_id: str,
        task_id: str,
        action_id: str,
        amount: float,
        capability_scope: str = "global",
        task_scope: str = "global",
        reason: str = "spend",
    ) -> bool:
        now = time.time()
        self._apply_decay(agent_id, now, capability_scope, task_scope)

        current = self._get_balance(agent_id, capability_scope, task_scope)
        if current < amount:
            return False

        new_balance = current - amount
        entry = LedgerEntry(
            agent_id=agent_id,
            task_id=task_id,
            action_id=action_id,
            earned_credit=0.0,
            spent_credit=amount,
            decayed_credit=0.0,
            remaining_credit=new_balance,
            reason=reason,
            oracle_score=0.0,
            compute_cost=0.0,
            timestamp=now,
            capability_scope=capability_scope,
            task_scope=task_scope,
        )
        self.entries.append(entry)
        self._set_balance(agent_id, capability_scope, task_scope, new_balance)
        return True

    def transfer(
        self,
        from_agent: str,
        to_agent: str,
        amount: float,
        capability_scope: str = "global",
        task_scope: str = "global",
    ) -> bool:
        # CREDITS ARE NON-TRANSFERABLE
        return False

    def balance(
        self,
        agent_id: str,
        capability_scope: str = "global",
        task_scope: str = "global",
    ) -> float:
        now = time.time()
        self._apply_decay(agent_id, now, capability_scope, task_scope)
        return self._get_balance(agent_id, capability_scope, task_scope)

    def _get_balance(self, agent_id: str, cap: str, task: str) -> float:
        return self.agent_balances.get(agent_id, {}).get(cap, {}).get(task, 0.0)

    def _set_balance(self, agent_id: str, cap: str, task: str, val: float) -> None:
        if agent_id not in self.agent_balances:
            self.agent_balances[agent_id] = {}
        if cap not in self.agent_balances[agent_id]:
            self.agent_balances[agent_id][cap] = {}
        self.agent_balances[agent_id][cap][task] = val

    def _apply_decay(self, agent_id: str, now: float, cap: str, task: str) -> None:
        current = self._get_balance(agent_id, cap, task)
        if current <= 0:
            return

        # Exponential decay of idle credits
        decayed = current * (1 - self.decay_lambda)
        if decayed < current:
            entry = LedgerEntry(
                agent_id=agent_id,
                task_id="decay",
                action_id="decay",
                earned_credit=0.0,
                spent_credit=0.0,
                decayed_credit=current - decayed,
                remaining_credit=decayed,
                reason="credit_decay",
                oracle_score=0.0,
                compute_cost=0.0,
                timestamp=now,
                capability_scope=cap,
                task_scope=task,
            )
            self.entries.append(entry)
            self._set_balance(agent_id, cap, task, decayed)

    def revoke(
        self,
        agent_id: str,
        task_id: str,
        amount: float,
        reason: str,
        capability_scope: str = "global",
        task_scope: str = "global",
    ) -> bool:
        now = time.time()
        self._apply_decay(agent_id, now, capability_scope, task_scope)
        current = self._get_balance(agent_id, capability_scope, task_scope)
        revoke_amount = min(current, amount)
        if revoke_amount > 0:
            new_balance = current - revoke_amount
            entry = LedgerEntry(
                agent_id=agent_id,
                task_id=task_id,
                action_id="revoke",
                earned_credit=0.0,
                spent_credit=revoke_amount,
                decayed_credit=0.0,
                remaining_credit=new_balance,
                reason=f"revoke: {reason}",
                oracle_score=0.0,
                compute_cost=0.0,
                timestamp=now,
                capability_scope=capability_scope,
                task_scope=task_scope,
            )
            self.entries.append(entry)
            self._set_balance(agent_id, capability_scope, task_scope, new_balance)
            return True
        return False

    def provenance(self, agent_id: str) -> List[LedgerEntry]:
        return [e for e in self.entries if e.agent_id == agent_id]

    def gaming_detected(
        self,
        agent_id: str,
        task_id: str,
        action_id: str,
        reason: str,
        capability_scope: str = "global",
    ) -> None:
        # Immediate revocation of credits on gaming detection
        self.revoke(
            agent_id, task_id, 999.0,
            reason=f"gaming_detected: {reason}",
            capability_scope=capability_scope,
        )