package app import ( "errors" "fmt" "sort" "gitea.maximumdirect.net/eric/narratio/internal/manifest" ) // RunStageResult describes this invocation's work, independently of prior state. type RunStageResult struct { Name string `json:"name"` Attempted bool `json:"attempted"` Outcome string `json:"outcome"` Reason string `json:"reason,omitempty"` } // RunArtifact identifies durable output evidence without exposing manifest internals. type RunArtifact struct { Stage string `json:"stage"` Source string `json:"source,omitempty"` Kind string `json:"kind"` Path string `json:"path"` ProducerRunID string `json:"producer_run_id,omitempty"` Evidence string `json:"evidence"` } func finalizeInvocation(runtime *invocationRuntime, operationErr error) (*RunSummary, error) { evidence := runtime.manifest resultErr := operationErr if operationErr != nil { resultErr = persistSessionTerminalFailure(runtime.ctx, runtime.manifestStore, runtime.manifestPath, runtime.manifest, operationErr, runtime.dependencies.now()) // Read durable authority after a failure: checkpoint saves can succeed // even when terminal persistence fails. var err error evidence, err = runtime.manifestStore.Load(runtime.ctx, runtime.manifestPath) if err != nil { resultErr = errors.Join(resultErr, fmt.Errorf("read durable run evidence: %w", err)) evidence = nil } } return &RunSummary{ SessionID: runtime.identity.SessionID, RunID: runtime.identity.RunID, ManifestPath: runtime.manifestPath, StageNames: runtime.stageNames, Executed: runtime.executed, Skipped: runtime.skipped, StageResults: runtime.stageResults, AvailableArtifacts: durableRunArtifacts(evidence), Complete: resultErr == nil, }, resultErr } func durableRunArtifacts(m *manifest.Manifest) []RunArtifact { out := []RunArtifact{} if m == nil { return out } names := make([]string, 0, len(m.Stages)) for name := range m.Stages { names = append(names, name) } sort.Strings(names) for _, name := range names { record := m.Stages[name] if record == nil { continue } seen := map[string]bool{} add := func(value manifest.ArtifactRecord, evidence string) { key := value.SourceID + "\x00" + value.LocalPath if seen[key] { return } seen[key] = true out = append(out, RunArtifact{Stage: name, Source: value.SourceID, Kind: value.Kind, Path: value.LocalPath, ProducerRunID: value.ProducerRunID, Evidence: evidence}) } if record.Status == manifest.StatusSucceeded { for _, value := range record.Outputs { add(value, "stage_result") } } if record.HasVersionedTranscribeState() { keys := make([]string, 0, len(record.TranscribeSpeakers)) for key := range record.TranscribeSpeakers { keys = append(keys, key) } sort.Strings(keys) for _, key := range keys { add(record.TranscribeSpeakers[key].Output, "speaker_checkpoint") } } if record.HasVersionedAnalyzeState() { keys := make([]string, 0, len(record.AnalyzeArtifacts)) for key := range record.AnalyzeArtifacts { keys = append(keys, key) } sort.Strings(keys) for _, key := range keys { value := record.AnalyzeArtifacts[key] if value.Status == manifest.AnalyzeArtifactCurrent && value.Output != nil { add(*value.Output, "artifact_checkpoint") } } } } return out }