Diff reconciles contracts against runtime effects and returns typed diffs.
(contracts []IntentContract, effects []RuntimeEffect)
| 52 | |
| 53 | // Diff reconciles contracts against runtime effects and returns typed diffs. |
| 54 | func 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 |