2026-11-30 15:16:00 +00:00
package app
import (
"errors"
"fmt"
"path/filepath"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/notarius"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/preflight"
"gitea.maximumdirect.net/eric/narratio/internal/stage"
)
type readinessCheck struct {
Group string ` json:"group" `
Outcome string ` json:"outcome" `
Message string ` json:"message,omitempty" `
}
type downstreamReadiness struct {
Checks [ ] readinessCheck
Findings [ ] preflight . Finding
}
// checkDownstreamReadiness checks independent tools using this invocation's
// prepared state and retains adapters for subsequent processing.
func checkDownstreamReadiness ( runtime * invocationRuntime , targets preflight . Targets , prepared , useCurrent , force bool ) ( downstreamReadiness , error ) {
var result downstreamReadiness
var failures [ ] error
record := func ( group string , checked preflight . Result , err error ) {
result . Findings = append ( result . Findings , checked . Findings ... )
outcome , message := "succeeded" , ""
if err != nil {
outcome , message = "failed" , err . Error ( )
failures = append ( failures , err )
}
result . Checks = append ( result . Checks , readinessCheck { Group : group , Outcome : outcome , Message : message } )
}
blocked := func ( group string ) {
result . Checks = append ( result . Checks , readinessCheck { Group : group , Outcome : "blocked" , Message : "prepared inputs are unavailable" } )
}
if len ( targets . Scriptorium ) > 0 && runtime . ctx . Err ( ) == nil {
2026-11-30 15:42:21 +00:00
runtime . env . Progress ( "checking prompt requirements" )
2026-11-30 15:16:00 +00:00
if runtime . env . Scriptorium == nil {
runtime . env . Scriptorium = runtime . dependencies . newScriptorium ( )
}
checked , err := preflight . Check ( runtime . ctx , runtime . env . Scriptorium , targets . Scriptorium )
record ( "prompt requirements" , checked , err )
}
if targets . Polish && runtime . ctx . Err ( ) == nil {
if ! prepared {
blocked ( "audita" )
} else {
2026-11-30 15:42:21 +00:00
runtime . env . Progress ( "checking Audita readiness" )
2026-11-30 15:16:00 +00:00
var err error
if runtime . env . Audita == nil {
runtime . env . Audita , err = runtime . dependencies . newAudita ( runtime . cfg )
if err != nil {
err = fmt . Errorf ( "initialize audita runner: %w" , err )
}
}
var checked preflight . Result
if err == nil {
checker , ok := runtime . env . Audita . ( preflight . AuditaChecker )
if ! ok || checker == nil {
err = fmt . Errorf ( "audita runner does not support preflight" )
} else {
root := artifacts . SessionInvocationProbeDir ( runtime . paths , runtime . identity . RunID , "audita" )
request , _ , buildErr := stage . BuildPolishRequest ( runtime . paths , runtime . manifest , "" , filepath . Join ( root , "outputs" , "transcripts" , "polished.json" ) , filepath . Join ( root , "logs" ) , filepath . Join ( root , "debug" ) )
if buildErr != nil {
err = fmt . Errorf ( "build audita preflight: %w" , buildErr )
} else {
checked , err = preflight . CheckAudita ( runtime . ctx , checker , request )
2026-11-30 15:42:21 +00:00
checked . Findings = append ( [ ] preflight . Finding { { Severity : preflight . SeverityInfo , Stage : "polish" , Message : "Audita checks a synthetic transcript and may contact providers" } } , checked . Findings ... )
2026-11-30 15:16:00 +00:00
}
}
}
record ( "audita" , checked , err )
}
}
if targets . Notarius && runtime . ctx . Err ( ) == nil {
if ! prepared {
blocked ( "notarius" )
} else {
2026-11-30 15:42:21 +00:00
runtime . env . Progress ( "checking Notarius readiness" )
2026-11-30 15:16:00 +00:00
if runtime . env . Notarius == nil {
runtime . env . Notarius = runtime . dependencies . newNotarius ( )
}
var checked preflight . Result
var err error
native , ok := runtime . env . Notarius . ( notarius . ReadinessChecker )
if ! ok || native == nil {
err = fmt . Errorf ( "notarius runner does not support preflight" )
} else {
root := artifacts . SessionInvocationProbeDir ( runtime . paths , runtime . identity . RunID , "notarius" )
probe , buildErr := stage . BuildNotariusReadiness ( runtime . cfg , runtime . paths , runtime . manifest , root , useCurrent , force )
if buildErr != nil {
err = fmt . Errorf ( "build notarius preflight: %w" , buildErr )
} else {
checked , err = preflight . CheckNotarius ( runtime . ctx , native , probe . Request )
if verifyErr := probe . VerifyReferences ( ) ; verifyErr != nil {
err = errors . Join ( err , fmt . Errorf ( "verify Notarius probe references: %w" , verifyErr ) )
}
}
}
record ( "notarius" , checked , err )
}
}
return result , errors . Join ( append ( failures , runtime . ctx . Err ( ) ) ... )
}