Add stage execution limits and result-first workflow views

This commit is contained in:
2026-10-10 03:03:46 +01:00
parent 7b10e3889e
commit a66d540125
35 changed files with 1392 additions and 93 deletions
+27 -5
View File
@@ -2,6 +2,7 @@ package server
import (
"context"
"errors"
"fmt"
"log"
"maps"
@@ -157,7 +158,7 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
return nil, err
}
if created {
ctx, cancel := context.WithTimeout(r.ctx, 30*time.Minute)
ctx, cancel := context.WithCancel(r.ctx)
r.active[id] = cancel
r.wg.Add(1)
go func() {
@@ -242,6 +243,7 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
input := stage.Values(request.Input)
key := d.Graph.Stages[0].Key
visits := map[string]int{}
budgets := map[string]*stage.Budget{}
finish := func(status, outcome, diagnostic string) {
c, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
@@ -305,10 +307,27 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
}
var result stage.Result
var err error
stageCtx := ctx
var modelConfig stage.ModelConfig
if stage.EffectiveType(def.Type) == stage.ModelType {
spec, specErr := (stage.ModelStage{}).Spec(def)
if specErr != nil {
finish("failed", "error", specErr.Error())
return
}
modelConfig = spec.Config
if budgets[key] == nil {
budgets[key] = stage.NewBudget(modelConfig.BudgetUSD, modelConfig.BudgetTokens)
}
stageCtx = stage.WithBudget(ctx, budgets[key])
}
if r.execute != nil {
result, err = r.execute(ctx, def, input)
timeout, _ := stage.StageTimeout(def)
executeCtx, cancel := context.WithTimeout(stageCtx, timeout)
result, err = r.execute(executeCtx, def, input)
cancel()
} else if useAgent {
result, err = (stage.ModelStage{Client: client}).Run(ctx, def, input)
result, err = (stage.ModelStage{Client: client}).Run(stageCtx, def, input)
} else if stage.EffectiveType(def.Type) == stage.ModelType {
plain := &stage.OpenCodeClient{Directory: workspace, Operations: operations}
plain.OnGenerationSession = func(session, backend, reference string, active bool) error {
@@ -316,12 +335,15 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
defer cancel()
return r.store.RunSession(c, id, sequence, session, active, backend, reference)
}
result, err = (stage.ModelStage{Client: plain}).Run(ctx, def, input)
result, err = (stage.ModelStage{Client: plain}).Run(stageCtx, def, input)
client.Transcript = plain.FormatAudit
} else {
result, err = stage.DefaultRegistry().Run(ctx, def, input)
}
step := protocol.RunStep{Sequence: sequence, Stage: key, Status: "completed", Outcome: result.Outcome, Output: result.Output, Transcript: operations.Transcript(client.Transcript)}
if budget := budgets[key]; budget != nil {
step.Transcript = stage.BudgetTranscript(step.Transcript, budget, modelConfig.Timeout, modelConfig.Retries)
}
if err != nil {
step.Status = "failed"
step.Diagnostic = err.Error()
@@ -343,7 +365,7 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
}
}
input = stage.Values(step.Output)
if useAgent {
if useAgent || errors.Is(err, stage.ErrStageCleanup) {
sessions, checkErr := r.store.ActiveAgentSessions(context.Background())
if checkErr != nil || len(sessions) > 0 {
finish("failed", "error", "agent quiescence not confirmed; inspect retained session before further work")