Add line-oriented result format for model and agent stages

Introduce a short, markdown-ish result format whose first non-empty line is
the declared outcome and whose "# output" section carries the routing data,
so stage output stops carrying multi-kilobyte prose inside a JSON envelope.

- New internal/stage/line_result.go: resultFormat() gates legacy JSON ('{'
  first) vs the new line format; decodeLineResult() parses outcome-from-first
  line, requires a JSON-object "# output" section, stores extra sections
  verbatim under normalised keys, and records a reserved result_parse status
  (parsed/partial/failed). Failures return a salvage Result plus
  ErrInvalidModelResult, and never route an undeclared outcome.
- internal/stage/model.go: write the raw response to raw-output.md (0600,
  best-effort) before parsing, and route the line format through
  decodeLineResult while leaving the JSON path (stageResultText ->
  decodeModelResult -> repairModelResult) unchanged.
- internal/stage/agent.go: agentResultText returns line-format text verbatim
  so the "# output" body is not stripped by the JSON-object extractor.
- internal/server/runner.go: merge a non-empty salvage Result into the error
  envelope under "salvage" on failure (a no-op for the legacy JSON path).
- internal/stage/line_result_test.go: raw-text fixtures covering happy path,
  missing/undeclared outcome, missing "# output", best-effort extras,
  adversarial prose, legacy JSON unchanged, >=64KB responses, and
  raw-output.md written before parsing.
This commit is contained in:
2026-10-16 21:34:33 +01:00
parent 8c1eea53a9
commit b05277a57d
5 changed files with 479 additions and 3 deletions
+10 -3
View File
@@ -255,9 +255,16 @@ func (m ModelStage) Run(ctx context.Context, def Definition, input Values) (Resu
}
replayPostAdmission := false
if generateErr == nil {
result, resultErr := decodeModelResult(stageResultText(text), def.Edges)
if resultErr != nil {
result, resultErr = repairModelResult(attemptCtx, client, spec.Config, text, def.Edges, resultErr)
var result Result
var resultErr error
if resultFormat(text) == "line" {
writeRawOutputFile(def.ArtifactsDir, text)
result, _, resultErr = decodeLineResult(text, def.Edges)
} else {
result, resultErr = decodeModelResult(stageResultText(text), def.Edges)
if resultErr != nil {
result, resultErr = repairModelResult(attemptCtx, client, spec.Config, text, def.Edges, resultErr)
}
}
if attemptCtx.Err() != nil {
resultErr = errors.Join(resultErr, attemptCtx.Err())