Validate providers and retain complete run debugging context

This commit is contained in:
2026-10-09 23:21:01 +01:00
parent f3d829bea5
commit 08b8647d5b
51 changed files with 1036 additions and 159 deletions
+75 -5
View File
@@ -19,6 +19,7 @@ import (
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/runinspect"
"git.a-wan.net/alan/pipelines/internal/stage"
"git.a-wan.net/alan/pipelines/internal/statusview"
"git.a-wan.net/alan/pipelines/internal/tui"
tea "github.com/charmbracelet/bubbletea"
@@ -29,10 +30,10 @@ type App struct {
In io.Reader
}
type options struct {
url, name, prompt, template, workspace, paths, key, opencode string
json, source, noOpen, yes, allow, createOnly, force, wait bool
version, expected int
timeout time.Duration
url, name, prompt, template, workspace, paths, key, opencode, model string
json, source, noOpen, yes, allow, createOnly, force, wait bool
version, expected int
timeout time.Duration
}
func (a App) flags(command string) (*flag.FlagSet, *options) {
@@ -43,7 +44,11 @@ func (a App) flags(command string) (*flag.FlagSet, *options) {
f.BoolVar(&o.json, "json", false, "Machine-readable JSON; no interactive picker/editor")
f.DurationVar(&o.timeout, "timeout", 30*time.Second, "Timeout per server request")
switch command {
case "provider-check":
f.StringVar(&o.model, "model", envOr("PIPELINES_DEFAULT_MODEL", ""), "Provider/model to validate with one tool-free generation")
f.BoolVar(&o.yes, "yes", false, "Approve the small validation generation without prompting")
case "new", "edit":
f.BoolVar(&o.yes, "yes", false, "Approve draft creation/editor launch without prompting")
f.StringVar(&o.name, "name", "", "Pipeline name (or edit ID)")
f.StringVar(&o.prompt, "prompt", "", "Request to give OpenCode; omitted asks what to change")
f.BoolVar(&o.noOpen, "no-open", false, "Create the draft without opening OpenCode")
@@ -83,6 +88,7 @@ func (a App) flags(command string) (*flag.FlagSet, *options) {
case "cancel", "draft-delete":
f.BoolVar(&o.yes, "yes", false, "Confirm this action without an interactive prompt")
case "debug":
f.BoolVar(&o.yes, "yes", false, "Approve opening a debugging session without prompting")
f.StringVar(&o.opencode, "opencode", envOr("PIPELINES_OPENCODE", "opencode"), "Host OpenCode executable")
f.BoolVar(&o.noOpen, "no-open", false, "Print the investigation prompt without launching OpenCode")
}
@@ -202,6 +208,39 @@ func (a App) Run(ctx context.Context, args []string) error {
return fmt.Errorf("timeout must be positive and versions nonnegative")
}
pos := f.Args()
if command == "provider-check" {
if len(pos) != 0 {
return fmt.Errorf("provider-check takes no positional arguments")
}
provider, model, ok := strings.Cut(o.model, "/")
if !ok || !stage.ValidModel(provider, model) {
return fmt.Errorf("--model requires provider/model")
}
if err := a.confirmAction("Validate "+o.model+" with one small tool-free generation (provider charges may apply)?", o.yes); err != nil {
return err
}
check, cancel := context.WithTimeout(ctx, o.timeout)
defer cancel()
operations := &stage.OperationLog{}
session := ""
c := stage.OpenCodeClient{Operations: operations, OnGenerationSession: func(id, backend, reference string, active bool) error { session = id; return nil }}
text, err := c.Generate(check, stage.ModelConfig{Provider: provider, Model: model}, "Provider connection validation only. Do not use tools. Reply with exactly PIPELINES_PROVIDER_OK.")
if err == nil && strings.TrimSpace(text) != "PIPELINES_PROVIDER_OK" {
err = fmt.Errorf("model returned a response but did not complete the validation instruction")
}
result := map[string]any{"healthy": err == nil, "model": o.model, "session_id": session, "failures": operations.Failures}
if err != nil {
result["diagnostic"] = err.Error()
}
if o.json {
if outputErr := a.output(result); outputErr != nil {
return outputErr
}
} else if err == nil {
fmt.Fprintln(a.Out, "Validated", o.model, "· retained session", session)
}
return err
}
if command == "draft-delete" {
if len(pos) != 1 {
return fmt.Errorf("draft-delete requires one retained draft path")
@@ -356,6 +395,20 @@ func (a App) Run(ctx context.Context, args []string) error {
return err
}
debug := runinspect.New(response.RunDetail, executable, o.url)
definition, definitionErr := call(protocol.Message{Type: protocol.GetPipeline, PipelineID: response.RunDetail.PipelineID, Version: response.RunDetail.Version})
if definitionErr != nil {
debug.Prompt += "\nPinned pipeline definition unavailable: " + definitionErr.Error() + "\n"
} else if definition.Detail != nil {
data, _ := json.Marshal(definition.Detail)
debug.Prompt += "\nExact immutable pipeline definition and routing graph:\n" + string(data) + "\n"
}
live, evidenceErr := call(protocol.Message{Type: protocol.GetRunEvidence, RunID: id})
if evidenceErr != nil {
debug.Prompt += "\nServer-side live session evidence unavailable: " + evidenceErr.Error() + "\n"
} else {
data, _ := json.Marshal(live.Evidence)
debug.Prompt += "\nRead-only retained OpenCode evidence retrieved on the execution computer:\n" + string(data) + "\n"
}
if o.json {
return a.output(debug)
}
@@ -363,6 +416,16 @@ func (a App) Run(ctx context.Context, args []string) error {
_, err := fmt.Fprintln(a.Out, debug.Prompt)
return err
}
if err := a.confirmAction(fmt.Sprintf("Open a fresh debugging session for run #%d?", id), o.yes); err != nil {
return err
}
debug, err = debug.Prepare()
if err != nil {
return fmt.Errorf("retain debugging context: %w", err)
}
if debug.ContextFile != "" {
fmt.Fprintln(a.Out, "Retained debugging context:", debug.ContextFile)
}
cmd := debug.Command(ctx, o.opencode)
cmd.Stdin = a.In
cmd.Stdout = a.Out
@@ -668,7 +731,7 @@ func (a App) author(ctx context.Context, command string, pos []string, o *option
return fmt.Errorf("pipeline already exists; use edit or choose another name")
}
}
source = pipeline.Template([]string{"worker"}, true)
source = pipeline.WithDefaultModel(pipeline.Template([]string{"worker"}, true), r.DefaultModel)
if o.template != "" {
r, err := call(protocol.Message{Type: protocol.ListTemplates})
if err != nil {
@@ -687,6 +750,13 @@ func (a App) author(ctx context.Context, command string, pos []string, o *option
}
}
}
confirmation := "Create a retained draft?"
if !o.noOpen && !o.json {
confirmation = "Create a retained draft and open OpenCode?"
}
if err := a.confirmAction(confirmation, o.yes); err != nil {
return err
}
d, err := draft.Create(o.name, source, o.url, command == "new", base)
if err != nil {
return err