Model stages: outcome-first output shape and one in-session outcome repair

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.
This commit is contained in:
2026-11-04 17:20:58 +00:00
parent b5927505cc
commit 88dfc051e5
13 changed files with 457 additions and 16 deletions
+58 -1
View File
@@ -64,7 +64,8 @@ func ParseOutput(markdown string, allowedOutcomes []string) (Outcome string, Bod
if value == len(lines) {
return "", "", fmt.Errorf("%w: outcome heading has no value", ErrMissingOutcome)
}
word := strings.TrimSpace(lines[value].text)
// 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)
@@ -89,6 +90,62 @@ func allowedOutcome(word string, outcomes []string) (string, bool) {
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
+32
View File
@@ -143,6 +143,38 @@ func TestParseOutputReturnsCanonicalOutcome(t *testing.T) {
}
}
func TestParseOutputToleratesTrailingPeriod(t *testing.T) {
for _, input := range []string{"# outcome\nsuccess.\n", "# outcome\n Success . \n"} {
if outcome, _, err := ParseOutput(input, []string{"success"}); err != nil || outcome != "success" {
t.Fatalf("%q: %q %v", input, outcome, err)
}
}
if _, _, err := ParseOutput("# outcome\nsuccess, mostly\n", []string{"success"}); !errors.Is(err, ErrOutcomeNotAllowed) {
t.Fatalf("prose accepted as outcome: %v", err)
}
}
func TestWithOutcome(t *testing.T) {
tests := []struct{ name, input, want string }{
{"no outcome", "# handoff\n\nx\n", "# outcome\nsuccess\n\n# handoff\n\nx\n"},
{"invalid value replaced", "# handoff\n\nx\n\n# outcome\nmaybe\n\n# log\n", "# outcome\nsuccess\n\n# handoff\n\nx\n\n\n# log\n"},
{"duplicates removed", "## outcome\nerror\n# body\n## outcome\nsuccess\n", "# outcome\nsuccess\n\n# body\n"},
{"empty heading keeps next heading", "# outcome\n\n# handoff\nx\n", "# outcome\nsuccess\n\n\n# handoff\nx\n"},
{"fenced kept", "```\n# outcome\nerror\n```\n", "# outcome\nsuccess\n\n```\n# outcome\nerror\n```\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := WithOutcome(tt.input, "success")
if got != tt.want {
t.Fatalf("WithOutcome = %q, want %q", got, tt.want)
}
if outcome, _, err := ParseOutput(got, []string{"success", "error"}); err != nil || outcome != "success" {
t.Fatalf("result does not parse: %q %v", outcome, err)
}
})
}
}
func TestParseOutputRequiresWhitespaceAfterHeadingMarker(t *testing.T) {
if _, _, err := ParseOutput("#outcome\nsuccess\n", []string{"success"}); !errors.Is(err, ErrMissingOutcome) {
t.Fatalf("error = %v, want missing outcome", err)