turn: keep the body written before a trailing outcome
ParseOutput returned only the text after the outcome value, so a stage that wrote its result first and "# outcome" last handed the next stage "(no output)" — the probe's Compose and Critique turns were both lost this way. The body is now the whole document with just the outcome heading and value line removed, in order, byte for byte. Tests cover a trailing and a mid-document outcome.
This commit is contained in:
@@ -47,9 +47,11 @@ Files written by a failed visit are not carried or attributed to a later turn.
|
|||||||
## Outcome-only output contract
|
## Outcome-only output contract
|
||||||
|
|
||||||
A turn's `output.md` must contain exactly one outcome section and may contain any
|
A turn's `output.md` must contain exactly one outcome section and may contain any
|
||||||
markdown before or after it. The outcome section is an ATX heading named
|
markdown before or after it. The body handed to the next turn is the document
|
||||||
`outcome` (any level) followed by the first non-blank line, which must be exactly
|
with only that heading and its value line removed, so a result written first and
|
||||||
one of the stage's allowed outcome words:
|
an outcome written last keeps the whole result. The outcome section is an ATX
|
||||||
|
heading named `outcome` (any level) followed by the first non-blank line, which
|
||||||
|
must be exactly one of the stage's allowed outcome words:
|
||||||
|
|
||||||
```md
|
```md
|
||||||
## outcome
|
## outcome
|
||||||
@@ -61,9 +63,9 @@ The rest of the output is the body, preserved verbatim.
|
|||||||
```
|
```
|
||||||
|
|
||||||
Matching trims whitespace and compares case-insensitively, returning the
|
Matching trims whitespace and compares case-insensitively, returning the
|
||||||
canonical allowed spelling. Text before the heading is ignored. Headings and
|
canonical allowed spelling. Text before the heading is never mistaken for the
|
||||||
words inside fenced code blocks (triple backticks or tildes) are ignored, so a
|
outcome value. Headings and words inside fenced code blocks (triple backticks or
|
||||||
diff or code sample cannot add a false outcome. Missing, duplicate, empty, or
|
tildes) are ignored, so a diff or code sample cannot add a false outcome. Missing, duplicate, empty, or
|
||||||
unknown outcomes are explicit errors; the parser never guesses, repairs the
|
unknown outcomes are explicit errors; the parser never guesses, repairs the
|
||||||
text, or looks for any other section.
|
text, or looks for any other section.
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func TestLayoutRenderAndParseRoundTrip(t *testing.T) {
|
|||||||
t.Fatalf("memory.md = %q, %v", got, err)
|
t.Fatalf("memory.md = %q, %v", got, err)
|
||||||
}
|
}
|
||||||
outcome, body, err := ParseOutput(output, []string{"success", "approved", "error"})
|
outcome, body, err := ParseOutput(output, []string{"success", "approved", "error"})
|
||||||
if err != nil || outcome != "approved" || body != "\n# Findings\n\nnone\n" {
|
if err != nil || outcome != "approved" || body != "# Notes\n\nChecked the diff.\n\n\n# Findings\n\nnone\n" {
|
||||||
t.Fatalf("ParseOutput() = %q, %q, %v", outcome, body, err)
|
t.Fatalf("ParseOutput() = %q, %q, %v", outcome, body, err)
|
||||||
}
|
}
|
||||||
missing, _, err := ParseOutput("# Notes\n\nno heading\n", []string{"success"})
|
missing, _, err := ParseOutput("# Notes\n\nno heading\n", []string{"success"})
|
||||||
|
|||||||
+11
-4
@@ -22,9 +22,10 @@ var (
|
|||||||
// A heading whose text is "outcome" (any ATX level) is followed by the first
|
// 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
|
// non-blank line, which must contain exactly one allowed word. Matching is
|
||||||
// trim-and-lower-case, and the canonical spelling from allowedOutcomes is
|
// trim-and-lower-case, and the canonical spelling from allowedOutcomes is
|
||||||
// returned. Everything after that value line is the body, preserved byte for
|
// returned. The body is the whole document with only that heading and its value
|
||||||
// byte. Text before the outcome heading is ignored. Headings and values inside
|
// line removed, so markdown written before the outcome section (the natural
|
||||||
// fenced code blocks are never considered.
|
// 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) {
|
func ParseOutput(markdown string, allowedOutcomes []string) (Outcome string, Body string, err error) {
|
||||||
if strings.TrimSpace(markdown) == "" {
|
if strings.TrimSpace(markdown) == "" {
|
||||||
return "", "", ErrEmptyOutput
|
return "", "", ErrEmptyOutput
|
||||||
@@ -68,7 +69,13 @@ func ParseOutput(markdown string, allowedOutcomes []string) (Outcome string, Bod
|
|||||||
if !ok {
|
if !ok {
|
||||||
return "", "", fmt.Errorf("%w: %q", ErrOutcomeNotAllowed, word)
|
return "", "", fmt.Errorf("%w: %q", ErrOutcomeNotAllowed, word)
|
||||||
}
|
}
|
||||||
return canonical, markdown[lines[value].next:], nil
|
// 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) {
|
func allowedOutcome(word string, outcomes []string) (string, bool) {
|
||||||
|
|||||||
@@ -15,10 +15,22 @@ func TestParseOutput(t *testing.T) {
|
|||||||
err error
|
err error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "leading commentary is ignored and trailing section is body",
|
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",
|
input: "Checking the work first.\n\n## outcome\nsuccess\n\n# Result\n\nThe work is complete.\n",
|
||||||
outcome: "success",
|
outcome: "success",
|
||||||
body: "\n# Result\n\nThe work is complete.\n",
|
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",
|
name: "diff embedding an outcome line cannot confuse the parser",
|
||||||
@@ -30,13 +42,13 @@ func TestParseOutput(t *testing.T) {
|
|||||||
name: "fenced headings before the real outcome are ignored",
|
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",
|
input: "```md\n# outcome\nfailed\n```\n\n~~~\n## outcome\nrejected\n~~~\n\n# outcome\nSUCCESS\n\nbody",
|
||||||
outcome: "success",
|
outcome: "success",
|
||||||
body: "\nbody",
|
body: "```md\n# outcome\nfailed\n```\n\n~~~\n## outcome\nrejected\n~~~\n\n\nbody",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "CRLF line endings",
|
name: "CRLF line endings",
|
||||||
input: "leading\r\n\r\n## outcome\r\n\r\nsuccess\r\n\r\nbody\r\n",
|
input: "leading\r\n\r\n## outcome\r\n\r\nsuccess\r\n\r\nbody\r\n",
|
||||||
outcome: "success",
|
outcome: "success",
|
||||||
body: "\r\nbody\r\n",
|
body: "leading\r\n\r\n\r\nbody\r\n",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "arbitrary markdown body is preserved exactly",
|
name: "arbitrary markdown body is preserved exactly",
|
||||||
@@ -53,6 +65,7 @@ func TestParseOutput(t *testing.T) {
|
|||||||
name: "duplicate inside a fence is ignored",
|
name: "duplicate inside a fence is ignored",
|
||||||
input: "```\n# outcome\nsuccess\n```\n\n# outcome\nblocked\n",
|
input: "```\n# outcome\nsuccess\n```\n\n# outcome\nblocked\n",
|
||||||
outcome: "blocked",
|
outcome: "blocked",
|
||||||
|
body: "```\n# outcome\nsuccess\n```\n\n",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "unknown outcome word",
|
name: "unknown outcome word",
|
||||||
|
|||||||
Reference in New Issue
Block a user