Prepare sessions before checking readiness
This commit is contained in:
@@ -176,7 +176,7 @@ can share the full lifecycle without copying stage transitions or finalization.
|
||||
|
||||
## Stage 4: Implement The Standalone Prepare-And-Check Operation
|
||||
|
||||
Status: Pending.
|
||||
Status: Complete. Verified with `GOTOOLCHAIN=go1.27.4 go test ./internal/app ./internal/preflight`, focused readiness tests after the final reporting adjustment, and `git diff --check`.
|
||||
|
||||
Read `internal/app/{session_inspection_command,operator_inspection,operator_findings,configuration_acquisition,command_dependencies}.go`,
|
||||
[Preparation](../internal/stage-prepare.md), and existing session preflight tests.
|
||||
|
||||
@@ -30,7 +30,7 @@ func executionInspectionTargets(cfg *config.Config, sessionID string, projection
|
||||
return buildInspectionTargets(cfg, sessionID, trim, analysis)
|
||||
}
|
||||
|
||||
func validationInspectionTargets(cfg *config.Config, sessionID string) ([]preflight.Target, error) {
|
||||
func enabledInspectionTargets(cfg *config.Config, sessionID string) ([]preflight.Target, error) {
|
||||
if cfg == nil || cfg.Pipeline == nil {
|
||||
return nil, fmt.Errorf("resolved configuration is required")
|
||||
}
|
||||
@@ -98,9 +98,9 @@ func executionDownstreamTargets(cfg *config.Config, sessionID string, projection
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// validationDownstreamTargets selects checks for enabled functionality regardless of reuse.
|
||||
func validationDownstreamTargets(cfg *config.Config, sessionID string) (preflight.Targets, error) {
|
||||
targets, err := validationInspectionTargets(cfg, sessionID)
|
||||
// enabledDownstreamTargets selects checks for enabled functionality regardless of reuse.
|
||||
func enabledDownstreamTargets(cfg *config.Config, sessionID string) (preflight.Targets, error) {
|
||||
targets, err := enabledInspectionTargets(cfg, sessionID)
|
||||
if err != nil {
|
||||
return preflight.Targets{}, err
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func TestInspectionTargetsUseProjectedWorkAndDefinitionClosure(t *testing.T) {
|
||||
if targets[2].Context.Vars["session_id"] != "narratio-session-"+cfg.Session.SessionID {
|
||||
t.Fatalf("context = %+v", targets[2].Context)
|
||||
}
|
||||
validation, err := validationInspectionTargets(cfg, cfg.Session.SessionID)
|
||||
validation, err := enabledInspectionTargets(cfg, cfg.Session.SessionID)
|
||||
if err != nil || !reflect.DeepEqual(targets, validation) {
|
||||
t.Fatalf("validation = %+v, error = %v", validation, err)
|
||||
}
|
||||
@@ -72,7 +72,7 @@ func TestInspectionTargetsSkipDisabledTrimAndAbsentScriptorium(t *testing.T) {
|
||||
disabled := false
|
||||
cfg.Pipeline.Trim.Enabled = &disabled
|
||||
cfg.Pipeline.Scriptorium = nil
|
||||
targets, err := validationInspectionTargets(cfg, cfg.Session.SessionID)
|
||||
targets, err := enabledInspectionTargets(cfg, cfg.Session.SessionID)
|
||||
if err != nil || len(targets) != 0 {
|
||||
t.Fatalf("targets = %+v, error = %v", targets, err)
|
||||
}
|
||||
@@ -146,7 +146,7 @@ func TestDownstreamTargetSelectionForPolish(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
got, err := validationDownstreamTargets(cfg, "session")
|
||||
got, err := enabledDownstreamTargets(cfg, "session")
|
||||
if err != nil || !got.Polish {
|
||||
t.Fatalf("validation targets: %+v %v", got, err)
|
||||
}
|
||||
@@ -170,12 +170,12 @@ func TestDownstreamTargetSelectionForNotarius(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
got, err := validationDownstreamTargets(cfg, "session")
|
||||
got, err := enabledDownstreamTargets(cfg, "session")
|
||||
if err != nil || !got.Notarius {
|
||||
t.Fatalf("validation targets = %#v, %v", got, err)
|
||||
}
|
||||
cfg.Pipeline.Notarius.Enabled = false
|
||||
got, err = validationDownstreamTargets(cfg, "session")
|
||||
got, err = enabledDownstreamTargets(cfg, "session")
|
||||
if err != nil || got.Notarius {
|
||||
t.Fatalf("disabled targets = %#v, %v", got, err)
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ func inspectSessionValidation(ctx context.Context, cfg *config.Config, deps comm
|
||||
effective, effectiveErr := resolveEffectiveArtifacts(cfg, nil)
|
||||
fail("config", effectiveErr)
|
||||
if configErr == nil && effectiveErr == nil {
|
||||
targets, targetErr := validationDownstreamTargets(cfg, cfg.Session.SessionID)
|
||||
targets, targetErr := enabledDownstreamTargets(cfg, cfg.Session.SessionID)
|
||||
if targetErr != nil {
|
||||
fail("preflight", targetErr)
|
||||
} else {
|
||||
|
||||
@@ -46,7 +46,7 @@ func TestSessionValidationSharesPromptFindingsAndLeavesNoWorkspace(t *testing.T)
|
||||
if err != nil || !report.Valid || len(fake.RequirementsRequests) == 0 {
|
||||
t.Fatalf("report = %+v, error = %v", report, err)
|
||||
}
|
||||
targets, err := validationInspectionTargets(cfg, cfg.Session.SessionID)
|
||||
targets, err := enabledInspectionTargets(cfg, cfg.Session.SessionID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
179
internal/app/session_readiness.go
Normal file
179
internal/app/session_readiness.go
Normal file
@@ -0,0 +1,179 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/preflight"
|
||||
)
|
||||
|
||||
type preparationAssessment struct {
|
||||
Outcome string `json:"outcome"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
type sessionReadinessReport struct {
|
||||
Campaign string `json:"campaign"`
|
||||
SessionID string `json:"session_id"`
|
||||
SessionSource string `json:"session_source,omitempty"`
|
||||
RunID string `json:"run_id,omitempty"`
|
||||
Preparation preparationAssessment `json:"preparation"`
|
||||
Ready bool `json:"ready"`
|
||||
Checks []readinessCheck `json:"checks"`
|
||||
Findings []finding `json:"findings"`
|
||||
}
|
||||
|
||||
func preflightSelectedSession(ctx context.Context, input *sessionInspectionInput, deps commandDependencies) (report *sessionReadinessReport, resultErr error) {
|
||||
deps = normalizeCommandDependencies(deps)
|
||||
report = &sessionReadinessReport{Campaign: input.Selection.Campaign.Value, SessionID: input.Selection.Session.Value, Preparation: preparationAssessment{Outcome: "not_attempted"}, Checks: []readinessCheck{}, Findings: []finding{}}
|
||||
acquired := newConfigurationAcquisition(input.Selection, deps)
|
||||
acquired.previousSessionID = input.PreviousSession.Value
|
||||
defer func() {
|
||||
if err := acquired.Close(); err != nil {
|
||||
resultErr = errors.Join(resultErr, err)
|
||||
report.Findings = append(report.Findings, errorFinding("configuration", err.Error()))
|
||||
}
|
||||
report.Ready = resultErr == nil
|
||||
}()
|
||||
cfg, err := acquired.Domain(ctx)
|
||||
if err == nil {
|
||||
err = config.Validate(cfg)
|
||||
}
|
||||
if err != nil {
|
||||
report.Checks = append(report.Checks, readinessCheck{Group: "configuration", Outcome: "failed", Message: err.Error()})
|
||||
report.Findings = append(report.Findings, errorFinding("configuration", err.Error()))
|
||||
return report, err
|
||||
}
|
||||
report.Campaign, report.SessionID, report.SessionSource = cfg.Session.Campaign, cfg.Session.SessionID, sessionSourceSummary(cfg)
|
||||
report.Checks = append(report.Checks, readinessCheck{Group: "configuration", Outcome: "succeeded"})
|
||||
plan, err := BuildBoundedPlan("prepare", "prepare")
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
invocationDeps := invocationDependencies{newAudita: deps.newAudita, newNotarius: deps.newNotarius, newScriptorium: deps.newScriptorium,
|
||||
newObjectStore: func(ctx context.Context, cfg *config.Config) (storage.ObjectStore, error) {
|
||||
return initializeObjectStoreBackend(ctx, cfg, deps.newObjectStore)
|
||||
}}
|
||||
summary, err := withLockedInvocation(ctx, cfg, plan, RunOptions{Reporter: deps.reporter}, invocationDeps, func(runtime *invocationRuntime) error {
|
||||
report.RunID = runtime.identity.RunID
|
||||
return assessSessionReadiness(runtime, report, deps)
|
||||
})
|
||||
if summary == nil && err != nil {
|
||||
report.Findings = append(report.Findings, errorFinding("workspace", err.Error()))
|
||||
report.Checks = append(report.Checks, readinessCheck{Group: "workspace/storage", Outcome: "failed", Message: err.Error()})
|
||||
} else if err != nil {
|
||||
// Finalization and lock-release failures may be joined after the checks.
|
||||
report.Findings = append(report.Findings, errorFinding("preflight", err.Error()))
|
||||
}
|
||||
return report, err
|
||||
}
|
||||
|
||||
func assessSessionReadiness(runtime *invocationRuntime, report *sessionReadinessReport, deps commandDependencies) error {
|
||||
prepareErr := executeSelectedStages(runtime)
|
||||
prepared := false
|
||||
if len(runtime.stageResults) > 0 {
|
||||
item := runtime.stageResults[0]
|
||||
report.Preparation = preparationAssessment{Outcome: item.Outcome, Reason: item.Reason}
|
||||
prepared = prepareErr == nil && (item.Outcome == "completed" || item.Outcome == "reused")
|
||||
}
|
||||
if prepareErr != nil {
|
||||
report.Findings = append(report.Findings, errorFinding("preparation", prepareErr.Error()))
|
||||
}
|
||||
outcome, message := "succeeded", ""
|
||||
if !prepared {
|
||||
outcome = "failed"
|
||||
if prepareErr != nil {
|
||||
message = prepareErr.Error()
|
||||
}
|
||||
}
|
||||
report.Checks = append(report.Checks, readinessCheck{Group: "preparation", Outcome: outcome, Message: message})
|
||||
if prepared {
|
||||
report.Findings = append(report.Findings, okFinding("inputs", "prepared session inputs and audio are current"))
|
||||
if record := runtime.manifest.Stages["prepare"]; record != nil {
|
||||
if missing, ok := record.Metadata["previous_artifacts_missing_optional"]; ok {
|
||||
report.Findings = append(report.Findings, infoFinding("previous", fmt.Sprintf("optional previous artifacts unavailable: %v", missing)))
|
||||
}
|
||||
}
|
||||
}
|
||||
var failures []error
|
||||
failures = append(failures, prepareErr)
|
||||
if runtime.ctx.Err() != nil {
|
||||
return errors.Join(append(failures, runtime.ctx.Err())...)
|
||||
}
|
||||
targets, err := enabledDownstreamTargets(runtime.cfg, runtime.identity.SessionID)
|
||||
if err != nil {
|
||||
failures = append(failures, err)
|
||||
report.Checks = append(report.Checks, readinessCheck{Group: "prompt requirements", Outcome: "failed", Message: err.Error()})
|
||||
report.Findings = append(report.Findings, errorFinding("preflight", err.Error()))
|
||||
} else {
|
||||
useCurrent := false
|
||||
if prepared && targets.Notarius {
|
||||
plan, planErr := BuildBoundedPlan("transcribe", "trim")
|
||||
if planErr == nil {
|
||||
var projection *workProjection
|
||||
projection, planErr = projectSelectedWork(runtime.ctx, runtime.cfg, plan, false, nil, runtime.effectiveArtifacts, runtime.artifactStore, runtime.manifest)
|
||||
if planErr == nil {
|
||||
useCurrent = currentTranscriptUsableForNotarius(projection)
|
||||
}
|
||||
}
|
||||
if planErr != nil {
|
||||
failures = append(failures, fmt.Errorf("project transcript readiness: %w", planErr))
|
||||
report.Findings = append(report.Findings, errorFinding("notarius", planErr.Error()))
|
||||
}
|
||||
}
|
||||
checked, checkErr := checkDownstreamReadiness(runtime, targets, prepared, useCurrent, false)
|
||||
report.Checks = append(report.Checks, checked.Checks...)
|
||||
failures = append(failures, checkErr)
|
||||
for _, check := range checked.Checks {
|
||||
if check.Outcome == "failed" && check.Message != "" {
|
||||
report.Findings = append(report.Findings, errorFinding(check.Group, check.Message))
|
||||
}
|
||||
}
|
||||
for _, item := range checked.Findings {
|
||||
category, text := "scriptorium", formatPromptFinding(item)
|
||||
if item.Stage == "polish" {
|
||||
category, text = "audita", item.Message
|
||||
} else if item.Stage == "extract" {
|
||||
category, text = "notarius", item.Message
|
||||
}
|
||||
switch item.Severity {
|
||||
case preflight.SeverityError:
|
||||
report.Findings = append(report.Findings, errorFinding(category, text))
|
||||
case preflight.SeverityWarning:
|
||||
report.Findings = append(report.Findings, warnFinding(category, text))
|
||||
default:
|
||||
report.Findings = append(report.Findings, infoFinding(category, text))
|
||||
}
|
||||
}
|
||||
}
|
||||
if runtime.ctx.Err() == nil {
|
||||
store := runtime.env.ObjectStore
|
||||
var storeErr error
|
||||
if store == nil {
|
||||
store, storeErr = objectStoreIfConfiguredWithDependencies(runtime.ctx, runtime.cfg, deps)
|
||||
}
|
||||
if storeErr != nil {
|
||||
failures = append(failures, storeErr)
|
||||
report.Checks = append(report.Checks, readinessCheck{Group: "workspace/storage", Outcome: "failed", Message: storeErr.Error()})
|
||||
report.Findings = append(report.Findings, errorFinding("storage", storeErr.Error()))
|
||||
} else {
|
||||
report.Checks = append(report.Checks, readinessCheck{Group: "workspace/storage", Outcome: "succeeded"})
|
||||
locks := inspectRemoteLocks(runtime.ctx, runtime.cfg, store)
|
||||
if locks.Err != nil {
|
||||
failures = append(failures, locks.Err)
|
||||
report.Checks = append(report.Checks, readinessCheck{Group: "publication locks", Outcome: "failed", Message: locks.Err.Error()})
|
||||
report.Findings = append(report.Findings, errorFinding("locks", locks.Err.Error()))
|
||||
} else if store != nil {
|
||||
report.Checks = append(report.Checks, readinessCheck{Group: "publication locks", Outcome: "succeeded"})
|
||||
for _, lock := range locks.Locks {
|
||||
report.Findings = append(report.Findings, warnFinding("locks", fmt.Sprintf("%s locked: %s", lock.Source, strings.TrimSpace(lock.Reason))))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return errors.Join(append(failures, runtime.ctx.Err())...)
|
||||
}
|
||||
76
internal/app/session_readiness_test.go
Normal file
76
internal/app/session_readiness_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
func readinessInput(pipeline, campaign, session string) *sessionInspectionInput {
|
||||
return &sessionInspectionInput{Selection: configurationSelection{Config: commandkit.Supplied[string]{Value: pipeline, Set: true}, CampaignFile: commandkit.Supplied[string]{Value: campaign, Set: true}, SessionFile: commandkit.Supplied[string]{Value: session, Set: true}, Session: commandkit.Supplied[string]{Value: "2026-05-03", Set: true}}}
|
||||
}
|
||||
|
||||
func TestSessionReadinessPreparesAndRepeatsChecks(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
pipeline, campaign, session := writeValidConfigFiles(t, root)
|
||||
provider := &audita.FakeRunner{}
|
||||
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||||
deps.newAudita = func(*config.Config) (audita.Runner, error) { return provider, nil }
|
||||
input := readinessInput(pipeline, campaign, session)
|
||||
first, err := preflightSelectedSession(context.Background(), input, deps)
|
||||
if err != nil || !first.Ready || first.Preparation.Outcome != "completed" || len(provider.PreflightRequests) != 1 {
|
||||
t.Fatalf("first=%+v error=%v", first, err)
|
||||
}
|
||||
second, err := preflightSelectedSession(context.Background(), input, deps)
|
||||
if err != nil || !second.Ready || second.Preparation.Outcome != "reused" || len(provider.PreflightRequests) != 2 {
|
||||
t.Fatalf("second=%+v error=%v", second, err)
|
||||
}
|
||||
failure := errors.New("synthetic provider failure")
|
||||
provider.PreflightErr = failure
|
||||
failed, err := preflightSelectedSession(context.Background(), input, deps)
|
||||
if !errors.Is(err, failure) || failed.Ready || failed.Preparation.Outcome != "reused" {
|
||||
t.Fatalf("failure=%+v error=%v", failed, err)
|
||||
}
|
||||
cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m, err := (&manifest.LocalStore{}).Load(context.Background(), manifestPathFor(cfg))
|
||||
if err != nil || m.Stages["prepare"].Status != manifest.StatusSucceeded {
|
||||
t.Fatalf("manifest=%+v error=%v", m, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionReadinessBlocksPreparedProbesButKeepsIndependentFindings(t *testing.T) {
|
||||
pipeline, campaign, session := writeValidConfigFilesWithScriptoriumArtifacts(t, t.TempDir())
|
||||
if err := os.Remove(filepath.Join(filepath.Dir(session), "audio", "alice.flac")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
provider := &audita.FakeRunner{}
|
||||
inspector := &scriptorium.FakeRunner{RequirementsResult: &scriptorium.RequirementsReport{Complete: false}}
|
||||
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||||
deps.newAudita = func(*config.Config) (audita.Runner, error) { return provider, nil }
|
||||
deps.newScriptorium = func() scriptorium.Runner { return inspector }
|
||||
report, err := preflightSelectedSession(context.Background(), readinessInput(pipeline, campaign, session), deps)
|
||||
if err == nil || report.Ready || report.Preparation.Outcome != "failed" || len(provider.PreflightRequests) != 0 || len(inspector.RequirementsRequests) == 0 || !findingMatches(report.Findings, "WARN", "scriptorium", "incomplete") {
|
||||
t.Fatalf("report=%+v error=%v", report, err)
|
||||
}
|
||||
found := false
|
||||
for _, check := range report.Checks {
|
||||
if check.Group == "audita" && check.Outcome == "blocked" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("checks=%+v", report.Checks)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user