226 lines
9.8 KiB
Go
226 lines
9.8 KiB
Go
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"
|
|
"go.maximumdirect.net/commandkit"
|
|
)
|
|
|
|
func preflightCommand(deps commandDependencies) commandkit.Command {
|
|
spec := sessionInspectionSpec("preflight", "Prepare session inputs and check readiness.", false)
|
|
spec.Description = "Preparation writes state and downloads inputs; readiness checks may contact providers."
|
|
spec.Run = func(invocation commandkit.Invocation, input *sessionInspectionInput) commandkit.Outcome {
|
|
report, err := preflightSelectedSession(invocation.Context, input, withCommandReporting(deps, invocation))
|
|
if report != nil {
|
|
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
|
|
}
|
|
return reportOperationOutcome(invocation, err)
|
|
}
|
|
return commandkit.NewCommand(spec)
|
|
}
|
|
|
|
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"})
|
|
return report, inspectConfiguredSessionReadiness(ctx, cfg, deps, report)
|
|
}
|
|
|
|
func inspectConfiguredSessionReadiness(ctx context.Context, cfg *config.Config, deps commandDependencies, report *sessionReadinessReport) error {
|
|
plan, err := BuildBoundedPlan("prepare", "prepare")
|
|
if err != nil {
|
|
return 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)
|
|
}}
|
|
result, 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 result == 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 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 !prepared && prepareErr == nil {
|
|
prepareErr = fmt.Errorf("preparation did not produce reusable input evidence")
|
|
report.Preparation.Outcome = "failed"
|
|
report.Preparation.Reason = prepareErr.Error()
|
|
}
|
|
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 hydrated := previousArtifactNames(record.Metadata["previous_artifacts_hydrated"]); len(hydrated) > 0 {
|
|
report.Findings = append(report.Findings, okFinding("previous", fmt.Sprintf("prepared previous artifacts: %v", hydrated)))
|
|
}
|
|
if missing := previousArtifactNames(record.Metadata["previous_artifacts_missing_optional"]); len(missing) > 0 {
|
|
report.Findings = append(report.Findings, infoFinding("previous", fmt.Sprintf("optional previous artifacts unavailable: %v", missing)))
|
|
}
|
|
if _, required := record.Metadata["previous_requirements_count"]; !required {
|
|
report.Findings = append(report.Findings, infoFinding("previous", "no previous-session artifacts required"))
|
|
}
|
|
}
|
|
}
|
|
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())...)
|
|
}
|
|
|
|
func previousArtifactNames(value any) []string {
|
|
switch items := value.(type) {
|
|
case []string:
|
|
return items
|
|
case []any:
|
|
var names []string
|
|
for _, item := range items {
|
|
if name, ok := item.(string); ok {
|
|
names = append(names, name)
|
|
}
|
|
}
|
|
return names
|
|
default:
|
|
return nil
|
|
}
|
|
}
|