This repository was archived by the owner on Mar 7, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtrust.go
More file actions
111 lines (95 loc) · 2.81 KB
/
Copy pathtrust.go
File metadata and controls
111 lines (95 loc) · 2.81 KB
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
package agentmesh
import (
"math"
"sync"
)
// TrustScore represents an agent's current trust standing.
type TrustScore struct {
Overall float64 `json:"overall"`
Dimensions map[string]float64 `json:"dimensions"`
Tier string `json:"tier"`
}
type scoreState struct {
score float64
interactions int
}
// TrustManager tracks and updates per-agent trust scores.
type TrustManager struct {
mu sync.RWMutex
config TrustConfig
scores map[string]*scoreState
}
// NewTrustManager creates a TrustManager with the given config.
func NewTrustManager(config TrustConfig) *TrustManager {
return &TrustManager{
config: config,
scores: make(map[string]*scoreState),
}
}
// VerifyPeer verifies a peer's identity and returns the current trust score.
func (tm *TrustManager) VerifyPeer(peerID string, peerIdentity *AgentIdentity) (*TrustVerificationResult, error) {
verified := peerIdentity != nil && peerIdentity.PublicKey != nil && len(peerIdentity.PublicKey) == 32
score := tm.GetTrustScore(peerID)
return &TrustVerificationResult{
PeerID: peerID,
Verified: verified,
Score: score,
}, nil
}
// GetTrustScore returns the current trust score for an agent.
func (tm *TrustManager) GetTrustScore(agentID string) TrustScore {
tm.mu.RLock()
defer tm.mu.RUnlock()
s, ok := tm.scores[agentID]
if !ok {
return TrustScore{
Overall: tm.config.InitialScore,
Dimensions: map[string]float64{"reliability": tm.config.InitialScore},
Tier: tm.tierFor(tm.config.InitialScore),
}
}
return TrustScore{
Overall: s.score,
Dimensions: map[string]float64{"reliability": s.score},
Tier: tm.tierFor(s.score),
}
}
// RecordSuccess increases an agent's trust score with decay.
func (tm *TrustManager) RecordSuccess(agentID string, reward float64) {
tm.mu.Lock()
defer tm.mu.Unlock()
s := tm.getOrCreate(agentID)
s.interactions++
decayed := tm.applyDecay(s.score)
s.score = math.Min(1.0, decayed+reward*tm.config.RewardFactor)
}
// RecordFailure decreases an agent's trust score with asymmetric penalty.
func (tm *TrustManager) RecordFailure(agentID string, penalty float64) {
tm.mu.Lock()
defer tm.mu.Unlock()
s := tm.getOrCreate(agentID)
s.interactions++
decayed := tm.applyDecay(s.score)
s.score = math.Max(0.0, decayed-penalty*tm.config.PenaltyFactor)
}
func (tm *TrustManager) getOrCreate(agentID string) *scoreState {
s, ok := tm.scores[agentID]
if !ok {
s = &scoreState{score: tm.config.InitialScore}
tm.scores[agentID] = s
}
return s
}
func (tm *TrustManager) applyDecay(score float64) float64 {
return score * (1.0 - tm.config.DecayRate)
}
func (tm *TrustManager) tierFor(score float64) string {
switch {
case score >= tm.config.TierThresholds.High:
return "high"
case score >= tm.config.TierThresholds.Medium:
return "medium"
default:
return "low"
}
}