~bigbes/sr-ht-spec

ref: 964716696bd588e1fe9d67c5a8b9ff2cd6a08613 sr-ht-spec/core/proposal.go -rw-r--r-- 4.6 KiB
96471669 — bigbes feat(cmd): mount the read plane, MCP and GraphQL surfaces 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package core

import (
	"fmt"
	"strconv"
)

// ProposalPrefix is the ref namespace a proposal branch lives under, and the
// only namespace an agent credential may write. The refs rule — an agent token
// can only update refs under this prefix, and only the owner can move the
// approved branch — is the boundary that actually bounds the damage a confused
// agent can do.
//
// It lives here rather than in gitx or db because both derive branch names from
// it and a proposal whose row and whose ref disagree about its name is a break
// with no cheap way to trace it.
const ProposalPrefix = "proposals/"

// ProposalBranch is the branch name for a proposal id: "proposals/42". The id
// is the proposal row's primary key, which is what the branch, the row and the
// stable proposal URL share — so this is the one derivation, and gitx and db
// both call it rather than each spelling the concatenation out.
//
// A non-positive id is refused rather than formatted: ids come from a Postgres
// sequence and start at 1, so a zero is an unwritten row or an unset field, and
// "proposals/0" is a branch name that would go on to be created, pushed and
// looked for.
func ProposalBranch(id int64) (string, error) {
	if id <= 0 {
		return "", fmt.Errorf("%w: proposal id %d must be positive", ErrInvalidProposalID, id)
	}
	return ProposalPrefix + strconv.FormatInt(id, 10), nil
}

// 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)
}