| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "encoding/json" |
| 7 | |
| 8 | "reasonix/internal/checkpoint" |
| 9 | "reasonix/internal/event" |
| 10 | "reasonix/internal/provider" |
| 11 | ) |
| 12 | |
| 13 | func (c *Controller) stampToolRecoveryEvent(e event.Event) error { |
| 14 | if e.Kind != event.ToolStarted { |
| 15 | return nil |
| 16 | } |
| 17 | return c.stampToolRecoveryCheckpoint(e.Tool.AttemptID) |
| 18 | } |
| 19 | |
| 20 | func (c *Controller) stampToolRecoveryCheckpoint(attempt string) error { |
| 21 | if c.executor == nil || attempt == "" { |
| 22 | return nil |
| 23 | } |
| 24 | for _, record := range c.executor.PendingToolRecovery() { |
| 25 | if record.Identity.AttemptID != attempt || record.ReadOnly { |
| 26 | continue |
| 27 | } |
| 28 | c.checkpoints.mu.Lock() |
| 29 | store := c.checkpoints.store |
| 30 | c.checkpoints.mu.Unlock() |
| 31 | if store == nil { |
| 32 | return nil |
| 33 | } |
| 34 | payload, err := json.Marshal(provider.ModelMessages(c.executor.Session().Snapshot())) |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | digest := sha256.Sum256(payload) |
| 39 | return store.BindRecoveryIdentity(checkpoint.RecoveryIdentity{Action: record.Identity, TranscriptDigest: hex.EncodeToString(digest[:])}) |
| 40 | } |
| 41 | return nil |
| 42 | } |
| 43 |