MCPcopy Create free account
hub / github.com/ByteYellow/AgentProvenance / Diff

Function Diff

internal/intent/diff.go:54–178  ·  view source on GitHub ↗

Diff reconciles contracts against runtime effects and returns typed diffs.

(contracts []IntentContract, effects []RuntimeEffect)

Source from the content-addressed store, hash-verified

52
53// Diff reconciles contracts against runtime effects and returns typed diffs.
54func Diff(contracts []IntentContract, effects []RuntimeEffect) []IntentRuntimeDiff {
55 byAgent := map[string][]RuntimeEffect{}
56 byToolCall := map[string][]RuntimeEffect{}
57 contractedAgents := map[string]bool{}
58 for _, e := range effects {
59 byAgent[e.AgentID] = append(byAgent[e.AgentID], e)
60 if e.ToolCallID != "" {
61 byToolCall[e.ToolCallID] = append(byToolCall[e.ToolCallID], e)
62 }
63 }
64
65 var out []IntentRuntimeDiff
66 for _, c := range contracts {
67 contractedAgents[c.ScopeAgent] = true
68
69 // Scope the effects a contract is answerable for: a tool_call contract
70 // owns its own tool_call's effects; a peer_message/refusal contract
71 // governs everything its target agent did.
72 scope := byAgent[c.ScopeAgent]
73 if c.Kind == ContractToolCall && c.ToolCallID != "" {
74 if te, ok := byToolCall[c.ToolCallID]; ok {
75 scope = te
76 } else {
77 scope = nil // no effects attributed to this specific tool call
78 }
79 }
80
81 forbidden := c.Profile.forbiddenSet()
82 if c.Negative {
83 // A refusal forbids every baseline-sensitive effect outright.
84 forbidden = map[EffectKind]bool{}
85 for _, e := range baselineForbidden {
86 forbidden[e] = true
87 }
88 }
89
90 var violating []RuntimeEffect
91 observedKinds := map[EffectKind]bool{}
92 for _, e := range scope {
93 observedKinds[e.Kind] = true
94 if forbidden[e.Kind] {
95 violating = append(violating, e)
96 }
97 }
98 if len(violating) == 0 {
99 // No boundary violation. Emit a green baseline only when the declared
100 // effects actually appeared (proof the intent executed as claimed).
101 if declaredObserved(c.Profile.Declared, observedKinds) && len(scope) > 0 {
102 out = append(out, IntentRuntimeDiff{
103 ID: "diff/" + trimContract(c.ID) + "/ok", RunID: "", AgentID: c.ScopeAgent, ToolCallID: c.ToolCallID,
104 ContractKind: c.Kind, Operation: c.Operation, Target: c.Target,
105 Status: StatusExecuted, Confidence: c.Confidence,
106 Declared: c.Profile.Declared, Observed: kindsOf(scope), Source: c.Source,
107 })
108 }
109 continue
110 }
111

Calls 11

declaredObservedFunction · 0.85
trimContractFunction · 0.85
kindsOfFunction · 0.85
joinKindsFunction · 0.85
shortenFunction · 0.85
violationConfidenceFunction · 0.85
sortedForbiddenFunction · 0.85
eventIDsOfFunction · 0.85
isBaselineForbiddenFunction · 0.85
forbiddenSetMethod · 0.80
statusRankFunction · 0.70