ExtractContracts builds every IntentContract for a run from the captured intent surfaces: each agent tool call (via its profile), each peer message (governing the recipient), and each refusal (a negative contract).
(db *sql.DB, runID string, profiles map[string]Profile)
| 42 | // intent surfaces: each agent tool call (via its profile), each peer message |
| 43 | // (governing the recipient), and each refusal (a negative contract). |
| 44 | func ExtractContracts(db *sql.DB, runID string, profiles map[string]Profile) ([]IntentContract, error) { |
| 45 | var out []IntentContract |
| 46 | |
| 47 | rows, err := db.Query(`SELECT id, agent_id, command, status FROM tool_calls |
| 48 | WHERE run_id = ? AND agent_id != '' AND command != ''`, runID) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | for rows.Next() { |
| 53 | var id, agentID, command, status string |
| 54 | if err := rows.Scan(&id, &agentID, &command, &status); err != nil { |
| 55 | rows.Close() |
| 56 | return nil, err |
| 57 | } |
| 58 | if status == "refused" || strings.HasPrefix(id, "refusal-") { |
| 59 | // A refusal is a negative contract: the agent said it would not act, |
| 60 | // so any baseline-sensitive effect later attributed to it is a bypass. |
| 61 | out = append(out, IntentContract{ |
| 62 | ID: "contract/" + id, |
| 63 | Kind: ContractRefusal, |
| 64 | ScopeAgent: agentID, |
| 65 | ToolCallID: id, |
| 66 | Operation: "refusal", |
| 67 | Target: strings.TrimPrefix(command, "[llm refusal] "), |
| 68 | Profile: Profile{Operation: "refusal"}, |
| 69 | Source: "refusal " + id, |
| 70 | Confidence: assertedConfidence, |
| 71 | Negative: true, |
| 72 | }) |
| 73 | continue |
| 74 | } |
| 75 | op := inferOperation(command) |
| 76 | out = append(out, IntentContract{ |
| 77 | ID: "contract/" + id, |
| 78 | Kind: ContractToolCall, |
| 79 | ScopeAgent: agentID, |
| 80 | ToolCallID: id, |
| 81 | Operation: op, |
| 82 | Target: command, |
| 83 | Profile: profileFor(profiles, op), |
| 84 | Source: "tool_call " + id, |
| 85 | Confidence: assertedConfidence, |
| 86 | }) |
| 87 | } |
| 88 | rows.Close() |
| 89 | |
| 90 | msgs, err := extractMessageContracts(db, runID, profiles) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | out = append(out, msgs...) |
| 95 | return out, nil |
| 96 | } |
| 97 | |
| 98 | // extractMessageContracts turns each peer SendMessage into a contract that |
| 99 | // governs the RECIPIENT: the instruction alice sends bob ("install X") declares |
no test coverage detected