Files
pipelines/internal/stage/model.go
T
alan e37b864c40 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 .
2026-10-19 22:27:44 +01:00

132 lines
3.8 KiB
Go

package stage
import (
"context"
"fmt"
"strconv"
"strings"
"time"
)
const ModelType = "model"
const ModelID = "deepseek-v4.1-flash"
// ModelConfig and ModelSpec group the settings/contracts that belong specifically
// to an LLM stage. Other stage types need not have a prompt, provider, or model.
type ModelConfig struct {
Harness string
Provider string
Model string
Retries int
Timeout time.Duration
BudgetUSD float64
BudgetTokens int64
Budget *Budget
}
type ModelSpec struct {
Prompt string
Config ModelConfig
}
// ModelGenerator isolates the provider integration from stage instructions/results.
type ModelGenerator interface {
Generate(context.Context, ModelConfig, string) (string, error)
}
type ModelStage struct {
Client ModelGenerator // nil uses the locally authenticated OpenCode service
}
var _ Stage = ModelStage{}
func (m ModelStage) Validate(def Definition) error {
_, err := m.Spec(def)
return err
}
func (ModelStage) Spec(def Definition) (ModelSpec, error) {
if EffectiveType(def.Type) != ModelType {
return ModelSpec{}, fmt.Errorf("expected model stage, got %q", def.Type)
}
for _, block := range []string{"prompt", "configs"} {
if strings.TrimSpace(def.Blocks[block]) == "" {
return ModelSpec{}, fmt.Errorf("required block %s is missing or empty", block)
}
}
for block := range def.Blocks {
switch block {
case "output", "prompt", "edges", "configs", "tools", "permissions", "checks", "verification":
default:
return ModelSpec{}, fmt.Errorf("unexpected block %q for model stage", block)
}
}
if _, _, err := ToolNames(def); err != nil {
return ModelSpec{}, err
}
if _, err := StagePermissions(def); err != nil {
return ModelSpec{}, err
}
if _, err := VerificationTargetFor(def); err != nil {
return ModelSpec{}, err
}
for _, key := range []string{"provider", "model"} {
if def.Configs[key] == "" {
return ModelSpec{}, fmt.Errorf("missing config %s", key)
}
}
for key := range def.Configs {
switch key {
case "provider", "model", "retries", "timeout", "budget_usd", "budget_tokens", "harness", "delivery":
default:
return ModelSpec{}, fmt.Errorf("unknown config %q", key)
}
}
if !ValidModel(def.Configs["provider"], def.Configs["model"]) {
return ModelSpec{}, fmt.Errorf("invalid provider/model identifier; use the exact IDs validated during setup")
}
backend := def.Configs["harness"]
if backend == "" {
backend = "opencode"
}
if backend != "opencode" && backend != "pi" {
return ModelSpec{}, fmt.Errorf("harness must be opencode or pi")
}
if delivery := def.Configs["delivery"]; delivery != "" && delivery != "true" {
return ModelSpec{}, fmt.Errorf("delivery must be true when set")
}
if def.Configs["delivery"] == "true" && backend != "opencode" {
return ModelSpec{}, fmt.Errorf("delivery is supported only by the opencode harness")
}
if _, present := def.Blocks["checks"]; present && def.Configs["delivery"] != "true" {
return ModelSpec{}, fmt.Errorf("checks block is supported only by delivery stages")
}
if backend == "pi" {
tools, _, _ := ToolNames(def)
if _, err := piTools(tools); err != nil {
return ModelSpec{}, err
}
}
retries := DefaultRetries
var err error
if value := def.Configs["retries"]; value != "" {
retries, err = strconv.Atoi(value)
}
if err != nil || retries < 0 || retries > 100 {
return ModelSpec{}, fmt.Errorf("retries must be an integer from 0 to 100")
}
timeout, err := StageTimeout(def)
if err != nil {
return ModelSpec{}, err
}
usd, tokens, err := parseBudget(def)
if err != nil {
return ModelSpec{}, err
}
return ModelSpec{
Prompt: def.Blocks["prompt"],
Config: ModelConfig{Harness: backend, Provider: def.Configs["provider"], Model: def.Configs["model"], Retries: retries, Timeout: timeout, BudgetUSD: usd, BudgetTokens: tokens},
}, nil
}