package judgement import ( "context" "errors" "fmt" "github.com/samcharles93/archie-core/modules/connectors" ) // Reversibility sets a ceiling from whether the action can be undone, as the // connector declares it. type Reversibility struct{} // Name is the guard's name in verdicts. func (Reversibility) Name() string { return "reversibility" } // Advisory is false: the declaration is data, not a model's opinion. func (Reversibility) Advisory() bool { return false } // Judge maps the declared reversibility to a ceiling. An action that declares // none lacks the evidence this guard needs. func (Reversibility) Judge(_ context.Context, a Action) (Verdict, error) { switch { case a.Effect.ReadOnly: return Verdict{Ceiling: ActQuietly}, nil case a.Effect.Reversibility == connectors.UndoneAutomatically: return Verdict{Ceiling: ActQuietly, Findings: []Finding{{Text: "undone automatically"}}}, nil case a.Effect.Reversibility == connectors.Recoverable || a.Target.Disposable: return Verdict{Ceiling: ActAndTell, Findings: []Finding{{Text: "recoverable, or the target is disposable"}}}, nil case a.Effect.Reversibility == connectors.Irreversible: return Verdict{Ceiling: ProposeAndWait, Findings: []Finding{{Text: "irreversible"}}}, nil } return Verdict{}, errors.New("the connector declares no reversibility for this action") } // ActionTypeLevels reports the level of an action type. type ActionTypeLevels interface { Level(ctx context.Context, c ActionType, readOnly bool) (Level, error) } // Precedent sets a ceiling from the action type's current level. An action type // starts at its default (read-only action types act quietly, every other action type // proposes and waits) and stays wherever its history has put it. type Precedent struct { ActionTypes ActionTypeLevels } // Name is the guard's name in verdicts. func (Precedent) Name() string { return "precedent" } // Advisory is false. func (Precedent) Advisory() bool { return false } // Judge returns the action type's level. func (p Precedent) Judge(ctx context.Context, a Action) (Verdict, error) { level, err := p.ActionTypes.Level(ctx, a.ActionType(), a.Effect.ReadOnly) if err != nil { return Verdict{}, fmt.Errorf("read action type level: %w", err) } return Verdict{Ceiling: level, Findings: []Finding{{Text: "this action type is at " + level.String()}}}, nil } // Freezes says whether the organisation has frozen its actions. type Freezes interface { Frozen(ctx context.Context) (bool, error) } // Freeze holds every action that changes something at propose and wait while // the organisation is frozen. It never changes an action type's level, so lifting the // freeze puts every action type back where it stood. type Freeze struct { Freezes Freezes } // Name is the guard's name in verdicts. func (Freeze) Name() string { return "freeze" } // Advisory is false. func (Freeze) Advisory() bool { return false } // Judge returns the freeze's ceiling. func (f Freeze) Judge(ctx context.Context, a Action) (Verdict, error) { frozen, err := f.Freezes.Frozen(ctx) if err != nil { return Verdict{}, fmt.Errorf("read freeze: %w", err) } v := Verdict{Ceiling: freezeCeiling(frozen, a.Effect.ReadOnly)} if v.Ceiling != ActQuietly { v.Findings = []Finding{{Text: "the organisation is frozen"}} } return v, nil }