| 1 | package evidence |
| 2 | |
| 3 | import "encoding/json" |
| 4 | |
| 5 | // FinalReadinessCheckpoint is the bounded, durable copy of a failed turn's |
| 6 | // host evidence, stored only in provider-excluded local metadata. |
| 7 | type FinalReadinessCheckpoint struct { |
| 8 | Receipts []Receipt `json:"receipts,omitempty"` |
| 9 | } |
| 10 | |
| 11 | // FinalReadinessCheckpoint snapshots the ledger without duplicating writer |
| 12 | // payloads, patches, or delegated prompts. |
| 13 | func (l *Ledger) FinalReadinessCheckpoint() FinalReadinessCheckpoint { |
| 14 | if l == nil { |
| 15 | return FinalReadinessCheckpoint{} |
| 16 | } |
| 17 | l.mu.Lock() |
| 18 | defer l.mu.Unlock() |
| 19 | receipts := make([]Receipt, len(l.receipts)) |
| 20 | for i, receipt := range l.receipts { |
| 21 | receipts[i] = receipt |
| 22 | receipts[i].Args = recoveryReceiptArgs(receipt) |
| 23 | } |
| 24 | return FinalReadinessCheckpoint{Receipts: receipts} |
| 25 | } |
| 26 | |
| 27 | // RestoreFinalReadinessCheckpoint rebuilds the ordered ledger through Record |
| 28 | // so live and restored receipts receive identical normalization. |
| 29 | func (l *Ledger) RestoreFinalReadinessCheckpoint(checkpoint FinalReadinessCheckpoint) bool { |
| 30 | if l == nil { |
| 31 | return false |
| 32 | } |
| 33 | l.Reset() |
| 34 | for _, receipt := range checkpoint.Receipts { |
| 35 | l.Record(receipt) |
| 36 | } |
| 37 | return true |
| 38 | } |
| 39 | |
| 40 | func recoveryReceiptArgs(r Receipt) json.RawMessage { |
| 41 | if isShellToolName(r.ToolName) { |
| 42 | if r.Command == "" { |
| 43 | return nil |
| 44 | } |
| 45 | args, _ := json.Marshal(map[string]string{"command": r.Command}) |
| 46 | return args |
| 47 | } |
| 48 | switch r.ToolName { |
| 49 | case "complete_step", "review_report", "complete_subtask": |
| 50 | return append(json.RawMessage(nil), r.Args...) |
| 51 | default: |
| 52 | // Derived receipt fields retain readiness facts; raw writer and |
| 53 | // delegation arguments are intentionally not duplicated on disk. |
| 54 | return nil |
| 55 | } |
| 56 | } |
| 57 |