Enforce runtime report contracts and recoverable session exports
This commit is contained in:
@@ -38,6 +38,9 @@ type runner struct {
|
||||
mu sync.Mutex
|
||||
active map[int64]activeRun
|
||||
execute func(context.Context, stage.Definition, stage.Values) (stage.Result, error)
|
||||
|
||||
exportRepairMu sync.Mutex
|
||||
|
||||
// title generates the operator-facing run title from the run prompt. When nil
|
||||
// the store derives a fallback from the prompt with no model call, so tests
|
||||
// and unconfigured servers stay deterministic.
|
||||
@@ -121,6 +124,9 @@ func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) {
|
||||
}
|
||||
|
||||
func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.RunDetail, error) {
|
||||
if err := protocol.ValidateOutputFormat(request.OutputFormat); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if request.Version <= 0 || request.RequestKey == "" || len(request.RequestKey) > 128 {
|
||||
return nil, fmt.Errorf("an explicit version and request key are required")
|
||||
}
|
||||
@@ -283,7 +289,7 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
id, created, err := r.store.CreateRunTitled(r.ctx, d, request.RequestKey, workspace, request.AllowTools, request.WritePaths, request.Input, pipeline.RunOverrides{Model: request.Model, StageModels: request.StageModels, NotifyMode: request.NotifyMode}, runTitle, retryOf...)
|
||||
id, created, err := r.store.CreateRunTitled(r.ctx, d, request.RequestKey, workspace, request.AllowTools, request.WritePaths, request.Input, pipeline.RunOverrides{Model: request.Model, StageModels: request.StageModels, NotifyMode: request.NotifyMode, OutputFormat: request.OutputFormat}, runTitle, retryOf...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -707,7 +713,7 @@ func (r *runner) retry(request protocol.Message) (*protocol.RunDetail, error) {
|
||||
if notifyMode == "" {
|
||||
notifyMode, _ = r.store.RunNotifyMode(r.ctx, original.ID)
|
||||
}
|
||||
return r.start(protocol.Message{PipelineID: original.PipelineID, Version: version, RequestKey: request.RequestKey, RetryMode: mode, RetrySource: original.ID, RetryStage: request.RetryStage, RetryVisit: request.RetryVisit, RetryInput: request.RetryInput, Workspace: original.Workspace, Input: original.Input, AllowTools: original.AllowTools, WritePaths: original.WritePaths, Model: original.Model, StageModels: original.StageModels, NotifyMode: notifyMode}, original.ID)
|
||||
return r.start(protocol.Message{PipelineID: original.PipelineID, Version: version, RequestKey: request.RequestKey, RetryMode: mode, RetrySource: original.ID, RetryStage: request.RetryStage, RetryVisit: request.RetryVisit, RetryInput: request.RetryInput, Workspace: original.Workspace, Input: original.Input, AllowTools: original.AllowTools, WritePaths: original.WritePaths, Model: original.Model, StageModels: original.StageModels, NotifyMode: notifyMode, OutputFormat: original.OutputFormat}, original.ID)
|
||||
}
|
||||
|
||||
func (r *runner) cancel(id int64) error {
|
||||
@@ -1307,6 +1313,11 @@ func (r *runner) finishRunState(id int64, name, workspace, status, outcome, diag
|
||||
r.mu.Lock()
|
||||
c, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if status == "succeeded" {
|
||||
if err := r.finalizeDeliverable(c, id, sequence, input); err != nil {
|
||||
status, outcome, diagnostic = "failed", "error", "deliverable contract: "+err.Error()
|
||||
}
|
||||
}
|
||||
if err := r.store.FinishRun(c, id, status, outcome, diagnostic, input); err != nil {
|
||||
log.Printf("finish run %d: %v", id, err)
|
||||
}
|
||||
@@ -1965,7 +1976,8 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
// Any export error is recorded as a sanitized diagnostic and never returned to
|
||||
// the stage: the export is read-only, bounded and best-effort.
|
||||
func (r *runner) exportStageSession(ctx context.Context, owner pipeline.SessionOwner, artifactsDir string, runID int64, sequence int, key string, modelConfig stage.ModelConfig) *protocol.SessionExport {
|
||||
evidence := &protocol.SessionExport{SessionID: owner.ID, Backend: owner.Backend}
|
||||
evidence := &protocol.SessionExport{SessionID: owner.ID, Backend: owner.Backend, Directory: owner.Directory, RecordedAt: time.Now().UTC().Format(time.RFC3339)}
|
||||
defer classifySessionExport(evidence)
|
||||
if owner.Backend != "v1" && owner.Backend != "v2" {
|
||||
evidence.Source = "unavailable"
|
||||
evidence.Diagnostic = "session export unavailable: harness has no native export path"
|
||||
@@ -1990,17 +2002,20 @@ func (r *runner) exportStageSession(ctx context.Context, owner pipeline.SessionO
|
||||
Provider: modelConfig.Provider,
|
||||
Model: modelConfig.Model,
|
||||
})
|
||||
evidence.Attempts = result.Attempts
|
||||
if err != nil {
|
||||
evidence.Source = "native_export"
|
||||
evidence.Diagnostic = stage.RedactEvidence(err.Error())
|
||||
return evidence
|
||||
}
|
||||
evidence.Source = result.Source
|
||||
evidence.Status = result.Status
|
||||
evidence.HTML = result.HTML
|
||||
evidence.HTMLBytes = result.HTMLBytes
|
||||
evidence.Messages = result.Messages
|
||||
evidence.Truncated = result.Truncated
|
||||
evidence.Diagnostic = result.Diagnostic
|
||||
verifySessionExportFile(evidence, artifactsDir)
|
||||
return evidence
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user