Files
alan 88dfc051e5 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.
2026-11-04 17:20:58 +00:00

183 lines
5.8 KiB
Go

package turn
import (
"errors"
"testing"
)
func TestParseOutput(t *testing.T) {
allowed := []string{"success", "blocked", "rejected"}
tests := []struct {
name string
input string
outcome string
body string
err error
}{
{
name: "text before and after the outcome section is the body",
input: "Checking the work first.\n\n## outcome\nsuccess\n\n# Result\n\nThe work is complete.\n",
outcome: "success",
body: "Checking the work first.\n\n\n# Result\n\nThe work is complete.\n",
},
{
name: "a trailing outcome keeps the whole body written before it",
input: "# Composition\n\nAll stages listed.\n\n# outcome\nsuccess\n",
outcome: "success",
body: "# Composition\n\nAll stages listed.\n\n",
},
{
name: "an outcome in the middle keeps both sides in order",
input: "Before.\n\n# outcome\nblocked\n\nAfter.\n",
outcome: "blocked",
body: "Before.\n\n\nAfter.\n",
},
{
name: "diff embedding an outcome line cannot confuse the parser",
input: "# outcome\nblocked\n\n```diff\n+# outcome success\n+unrelated change\n```\n",
outcome: "blocked",
body: "\n```diff\n+# outcome success\n+unrelated change\n```\n",
},
{
name: "fenced headings before the real outcome are ignored",
input: "```md\n# outcome\nfailed\n```\n\n~~~\n## outcome\nrejected\n~~~\n\n# outcome\nSUCCESS\n\nbody",
outcome: "success",
body: "```md\n# outcome\nfailed\n```\n\n~~~\n## outcome\nrejected\n~~~\n\n\nbody",
},
{
name: "CRLF line endings",
input: "leading\r\n\r\n## outcome\r\n\r\nsuccess\r\n\r\nbody\r\n",
outcome: "success",
body: "leading\r\n\r\n\r\nbody\r\n",
},
{
name: "arbitrary markdown body is preserved exactly",
input: "# outcome\nsuccess\n\n## Heading\n\n- item\n\n```go\nfmt.Println(\"hi\")\n```\n\n# outcome is discussed here\n",
outcome: "success",
body: "\n## Heading\n\n- item\n\n```go\nfmt.Println(\"hi\")\n```\n\n# outcome is discussed here\n",
},
{
name: "duplicate outcome",
input: "# outcome\nsuccess\n\n# outcome\nblocked\n",
err: ErrDuplicateOutcome,
},
{
name: "duplicate inside a fence is ignored",
input: "```\n# outcome\nsuccess\n```\n\n# outcome\nblocked\n",
outcome: "blocked",
body: "```\n# outcome\nsuccess\n```\n\n",
},
{
name: "unknown outcome word",
input: "# outcome\nmaybe\n",
err: ErrOutcomeNotAllowed,
},
{
name: "extra text on the outcome line",
input: "# outcome\nsuccess please\n",
err: ErrOutcomeNotAllowed,
},
{
name: "value inside a fence is not an outcome value",
input: "# outcome\n```\nsuccess\n```\n",
err: ErrOutcomeNotAllowed,
},
{
name: "empty output",
input: "",
err: ErrEmptyOutput,
},
{
name: "whitespace-only output",
input: " \r\n\t\n",
err: ErrEmptyOutput,
},
{
name: "missing outcome heading",
input: "# Result\nsuccess\n",
err: ErrMissingOutcome,
},
{
name: "outcome heading without a value",
input: "# outcome\n\n",
err: ErrMissingOutcome,
},
{
name: "fenced outcome is not a real outcome",
input: "```\n# outcome\nsuccess\n```\n",
err: ErrMissingOutcome,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
outcome, body, err := ParseOutput(test.input, allowed)
if test.err != nil {
if !errors.Is(err, test.err) {
t.Fatalf("error = %v, want %v", err, test.err)
}
if outcome != "" || body != "" {
t.Fatalf("error result = (%q, %q), want empty", outcome, body)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if outcome != test.outcome || body != test.body {
t.Fatalf("ParseOutput() = (%q, %q), want (%q, %q)", outcome, body, test.outcome, test.body)
}
})
}
}
func TestParseOutputReturnsCanonicalOutcome(t *testing.T) {
outcome, body, err := ParseOutput("#### Outcome\n\n Success \n\nrest", []string{" success ", "blocked"})
if err != nil {
t.Fatalf("ParseOutput() error: %v", err)
}
if outcome != "success" {
t.Fatalf("outcome = %q, want canonical %q", outcome, "success")
}
if body != "\nrest" {
t.Fatalf("body = %q, want %q", body, "\nrest")
}
}
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)
}
}