Report structured session validation and inspection results
This commit is contained in:
301
internal/app/session_state_command.go
Normal file
301
internal/app/session_state_command.go
Normal file
@@ -0,0 +1,301 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sort"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
type sessionStateReport struct {
|
||||
Campaign string `json:"campaign"`
|
||||
SessionID string `json:"session_id"`
|
||||
SessionSource string `json:"session_source,omitempty"`
|
||||
LocalManifest string `json:"local_manifest"`
|
||||
Stages []sessionStageState `json:"stages"`
|
||||
Artifacts []sessionArtifactState `json:"artifacts"`
|
||||
Remote *sessionRemoteState `json:"remote,omitempty"`
|
||||
Configuration string `json:"configuration"`
|
||||
PersistedConfiguration string `json:"persisted_configuration,omitempty"`
|
||||
LocalAudio *sessionAudioState `json:"local_audio,omitempty"`
|
||||
RemoteAudio *sessionAudioState `json:"remote_audio,omitempty"`
|
||||
Previous []sessionPreviousState `json:"previous_artifacts,omitempty"`
|
||||
Findings []finding `json:"findings"`
|
||||
}
|
||||
type sessionAudioState struct {
|
||||
State string `json:"state"`
|
||||
Count int `json:"count"`
|
||||
Prefix string `json:"prefix,omitempty"`
|
||||
}
|
||||
type sessionPreviousState struct {
|
||||
Name string `json:"name"`
|
||||
Required bool `json:"required"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
type sessionStageState struct {
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
SkipReason string `json:"skip_reason,omitempty"`
|
||||
}
|
||||
type sessionArtifactState struct {
|
||||
artifactDefinitionReport
|
||||
Availability string `json:"availability"`
|
||||
Provenance string `json:"provenance,omitempty"`
|
||||
}
|
||||
type sessionRemoteState struct {
|
||||
State string `json:"state"`
|
||||
RunID string `json:"run_id,omitempty"`
|
||||
ManifestKey string `json:"manifest_key,omitempty"`
|
||||
Locks []sessionLockState `json:"locks"`
|
||||
Outputs []sessionPublishedOutput `json:"outputs"`
|
||||
}
|
||||
type sessionLockState struct {
|
||||
Source string `json:"source"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
type sessionPublishedOutput struct {
|
||||
Source string `json:"source"`
|
||||
Destination string `json:"destination"`
|
||||
Required bool `json:"required"`
|
||||
State string `json:"state"`
|
||||
LockState string `json:"lock_state"`
|
||||
Outcome string `json:"outcome,omitempty"`
|
||||
DeclaredDestination string `json:"declared_destination,omitempty"`
|
||||
DeclaredKey string `json:"declared_key,omitempty"`
|
||||
OriginRun string `json:"origin_run,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func sessionStateCommand(name string, status bool, deps commandDependencies) commandkit.Command {
|
||||
spec := sessionInspectionSpec(name, "Inspect session state without changing it.", !status)
|
||||
spec.Run = func(invocation commandkit.Invocation, input *sessionInspectionInput) commandkit.Outcome {
|
||||
report, err := inspectSelectedSessionState(invocation.Context, input, status, deps)
|
||||
if report != nil {
|
||||
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
|
||||
}
|
||||
return operationOutcome(err)
|
||||
}
|
||||
return commandkit.NewCommand(spec)
|
||||
}
|
||||
|
||||
func inspectSelectedSessionState(ctx context.Context, input *sessionInspectionInput, status bool, deps commandDependencies) (report *sessionStateReport, err error) {
|
||||
deps = normalizeCommandDependencies(deps)
|
||||
deps.secretsBootstrap = &secretsBootstrap{}
|
||||
acquired := newConfigurationAcquisition(input.Selection, deps)
|
||||
acquired.previousSessionID = input.PreviousSession.Value
|
||||
defer func() { err = errors.Join(err, acquired.Close()) }()
|
||||
report = &sessionStateReport{Campaign: input.Selection.Campaign.Value, SessionID: input.Selection.Session.Value, LocalManifest: "not_checked", Stages: []sessionStageState{}, Artifacts: []sessionArtifactState{}, Findings: []finding{}}
|
||||
cfg, err := acquired.Domain(ctx)
|
||||
if err != nil {
|
||||
report.Findings = append(report.Findings, errorFinding("config", err.Error()))
|
||||
return report, err
|
||||
}
|
||||
return inspectSessionState(ctx, cfg, status, status || input.Remote.Value, deps)
|
||||
}
|
||||
|
||||
func inspectSessionState(ctx context.Context, cfg *config.Config, status, remote bool, deps commandDependencies) (*sessionStateReport, error) {
|
||||
report := &sessionStateReport{Campaign: cfg.Session.Campaign, SessionID: cfg.Session.SessionID, SessionSource: sessionSourceSummary(cfg), LocalManifest: "missing", Stages: []sessionStageState{}, Artifacts: []sessionArtifactState{}, Findings: []finding{}}
|
||||
var resultErr error
|
||||
fail := func(category string, err error) {
|
||||
if err != nil {
|
||||
report.Findings = append(report.Findings, errorFinding(category, err.Error()))
|
||||
resultErr = errors.Join(resultErr, err)
|
||||
}
|
||||
}
|
||||
if err := config.Validate(cfg); err != nil {
|
||||
fail("config", err)
|
||||
return report, resultErr
|
||||
}
|
||||
report.Configuration = effectiveConfigSummary(cfg)
|
||||
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
local, err := loadLocalManifest(ctx, paths.ManifestPath)
|
||||
if err != nil {
|
||||
report.LocalManifest = "error"
|
||||
fail("manifest", err)
|
||||
} else if local != nil {
|
||||
report.LocalManifest = "present"
|
||||
report.PersistedConfiguration = persistedEffectiveConfigSummary(local.EffectiveConfig)
|
||||
names := make([]string, 0, len(local.Stages))
|
||||
for name := range local.Stages {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
record := local.Stages[name]
|
||||
if record != nil {
|
||||
report.Stages = append(report.Stages, sessionStageState{Name: name, Status: string(record.Status), SkipReason: inspectionSkipReason(record)})
|
||||
}
|
||||
}
|
||||
}
|
||||
catalog, catalogErr := buildHelperArtifactCatalog(cfg, local)
|
||||
fail("artifacts", catalogErr)
|
||||
definitions, definitionsErr := describeArtifactDefinitions(cfg.Pipeline, cfg.ArtifactCatalog)
|
||||
fail("artifacts", definitionsErr)
|
||||
if catalogErr == nil && definitionsErr == nil {
|
||||
for _, definition := range definitions.Artifacts {
|
||||
entry, _ := catalog.Lookup(definition.Source)
|
||||
state := "unavailable"
|
||||
if entry.Available {
|
||||
state = "available"
|
||||
}
|
||||
if definition.Key == "" {
|
||||
state = builtinArtifactInspectionState(local, entry)
|
||||
}
|
||||
report.Artifacts = append(report.Artifacts, sessionArtifactState{artifactDefinitionReport: definition, Availability: state, Provenance: entry.Provenance})
|
||||
}
|
||||
}
|
||||
var store storage.ObjectStore
|
||||
if remote {
|
||||
report.Remote = &sessionRemoteState{State: "not_configured", Locks: []sessionLockState{}, Outputs: []sessionPublishedOutput{}}
|
||||
store, err = objectStoreIfConfiguredWithDependencies(ctx, cfg, deps)
|
||||
if err != nil {
|
||||
report.Remote.State = "error"
|
||||
fail("storage", err)
|
||||
} else if store != nil {
|
||||
current := inspectRemoteCurrentStateWithDependencies(ctx, cfg, store, deps)
|
||||
if current.Err != nil {
|
||||
if errors.Is(current.Err, artifacts.ErrCurrentCommitPointerMissing) {
|
||||
report.Remote.State = "missing"
|
||||
} else {
|
||||
report.Remote.State = "error"
|
||||
fail("remote", current.Err)
|
||||
}
|
||||
} else if current.State != nil {
|
||||
report.Remote.State = "current"
|
||||
report.Remote.RunID = current.State.RunID
|
||||
report.Remote.ManifestKey = current.State.CurrentManifestKey
|
||||
}
|
||||
locks := inspectRemoteLocks(ctx, cfg, store)
|
||||
fail("locks", locks.Err)
|
||||
for _, lock := range locks.Locks {
|
||||
report.Remote.Locks = append(report.Remote.Locks, sessionLockState{Source: lock.Source, Reason: lock.Reason})
|
||||
}
|
||||
if catalogErr == nil {
|
||||
outputs, err := inspectPublishedOutputs(ctx, cfg, store, catalog, current.State, locks.Locks, locks.Err == nil, report.Remote.State)
|
||||
report.Remote.Outputs = outputs
|
||||
fail("publication", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if status {
|
||||
for _, check := range inspectStableInputs(cfg) {
|
||||
if check.Err != nil {
|
||||
fail("inputs", check.Err)
|
||||
} else {
|
||||
report.Findings = append(report.Findings, infoFinding("inputs", check.Name+": "+check.Path))
|
||||
}
|
||||
}
|
||||
audio := inspectLocalAudioPresence(cfg)
|
||||
if audio.Checked {
|
||||
report.LocalAudio = &sessionAudioState{State: "available", Count: len(audio.Paths)}
|
||||
if audio.Err != nil {
|
||||
report.LocalAudio.State = "unavailable"
|
||||
fail("audio", audio.Err)
|
||||
}
|
||||
}
|
||||
if cfg.Session.Inputs.AudioS3 != nil {
|
||||
report.RemoteAudio = &sessionAudioState{State: "unavailable"}
|
||||
if store != nil {
|
||||
audio := inspectRemoteAudioPresence(ctx, cfg, store)
|
||||
report.RemoteAudio.Count = len(audio.Keys)
|
||||
report.RemoteAudio.Prefix = audio.Prefix
|
||||
if audio.Err == nil {
|
||||
report.RemoteAudio.State = "available"
|
||||
} else {
|
||||
fail("audio", audio.Err)
|
||||
}
|
||||
}
|
||||
}
|
||||
effective, err := resolveEffectiveArtifacts(cfg, nil)
|
||||
fail("artifacts", err)
|
||||
if err == nil {
|
||||
previous := inspectPreviousArtifactReadiness(ctx, cfg, store, artifacts.CollectPreviousArtifactRequirements(configuredScriptoriumArtifacts(cfg), effective))
|
||||
fail("previous", previous.Err)
|
||||
for _, requirement := range previous.Requirements {
|
||||
state := "ready"
|
||||
if previous.Err != nil || previousRequirementSkipped(previous.SkippedMissing, requirement.Name) {
|
||||
state = "unavailable"
|
||||
}
|
||||
report.Previous = append(report.Previous, sessionPreviousState{Name: requirement.Name, Required: requirement.Required, State: state})
|
||||
}
|
||||
}
|
||||
}
|
||||
return report, resultErr
|
||||
}
|
||||
|
||||
// Built-in inspection reports manifest evidence without opening artifact payloads.
|
||||
func builtinArtifactInspectionState(local *manifest.Manifest, entry artifacts.CatalogEntry) string {
|
||||
if local == nil {
|
||||
return "unavailable"
|
||||
}
|
||||
record := local.Stages[entry.ProducerStage]
|
||||
if record == nil || record.Status != manifest.StatusSucceeded {
|
||||
return "unavailable"
|
||||
}
|
||||
for _, output := range record.Outputs {
|
||||
if output.Kind == entry.OutputKind {
|
||||
return "recorded"
|
||||
}
|
||||
}
|
||||
return "unavailable"
|
||||
}
|
||||
|
||||
func inspectPublishedOutputs(ctx context.Context, cfg *config.Config, store storage.ObjectStore, catalog *artifacts.ArtifactCatalog, current *RemoteCurrentState, locks []artifacts.RemoteLock, locksKnown bool, remoteState string) ([]sessionPublishedOutput, error) {
|
||||
outputs := make([]sessionPublishedOutput, 0, len(cfg.PublicationOutputs()))
|
||||
locked := remoteLockSourceSet(locks)
|
||||
outcomes := map[string]string{}
|
||||
if current != nil {
|
||||
outcomes = committedPublishOutputOutcomes(current.Manifest)
|
||||
}
|
||||
var resultErr error
|
||||
for _, rule := range cfg.PublicationOutputs() {
|
||||
destination, _, err := helperPublishedOutputDest(rule, catalog)
|
||||
_, isLocked := locked[rule.Source]
|
||||
output := sessionPublishedOutput{Source: rule.Source, Destination: destination, Required: rule.Required == nil || *rule.Required, LockState: "unknown", State: "missing", Outcome: outcomes[rule.Source]}
|
||||
if locksKnown {
|
||||
output.LockState = "unlocked"
|
||||
if isLocked {
|
||||
output.LockState = "locked"
|
||||
}
|
||||
}
|
||||
if remoteState == "error" {
|
||||
output.State = "error"
|
||||
}
|
||||
if err == nil && current != nil && current.Commit != nil {
|
||||
if declaration, ok := committedPublishedOutput(current.Commit, rule.Source); ok {
|
||||
identity, identityErr := artifacts.ParsePublishedOutputObjectKey(current.SessionPrefix, declaration.DestinationKey)
|
||||
if identityErr != nil {
|
||||
err = identityErr
|
||||
} else {
|
||||
output.DeclaredDestination = identity.Destination
|
||||
output.DeclaredKey = declaration.DestinationKey
|
||||
output.OriginRun = identity.RunID
|
||||
exists, existsErr := store.Exists(ctx, declaration.DestinationKey)
|
||||
err = existsErr
|
||||
if exists && err == nil {
|
||||
output.State = "published"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
output.State = "error"
|
||||
output.Error = err.Error()
|
||||
resultErr = errors.Join(resultErr, err)
|
||||
}
|
||||
outputs = append(outputs, output)
|
||||
}
|
||||
return outputs, resultErr
|
||||
}
|
||||
|
||||
func inspectionSkipReason(record *manifest.StageRecord) string {
|
||||
if record.Status == manifest.StatusSkipped && record.Error != nil {
|
||||
return record.Error.Message
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user