| 1 | package eventwire |
| 2 | |
| 3 | import ( |
| 4 | "reasonix/internal/checkpoint" |
| 5 | "reasonix/internal/event" |
| 6 | ) |
| 7 | |
| 8 | // CompletionReceipt is the JSON form of event.CompletionReceipt: what the host |
| 9 | // verified about a turn's work, and what it could not. |
| 10 | type CompletionReceipt struct { |
| 11 | AssessmentKind string `json:"assessmentKind,omitempty"` |
| 12 | Diff *checkpoint.TurnChanges `json:"diff,omitempty"` |
| 13 | Interrupted bool `json:"interrupted,omitempty"` |
| 14 | Verdict string `json:"verdict"` |
| 15 | Changes []ReceiptChange `json:"changes,omitempty"` |
| 16 | Verifications []ReceiptVerification `json:"verifications,omitempty"` |
| 17 | Gaps []ReceiptGap `json:"gaps,omitempty"` |
| 18 | Risks []string `json:"risks,omitempty"` |
| 19 | } |
| 20 | |
| 21 | type ReceiptChange struct { |
| 22 | Path string `json:"path"` |
| 23 | Reviewed bool `json:"reviewed"` |
| 24 | } |
| 25 | |
| 26 | type ReceiptVerification struct { |
| 27 | Command string `json:"command"` |
| 28 | Passed bool `json:"passed"` |
| 29 | Stale bool `json:"stale,omitempty"` |
| 30 | ToolCallID string `json:"toolCallId,omitempty"` |
| 31 | ToolResultID string `json:"toolResultId,omitempty"` |
| 32 | Interrupted bool `json:"interrupted,omitempty"` |
| 33 | ExitCode *int `json:"exitCode,omitempty"` |
| 34 | } |
| 35 | |
| 36 | type ReceiptGap struct { |
| 37 | Kind string `json:"kind"` |
| 38 | Detail string `json:"detail,omitempty"` |
| 39 | } |
| 40 | |
| 41 | // completionReceiptWire converts the receipt, nil in and nil out so the call |
| 42 | // site needs no branch. |
| 43 | func completionReceiptWire(r *event.CompletionReceipt) *CompletionReceipt { |
| 44 | if r == nil { |
| 45 | return nil |
| 46 | } |
| 47 | out := &CompletionReceipt{AssessmentKind: r.AssessmentKind, Verdict: r.Verdict, Risks: append([]string(nil), r.Risks...), Diff: r.Diff, Interrupted: r.Interrupted} |
| 48 | for _, c := range r.Changes { |
| 49 | out.Changes = append(out.Changes, ReceiptChange{Path: c.Path, Reviewed: c.Reviewed}) |
| 50 | } |
| 51 | for _, v := range r.Verifications { |
| 52 | out.Verifications = append(out.Verifications, ReceiptVerification{Command: v.Command, Passed: v.Passed, Stale: v.Stale, ToolCallID: v.ToolCallID, ToolResultID: v.ToolResultID, ExitCode: v.ExitCode, Interrupted: v.Interrupted}) |
| 53 | } |
| 54 | for _, g := range r.Gaps { |
| 55 | out.Gaps = append(out.Gaps, ReceiptGap{Kind: g.Kind, Detail: g.Detail}) |
| 56 | } |
| 57 | return out |
| 58 | } |
| 59 | |
| 60 | // CompletionReceiptEvent restores the host record when replaying durable events. |
| 61 | func CompletionReceiptEvent(r *CompletionReceipt) *event.CompletionReceipt { |
| 62 | if r == nil { |
| 63 | return nil |
| 64 | } |
| 65 | out := &event.CompletionReceipt{AssessmentKind: r.AssessmentKind, Verdict: r.Verdict, Diff: r.Diff, Interrupted: r.Interrupted, Risks: append([]string(nil), r.Risks...)} |
| 66 | for _, c := range r.Changes { |
| 67 | out.Changes = append(out.Changes, event.ReceiptChange{Path: c.Path, Reviewed: c.Reviewed}) |
| 68 | } |
| 69 | for _, v := range r.Verifications { |
| 70 | out.Verifications = append(out.Verifications, event.ReceiptVerification{Command: v.Command, Passed: v.Passed, Stale: v.Stale, ToolCallID: v.ToolCallID, ToolResultID: v.ToolResultID, ExitCode: v.ExitCode, Interrupted: v.Interrupted}) |
| 71 | } |
| 72 | for _, g := range r.Gaps { |
| 73 | out.Gaps = append(out.Gaps, event.ReceiptGap{Kind: g.Kind, Detail: g.Detail}) |
| 74 | } |
| 75 | return out |
| 76 | } |
| 77 |