stage turns: replace JSON results with file-based markdown turns

Each model visit now gets <run>/stages/<seq>-<stage>/ with input.md,
memory.md, output.md, private/ and a shared run-level public/. The model
reads the two input files, follows the stage's instructions verbatim and
returns complete markdown; only the # outcome section is parsed.

- add internal/turn layout, render and output parsing
- write input.md/memory.md before a visit and output.md from the final
  message; inventory all turn files as stage artifacts
- accumulate completed turns and system-written public file additions in
  memory, including across retries and resume
- remove the JSON envelope, strict field validation, line format and the
  repair ladder for model stages
- republish pipeline authoring prompts and docs in the new style

Checks: go build ./..., go vet ./..., go test -count=1 ./..., gofmt -l .
This commit is contained in:
2026-10-19 22:27:44 +01:00
parent b2fd614ba4
commit e37b864c40
66 changed files with 2199 additions and 3028 deletions
+38 -30
View File
@@ -155,8 +155,8 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s
}
}
}
prompt += "\nRun directory: " + c.Directory + ". Edit scope: " + editPaths + ". There is no implicit language or extension restriction; explicit stage permissions and run scopes are authoritative. Select verification commands from the actual repository and permitted commands. Older generic language-specific boilerplate is not a runtime permission policy. Do not invent a missing file-list or manual-approval requirement. Do not infer that later stages lack editing because this stage is read-only. Respect this stage's role and available tools. Work in the run directory; do not modify unrelated locations or Pipelines history/configuration. Do not delegate. Commit/push only when the task explicitly requests it. Final response must be only the stage result JSON."
prompt += artifactsPrompt(c.ArtifactsDir)
prompt += "\nRun directory: " + c.Directory + ". Edit scope: " + editPaths + ". There is no implicit language or extension restriction; explicit stage permissions and run scopes are authoritative. Select verification commands from the actual repository and permitted commands. Older generic language-specific boilerplate is not a runtime permission policy. Do not invent a missing file-list or manual-approval requirement. Do not infer that later stages lack editing because this stage is read-only. Respect this stage's role and available tools. Work in the run directory; do not modify unrelated locations or Pipelines history/configuration. Do not delegate. Commit/push only when the task explicitly requests it. Return the complete turn output markdown as your final message."
prompt += artifactsPrompt(c.ArtifactsDir, c.ArtifactAccess)
if c.Policy.Shell != nil {
prompt += "\nPermitted shell commands: " + strings.Join(c.Policy.Shell, "; ")
}
@@ -191,14 +191,35 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s
return text, err
}
// artifactsPrompt is the general, technology-neutral boilerplate that tells an
// agent stage where to save the files it produces. It is empty when the runtime
// did not provide a directory, so existing prompts are unchanged without one.
func artifactsPrompt(directory string) string {
if directory == "" {
// artifactsPrompt tells an agent stage where it may write the files it
// produces. The turn directory itself belongs to the server: input.md,
// memory.md and output.md are written by the runner and output.md comes from
// the model's final message. The only writable artifact scopes are this
// visit's private/ directory and the run's shared public/ directory, matching
// the exact paths permissionRules grants. It is empty when the runtime provided
// neither a directory nor a writable scope, so existing prompts are unchanged.
func artifactsPrompt(directory string, access ArtifactAccess) string {
writable := access.Write != "" || access.WriteShared != ""
if directory == "" && !writable {
return ""
}
return "\nArtifacts directory: " + directory + ". Save any artifacts you produce - images, screenshots, reports, logs - into that directory; they are attached to this stage."
var b strings.Builder
b.WriteString("\nArtifacts:")
if directory != "" {
fmt.Fprintf(&b, " this visit's turn directory is %s.", directory)
}
if access.Write != "" {
fmt.Fprintf(&b, " Save turn-local artifacts under %s; they stay with this visit and are not handed to later stages.", access.Write)
}
if access.WriteShared != "" {
fmt.Fprintf(&b, " Save artifacts for later stages under %s; files there are shared by the run.", access.WriteShared)
}
if writable {
b.WriteString(" Do not write input.md, memory.md or output.md yourself; the server owns those and writes output.md from your final message.")
} else {
b.WriteString(" The server records the turn files as artifacts.")
}
return b.String()
}
// settleV2Usage issues one bounded final session-aggregate read and records the
@@ -267,11 +288,10 @@ func completedAgentResult(data []byte) (string, error) {
text := ""
var messages struct {
Data []struct {
Finish string `json:"finish"`
Completed bool `json:"completed"`
Error json.RawMessage `json:"error"`
Structured json.RawMessage `json:"structured"`
Content []struct {
Finish string `json:"finish"`
Completed bool `json:"completed"`
Error json.RawMessage `json:"error"`
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
@@ -287,31 +307,19 @@ func completedAgentResult(data []byte) (string, error) {
if len(m.Error) > 0 && string(m.Error) != "null" {
return "", fmt.Errorf("%w: %w", ErrHarnessTransport, opencode.ExecutionError(m.Error))
}
if m.Finish == "stop" || (m.Finish == "tool-calls" && m.Completed && len(m.Structured) > 0 && string(m.Structured) != "null") {
if len(m.Structured) > 0 && string(m.Structured) != "null" {
return string(m.Structured), nil
}
// Model turns have exactly one result dialect: assistant text. A
// structured payload would be a retired JSON result and is ignored rather
// than written to output.md and rejected later by ParseOutput.
if m.Finish == "stop" {
for _, part := range m.Content {
if part.Type == "text" {
text += part.Text
}
}
if text != "" {
return agentResultText(text), nil
return text, nil
}
}
}
return "", fmt.Errorf("%w: final assistant readback had no completed result", ErrHarnessTransport)
}
// Agents may emit a short explanation before their final result. Preserve the
// full response in the audit, but pass only a complete final JSON object to the
// same strict stage validator. Never infer outcomes from prose or tool output.
// The line-oriented format is returned verbatim so its '# output' body is not
// stripped by the JSON-object extractor.
func agentResultText(text string) string {
if resultFormat(text) == "line" {
return text
}
return stageResultText(text)
}