~bigbes/sr-ht-spec

ref: 8edca94eedcc16b610d99487e8e427d87406b11e sr-ht-spec/core/proposal.go -rw-r--r-- 3.3 KiB
8edca94e — bigbes feat(mcpsrv): MCP read tools, with Host validation replacing the SDK guard 27 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package core

import "fmt"

// ProposalState is the lifecycle of a proposal.
//
//	open ──► merged
//	  └───► rejected
//
// Collapsed from the usual five-state machine because there is exactly one
// reviewer: with nobody else in the loop, "approve" is "merge now", and there
// is no one to request changes from — a proposal you dislike is rejected and
// the agent proposes again. Keeping `approved` and `merged` apart, or a
// `changes-requested` cycle, would be machinery serving a review conversation
// that has no second party.
type ProposalState string

const (
	StateOpen     ProposalState = "open"
	StateMerged   ProposalState = "merged"
	StateRejected ProposalState = "rejected"
)

// ProposalStates returns every state, in lifecycle order.
func ProposalStates() []ProposalState {
	return []ProposalState{StateOpen, StateMerged, StateRejected}
}

// ParseProposalState validates a state string, typically one read back from
// Postgres or an API request.
func ParseProposalState(s string) (ProposalState, error) {
	switch ProposalState(s) {
	case StateOpen, StateMerged, StateRejected:
		return ProposalState(s), nil
	}
	return "", fmt.Errorf("%w: %q is not one of open|merged|rejected", ErrInvalidState, s)
}

// Terminal reports whether the proposal has been resolved. Terminal proposals
// keep their URL — a link still resolves after merge or rejection, showing the
// outcome — but they never move again.
func (s ProposalState) Terminal() bool {
	return s == StateMerged || s == StateRejected
}

// CanTransitionTo reports whether the proposal may move from s to next,
// returning ErrInvalidTransition with both states named if it may not.
//
// Only open→merged and open→rejected are legal. Self-transitions are rejected
// too: the reconciler repairs a crashed merge by comparing the ref against the
// row and only writing when they differ, so a "merged→merged" call is a bug in
// the caller rather than an idempotent retry.
func (s ProposalState) CanTransitionTo(next ProposalState) error {
	if _, err := ParseProposalState(string(s)); err != nil {
		return err
	}
	if _, err := ParseProposalState(string(next)); err != nil {
		return err
	}
	if !ValidTransition(s, next) {
		return fmt.Errorf("%w: %s -> %s", ErrInvalidTransition, s, next)
	}
	return nil
}

// ValidTransition is the transition table itself.
func ValidTransition(from, to ProposalState) bool {
	return from == StateOpen && (to == StateMerged || to == StateRejected)
}

// Approval records how a merge was authorized, and is set on merge.
//
// Auto-merged is not human-approved, and readers must be able to tell: a bot
// asking for the approved text of a spec should be able to require human
// approval and get a different answer than for a firehose note. Collapsing the
// two would quietly launder unreviewed agent output as blessed.
type Approval string

const (
	// ApprovalHuman means the owner clicked approve.
	ApprovalHuman Approval = "human"
	// ApprovalPolicy means the path matched the space's auto_merge patterns.
	ApprovalPolicy Approval = "policy"
)

// ParseApproval validates an approval kind read back from Postgres or an API.
func ParseApproval(s string) (Approval, error) {
	switch Approval(s) {
	case ApprovalHuman, ApprovalPolicy:
		return Approval(s), nil
	}
	return "", fmt.Errorf("%w: %q is not one of human|policy", ErrInvalidApproval, s)
}