-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontmatter.go
More file actions
233 lines (213 loc) · 8.15 KB
/
Copy pathfrontmatter.go
File metadata and controls
233 lines (213 loc) · 8.15 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package devflow
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/tinywasm/markdown"
)
// PlanMeta holds the parsed frontmatter of a docs/PLAN.md file.
type PlanMeta struct {
Message string // required: commit message used when closing the loop
Tag string // optional: explicit version tag (e.g. "v0.1.0")
Executor string // optional: agent that implements (default "jules")
Reviewer string // optional: agent that reviews (default "none")
Corrector string // optional: agent that applies review feedback (default: executor)
ReviewGuide string // optional: path to extra review criteria
Status string // optional: dispatch -> running -> reviewing -> review
Session string // optional: executor session id
ReviewSession string // optional: reviewer session id
Round int // optional: executor<->reviewer round count (capped, default 3)
PR string // optional: URL of the PR opened by the executor
}
const (
FrontmatterKeyPlan = "PLAN" // required: commit message used when closing the loop
FrontmatterKeyTag = "TAG" // optional: explicit version tag
FrontmatterKeyExecutor = "EXECUTOR" // optional: executor agent
FrontmatterKeyReviewer = "REVIEWER" // optional: reviewer agent
FrontmatterKeyCorrector = "CORRECTOR" // optional: corrector agent
FrontmatterKeyReviewGuide = "REVIEW_GUIDE" // optional: path to review guidelines
FrontmatterKeyStatus = "STATUS" // optional: orchestrator status
FrontmatterKeySession = "SESSION" // optional: executor session ID
FrontmatterKeyReviewSession = "REVIEW_SESSION" // optional: reviewer session ID
FrontmatterKeyRound = "ROUND" // optional: round count
FrontmatterKeyPR = "PR" // optional: pull request URL
)
// frontmatterHelp is appended to every frontmatter error so the fix is obvious from the
// terminal alone — nobody should need to open the docs to unblock a dispatch.
const frontmatterHelp = `
docs/PLAN.md must OPEN with a frontmatter block — the very first line is '---':
---
PLAN: "feat: what this plan implements"
TAG: v0.2.0
---
# Plan — ...
PLAN REQUIRED. The commit message used when the loop is closed.
TAG optional. Explicit version (e.g. v0.2.0); omitted = auto-bump.`
var (
ErrFrontmatterMissing = errors.New("plan frontmatter: file must start with a '---' line" + frontmatterHelp)
ErrFrontmatterUnclosed = errors.New("plan frontmatter: opening '---' has no matching closing '---'" + frontmatterHelp)
ErrFrontmatterNoPlan = errors.New("plan frontmatter: missing required 'PLAN:' field (the old 'message:' key was renamed — rename it in your plan)" + frontmatterHelp)
)
// wrapStructuralErr translates markdown's structural frontmatter errors into
// devflow's own (which carry frontmatterHelp); anything else passes through.
func wrapStructuralErr(err error) error {
switch {
case errors.Is(err, markdown.ErrFrontmatterMissing):
return ErrFrontmatterMissing
case errors.Is(err, markdown.ErrFrontmatterUnclosed):
return ErrFrontmatterUnclosed
default:
return err
}
}
// metaFromMap maps generic frontmatter key/values to PlanMeta, enforcing the
// devflow-specific rule that 'PLAN' is required.
func metaFromMap(kv map[string]string) (PlanMeta, error) {
message := kv[FrontmatterKeyPlan]
if message == "" {
return PlanMeta{}, ErrFrontmatterNoPlan
}
round, _ := strconv.Atoi(kv[FrontmatterKeyRound])
return PlanMeta{
Message: message,
Tag: kv[FrontmatterKeyTag],
Executor: kv[FrontmatterKeyExecutor],
Reviewer: kv[FrontmatterKeyReviewer],
Corrector: kv[FrontmatterKeyCorrector],
ReviewGuide: kv[FrontmatterKeyReviewGuide],
Status: kv[FrontmatterKeyStatus],
Session: kv[FrontmatterKeySession],
ReviewSession: kv[FrontmatterKeyReviewSession],
Round: round,
PR: kv[FrontmatterKeyPR],
}, nil
}
// ParseFrontmatter parses the leading frontmatter block of content and maps it
// to PlanMeta, requiring 'PLAN'. Structural parsing is delegated to
// tinywasm/markdown; devflow only owns the "which keys are required" rule.
func ParseFrontmatter(content string) (PlanMeta, error) {
kv, err := markdown.ParseFrontmatter(content)
if err != nil {
return PlanMeta{}, wrapStructuralErr(err)
}
return metaFromMap(kv)
}
// ReadPlanMeta reads and validates the frontmatter of a plan file at path.
func ReadPlanMeta(path string) (PlanMeta, error) {
kv, err := markdown.New(".", "", nil).InputPath(path, os.ReadFile).Frontmatter()
if err != nil {
return PlanMeta{}, wrapStructuralErr(err)
}
return metaFromMap(kv)
}
var ErrNoCloseLoopMessage = errors.New("no close-loop commit message: pass one on the CLI or add 'PLAN:' to the plan frontmatter")
// ResolvePublishMessage picks the effective close-loop commit message and tag:
// an explicit CLI value wins; otherwise the plan frontmatter is used.
func ResolvePublishMessage(cliMessage, cliTag string, meta PlanMeta) (message, tag string, err error) {
message = cliMessage
if message == "" {
message = meta.Message
}
if message == "" {
return "", "", ErrNoCloseLoopMessage
}
tag = cliTag
if tag == "" {
tag = meta.Tag
}
return message, tag, nil
}
// SplitMarkdown splits a markdown content into frontmatter keys and body.
func SplitMarkdown(content string) (map[string]string, string, error) {
normalized := strings.ReplaceAll(content, "\r\n", "\n")
lines := strings.Split(normalized, "\n")
if len(lines) == 0 || lines[0] != "---" {
return nil, normalized, ErrFrontmatterMissing
}
closingIdx := -1
for i := 1; i < len(lines); i++ {
if lines[i] == "---" {
closingIdx = i
break
}
}
if closingIdx == -1 {
return nil, normalized, ErrFrontmatterUnclosed
}
kv := make(map[string]string)
for i := 1; i < closingIdx; i++ {
line := strings.TrimSpace(lines[i])
if line == "" || strings.HasPrefix(line, "#") {
continue
}
parts := strings.SplitN(line, ":", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
val := strings.TrimSpace(parts[1])
if len(val) >= 2 && ((val[0] == '"' && val[len(val)-1] == '"') || (val[0] == '\'' && val[len(val)-1] == '\'')) {
val = val[1 : len(val)-1]
}
kv[key] = val
}
}
body := strings.Join(lines[closingIdx+1:], "\n")
return kv, body, nil
}
// SerializeFrontmatter serializes PlanMeta back to a YAML-like frontmatter string block.
func SerializeFrontmatter(meta PlanMeta) string {
var sb strings.Builder
sb.WriteString("---\n")
if meta.Message != "" {
sb.WriteString(fmt.Sprintf("%s: %q\n", FrontmatterKeyPlan, meta.Message))
}
if meta.Tag != "" {
sb.WriteString(fmt.Sprintf("%s: %s\n", FrontmatterKeyTag, meta.Tag))
}
if meta.Executor != "" {
sb.WriteString(fmt.Sprintf("%s: %s\n", FrontmatterKeyExecutor, meta.Executor))
}
if meta.Reviewer != "" {
sb.WriteString(fmt.Sprintf("%s: %s\n", FrontmatterKeyReviewer, meta.Reviewer))
}
if meta.Corrector != "" {
sb.WriteString(fmt.Sprintf("%s: %s\n", FrontmatterKeyCorrector, meta.Corrector))
}
if meta.ReviewGuide != "" {
sb.WriteString(fmt.Sprintf("%s: %s\n", FrontmatterKeyReviewGuide, meta.ReviewGuide))
}
if meta.Status != "" {
sb.WriteString(fmt.Sprintf("%s: %s\n", FrontmatterKeyStatus, meta.Status))
}
if meta.Session != "" {
sb.WriteString(fmt.Sprintf("%s: %s\n", FrontmatterKeySession, meta.Session))
}
if meta.ReviewSession != "" {
sb.WriteString(fmt.Sprintf("%s: %s\n", FrontmatterKeyReviewSession, meta.ReviewSession))
}
if meta.Round > 0 {
sb.WriteString(fmt.Sprintf("%s: %d\n", FrontmatterKeyRound, meta.Round))
}
if meta.PR != "" {
sb.WriteString(fmt.Sprintf("%s: %s\n", FrontmatterKeyPR, meta.PR))
}
sb.WriteString("---\n")
return sb.String()
}
// WritePlanMeta updates or creates a PLAN.md file at path with the given meta, preserving the markdown body.
func WritePlanMeta(path string, meta PlanMeta) error {
var body string
content, err := os.ReadFile(path)
if err == nil {
_, b, err := SplitMarkdown(string(content))
if err == nil {
body = b
} else {
body = string(content)
}
}
fm := SerializeFrontmatter(meta)
newContent := fm + body
return os.WriteFile(path, []byte(newContent), 0644)
}