| 1 | package hostclaim |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | type operationLossGate struct { |
| 10 | Box string `json:"box"` |
| 11 | RevokeExecution bool `json:"revoke_execution"` |
| 12 | } |
| 13 | |
| 14 | // OperationLossID selects only a declaration journalled under this operation. |
| 15 | // Old claim-level LossRefs alone do not turn a later move into a loss recovery. |
| 16 | func OperationLossID(c *Claim) (string, error) { |
| 17 | if c == nil { |
| 18 | return "", ErrLossRecord |
| 19 | } |
| 20 | id := "" |
| 21 | for key := range c.Op.Journal { |
| 22 | if !strings.HasPrefix(key, "loss.") { |
| 23 | continue |
| 24 | } |
| 25 | next := strings.TrimPrefix(key, "loss.") |
| 26 | if c.Op.ID == "" || !safeLossID(next) || !lossRefPresent(c, next) { |
| 27 | return "", ErrLossRecord |
| 28 | } |
| 29 | var gate operationLossGate |
| 30 | if raw := c.Op.Journal["loss_gate."+next]; raw != "" { |
| 31 | if json.Unmarshal([]byte(raw), &gate) != nil || gate.Box == "" { |
| 32 | return "", ErrLossRecord |
| 33 | } |
| 34 | } else { |
| 35 | var legacy struct { |
| 36 | Kind, Target string |
| 37 | RevokeExecution bool `json:"revoke_execution"` |
| 38 | } |
| 39 | if json.Unmarshal([]byte(c.Op.Journal[key]), &legacy) != nil { |
| 40 | return "", ErrLossRecord |
| 41 | } |
| 42 | gate.Box, gate.RevokeExecution = legacy.Target, legacy.RevokeExecution |
| 43 | if gate.Box == "" && legacy.Kind == "move-target-lost" { |
| 44 | gate.Box = c.Op.Intent["target"] |
| 45 | } |
| 46 | if gate.Box == "" && !gate.RevokeExecution { |
| 47 | return "", ErrLossRecord |
| 48 | } |
| 49 | } |
| 50 | if gate.Box != c.Op.Intent["target"] && !gate.RevokeExecution { |
| 51 | continue |
| 52 | } |
| 53 | if id != "" { |
| 54 | return "", ErrLossRecord |
| 55 | } |
| 56 | id = next |
| 57 | } |
| 58 | return id, nil |
| 59 | } |
| 60 | |
| 61 | // OperationLossConsumed survives admission-history trimming and process restart. |
| 62 | // Only the verified admission CAS records this operation-local marker. |
| 63 | func OperationLossConsumed(c *Claim, id string) bool { |
| 64 | if c == nil || c.Holder == nil || id == "" { |
| 65 | return false |
| 66 | } |
| 67 | n, err := strconv.ParseUint(c.Op.Journal["loss_admitted."+id], 10, 64) |
| 68 | return err == nil && n > 0 && n <= c.Holder.Session |
| 69 | } |
| 70 | |