88dfc051e5
The prompt now says output.md must start with # outcome and the outcome word alone on the next line, then # handoff, then detail. Runs 241/243 followed the old "start with # handoff" wording and dropped the outcome. When a completed turn lacks a valid outcome, the OpenCode/agent and Pi clients send one short follow-up in the same session asking only for the outcome line. An outcome-only reply goes at the top of the original report; a still-invalid reply fails with the same validation error as before. The follow-up's tokens accrue to the session usage. No outcome is inferred. Missing tools/evidence now name blocked when declared, else error. The parser tolerates a trailing period on the outcome word.
235 lines
6.5 KiB
Go
235 lines
6.5 KiB
Go
package turn
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
// ErrEmptyOutput means the turn produced no markdown at all.
|
|
ErrEmptyOutput = errors.New("turn output: empty")
|
|
// ErrMissingOutcome means no outcome heading and value were found.
|
|
ErrMissingOutcome = errors.New("turn output: missing outcome")
|
|
// ErrDuplicateOutcome means more than one outcome heading was found.
|
|
ErrDuplicateOutcome = errors.New("turn output: duplicate outcome")
|
|
// ErrOutcomeNotAllowed means the value was not one of the declared words.
|
|
ErrOutcomeNotAllowed = errors.New("turn output: outcome not allowed")
|
|
)
|
|
|
|
// ParseOutput extracts the single outcome section from a turn's markdown.
|
|
//
|
|
// A heading whose text is "outcome" (any ATX level) is followed by the first
|
|
// non-blank line, which must contain exactly one allowed word. Matching is
|
|
// trim-and-lower-case, and the canonical spelling from allowedOutcomes is
|
|
// returned. The body is the whole document with only that heading and its value
|
|
// line removed, so markdown written before the outcome section (the natural
|
|
// place for a trailing "# outcome") is kept too, in its original order and
|
|
// bytes. Headings and values inside fenced code blocks are never considered.
|
|
func ParseOutput(markdown string, allowedOutcomes []string) (Outcome string, Body string, err error) {
|
|
if strings.TrimSpace(markdown) == "" {
|
|
return "", "", ErrEmptyOutput
|
|
}
|
|
lines := markdownLines(markdown)
|
|
var headings []int
|
|
var fence fenceState
|
|
for i, line := range lines {
|
|
if fence.open() {
|
|
if fence.closes(line.text) {
|
|
fence = fenceState{}
|
|
}
|
|
continue
|
|
}
|
|
if marker, ok := openingFence(line.text); ok {
|
|
fence = marker
|
|
continue
|
|
}
|
|
if isOutcomeHeading(line.text) {
|
|
headings = append(headings, i)
|
|
}
|
|
}
|
|
switch len(headings) {
|
|
case 0:
|
|
return "", "", ErrMissingOutcome
|
|
case 1:
|
|
default:
|
|
return "", "", fmt.Errorf("%w: found %d outcome headings", ErrDuplicateOutcome, len(headings))
|
|
}
|
|
|
|
heading := headings[0]
|
|
value := heading + 1
|
|
for value < len(lines) && strings.TrimSpace(lines[value].text) == "" {
|
|
value++
|
|
}
|
|
if value == len(lines) {
|
|
return "", "", fmt.Errorf("%w: outcome heading has no value", ErrMissingOutcome)
|
|
}
|
|
// The value line is the word alone; a trailing period is tolerated.
|
|
word := strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(lines[value].text), "."))
|
|
canonical, ok := allowedOutcome(word, allowedOutcomes)
|
|
if !ok {
|
|
return "", "", fmt.Errorf("%w: %q", ErrOutcomeNotAllowed, word)
|
|
}
|
|
// Remove the outcome section and nothing else: a document that states its
|
|
// result first and its outcome last must still hand its whole body forward.
|
|
sectionStart := 0
|
|
if heading > 0 {
|
|
sectionStart = lines[heading-1].next
|
|
}
|
|
return canonical, markdown[:sectionStart] + markdown[lines[value].next:], nil
|
|
}
|
|
|
|
func allowedOutcome(word string, outcomes []string) (string, bool) {
|
|
key := strings.ToLower(word)
|
|
for _, outcome := range outcomes {
|
|
outcome = strings.TrimSpace(outcome)
|
|
if outcome != "" && strings.ToLower(outcome) == key {
|
|
return outcome, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// WithOutcome returns markdown with a leading "# outcome" section holding the
|
|
// given word, replacing any outcome headings (and their value lines) already in
|
|
// it. The word is never derived from the text: callers pass an outcome the
|
|
// model stated explicitly, such as its reply to the outcome repair follow-up.
|
|
// Every other byte of the original document is kept in order.
|
|
func WithOutcome(markdown, outcome string) string {
|
|
lines := markdownLines(markdown)
|
|
drop := make(map[int]bool)
|
|
var fence fenceState
|
|
for i := 0; i < len(lines); i++ {
|
|
text := lines[i].text
|
|
if fence.open() {
|
|
if fence.closes(text) {
|
|
fence = fenceState{}
|
|
}
|
|
continue
|
|
}
|
|
if marker, ok := openingFence(text); ok {
|
|
fence = marker
|
|
continue
|
|
}
|
|
if !isOutcomeHeading(text) {
|
|
continue
|
|
}
|
|
drop[i] = true
|
|
value := i + 1
|
|
for value < len(lines) && strings.TrimSpace(lines[value].text) == "" {
|
|
value++
|
|
}
|
|
if value == len(lines) {
|
|
continue
|
|
}
|
|
_, _, heading := atxHeading(lines[value].text)
|
|
_, fenced := openingFence(lines[value].text)
|
|
if !heading && !fenced {
|
|
drop[value] = true
|
|
i = value
|
|
}
|
|
}
|
|
var b strings.Builder
|
|
b.WriteString("# outcome\n")
|
|
b.WriteString(outcome)
|
|
b.WriteString("\n\n")
|
|
for i, line := range lines {
|
|
if drop[i] {
|
|
continue
|
|
}
|
|
start := 0
|
|
if i > 0 {
|
|
start = lines[i-1].next
|
|
}
|
|
b.WriteString(markdown[start:line.next])
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
type markdownLine struct {
|
|
text string
|
|
next int
|
|
}
|
|
|
|
// markdownLines records each line's content without its line ending and the
|
|
// byte position immediately after that line ending. Body slices therefore keep
|
|
// CRLF and every other byte exactly as supplied.
|
|
func markdownLines(markdown string) []markdownLine {
|
|
lines := make([]markdownLine, 0, strings.Count(markdown, "\n")+1)
|
|
for start := 0; start < len(markdown); {
|
|
next := len(markdown)
|
|
text := markdown[start:]
|
|
if relative := strings.IndexByte(markdown[start:], '\n'); relative >= 0 {
|
|
next = start + relative + 1
|
|
text = markdown[start : start+relative]
|
|
}
|
|
lines = append(lines, markdownLine{
|
|
text: strings.TrimSuffix(text, "\r"),
|
|
next: next,
|
|
})
|
|
if next == len(markdown) {
|
|
break
|
|
}
|
|
start = next
|
|
}
|
|
return lines
|
|
}
|
|
|
|
func isOutcomeHeading(text string) bool {
|
|
text = strings.TrimSpace(text)
|
|
hashes := 0
|
|
for hashes < len(text) && text[hashes] == '#' {
|
|
hashes++
|
|
}
|
|
if hashes == 0 || hashes == len(text) {
|
|
return false
|
|
}
|
|
// Markdown ATX headings require whitespace between the marker and text.
|
|
if text[hashes] != ' ' && text[hashes] != '\t' {
|
|
return false
|
|
}
|
|
return strings.EqualFold(strings.TrimSpace(text[hashes:]), "outcome")
|
|
}
|
|
|
|
type fenceState struct {
|
|
char byte
|
|
length int
|
|
}
|
|
|
|
func (f fenceState) open() bool {
|
|
return f.length > 0
|
|
}
|
|
|
|
func (f fenceState) closes(text string) bool {
|
|
char, length, ok := fenceMarker(text)
|
|
if !ok || char != f.char || length < f.length {
|
|
return false
|
|
}
|
|
trimmed := strings.TrimLeft(text, " \t")
|
|
return strings.TrimSpace(trimmed[length:]) == ""
|
|
}
|
|
|
|
func openingFence(text string) (fenceState, bool) {
|
|
char, length, ok := fenceMarker(text)
|
|
if !ok {
|
|
return fenceState{}, false
|
|
}
|
|
return fenceState{char: char, length: length}, true
|
|
}
|
|
|
|
func fenceMarker(text string) (byte, int, bool) {
|
|
trimmed := strings.TrimLeft(text, " \t")
|
|
if len(trimmed) < 3 || (trimmed[0] != '`' && trimmed[0] != '~') {
|
|
return 0, 0, false
|
|
}
|
|
char := trimmed[0]
|
|
length := 0
|
|
for length < len(trimmed) && trimmed[length] == char {
|
|
length++
|
|
}
|
|
if length < 3 {
|
|
return 0, 0, false
|
|
}
|
|
return char, length, true
|
|
}
|