2026-09-24 17:48:52 +00:00
package cli
import (
2026-11-10 20:21:11 +00:00
"errors"
2026-09-24 17:48:52 +00:00
"fmt"
2026-09-24 21:48:18 +00:00
"sync"
2026-09-24 17:48:52 +00:00
2026-10-11 01:40:38 +00:00
"gitea.maximumdirect.net/eric/audita/v3/internal/core/normalization"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/outputschema"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/reporting"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/llm"
2026-11-16 14:18:38 +00:00
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/preflight"
2026-11-16 15:00:05 +00:00
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/runner"
2026-11-10 20:21:11 +00:00
"go.maximumdirect.net/ckconfig"
2026-11-10 17:12:28 +00:00
commandkit "go.maximumdirect.net/commandkit"
2026-11-10 20:21:11 +00:00
ck "go.maximumdirect.net/configkit"
2026-09-24 17:48:52 +00:00
)
2026-11-10 17:12:28 +00:00
// processResult carries the runner's transcript and ordinary report facts.
type processResult struct {
Transcript * schema . Transcript
Snapshot reporting . Snapshot
2026-09-24 17:48:52 +00:00
}
2026-11-10 17:12:28 +00:00
func runProcess ( inv commandkit . Invocation , input processInput ) commandkit . Outcome {
loaded , err := loadProcessInputs ( inv . Context , input , inv . ReportingMode ( ) )
2026-09-24 17:48:52 +00:00
if err != nil {
2026-11-17 04:18:29 +00:00
if canceled := inv . Context . Err ( ) ; canceled != nil && ! errors . Is ( err , canceled ) {
err = errors . Join ( err , canceled )
2026-11-10 17:12:28 +00:00
}
2026-11-10 20:21:11 +00:00
var loadErr * ck . LoadError
if errors . As ( err , & loadErr ) {
_ , explanation := ckconfig . AdaptDiagnostics ( "Application configuration:\n" , loadErr . Sources ( ) , loadErr . Diagnostics ( ) )
2026-11-17 04:18:29 +00:00
if ckconfig . ClassifyConfigurationError ( err ) == commandkit . OutcomeUsage {
return commandkit . Usage ( loadErr . Error ( ) , explanation . Details ... ) . WithCause ( err )
}
return commandkit . Failure ( loadErr . Error ( ) , explanation . Details ... ) . WithCause ( err )
}
if inv . Context . Err ( ) != nil || llm . IsAssetError ( err ) {
return commandkit . Failure ( "Could not prepare processing." ) . WithCause ( err )
2026-11-10 20:21:11 +00:00
}
2026-11-10 17:12:28 +00:00
return commandkit . Usage ( err . Error ( ) ) . WithCause ( err )
2026-09-24 17:48:52 +00:00
}
2026-11-10 17:12:28 +00:00
prepared , err := prepareProcess ( inv . Context , input , loaded , inv . Reporter ( ) . Output ( ) )
2026-09-24 17:48:52 +00:00
if err != nil {
2026-11-16 14:18:38 +00:00
var selection * preflight . SelectionError
if inv . Context . Err ( ) == nil && errors . As ( err , & selection ) {
return commandkit . Usage ( err . Error ( ) ) . WithCause ( err )
}
2026-11-16 03:37:29 +00:00
return commandkit . Failure ( err . Error ( ) ) . WithCause ( err )
2026-09-24 17:48:52 +00:00
}
2026-11-10 17:12:28 +00:00
defer prepared . Prepared . Output . Close ( )
var warningsMu sync . Mutex
warn := func ( err error ) {
if err == nil {
return
}
warningsMu . Lock ( )
prepared . Prepared . Warnings = append ( prepared . Prepared . Warnings , err )
warningsMu . Unlock ( )
2026-09-24 17:48:52 +00:00
}
2026-11-10 17:12:28 +00:00
debug := prepared . Prepared . Debug
if debug != nil {
warn ( debug . WriteSourceTranscript ( loaded . Source , loaded . RawTranscript ) )
2026-09-24 17:48:52 +00:00
}
2026-11-16 03:56:54 +00:00
loaded . RawTranscript = nil
2026-11-10 17:12:28 +00:00
cfg := & loaded . Configuration . Config
2026-09-24 17:48:52 +00:00
normalizer := normalization . NewNormalizer ( normalization . NormalizationConfig {
2026-11-10 17:12:28 +00:00
MaxSegmentGap : cfg . Normalization . MaxSegmentGap , EllipsisGap : cfg . Normalization . EllipsisGap ,
MaxSegmentDuration : cfg . Normalization . MaxSegmentDuration , MaxSegmentTokens : cfg . Normalization . MaxSegmentTokens ,
2026-09-24 17:48:52 +00:00
} )
2026-11-16 03:56:54 +00:00
transcript := loaded . Source . Canonical ( )
loaded . Source = nil
normalized , summary := normalizer . Normalize ( transcript )
2026-11-10 17:12:28 +00:00
if debug != nil {
warn ( debug . WriteNormalizedTranscript ( normalized ) )
warn ( debug . WriteNormalizationSummary ( summary ) )
2026-09-24 17:48:52 +00:00
}
2026-11-10 20:28:41 +00:00
if err := inv . Context . Err ( ) ; err != nil {
2026-11-16 15:00:05 +00:00
return finishProcess ( inv , input , prepared , reporting . Snapshot { } , nil , & runner . ExecutionFailure { Phase : "normalization" , Cause : err } )
2026-11-10 20:28:41 +00:00
}
2026-11-16 15:12:06 +00:00
reporter := inv . Reporter ( )
result , runErr := executePipeline ( inv . Context , loaded , prepared , normalized , warn , func ( message string ) {
_ = reporter . Progress ( message )
} )
2026-09-24 17:48:52 +00:00
if runErr != nil {
2026-11-16 03:37:29 +00:00
return finishProcess ( inv , input , prepared , result . Snapshot , nil , runErr )
2026-09-24 17:48:52 +00:00
}
2026-11-10 17:12:28 +00:00
encoder , err := outputschema . Resolve ( cfg . Output . Schema )
2026-09-24 17:48:52 +00:00
if err != nil {
2026-11-16 15:00:05 +00:00
return finishProcess ( inv , input , prepared , result . Snapshot , nil , & runner . ExecutionFailure { Phase : "encoding" , Cause : err } )
2026-09-24 17:48:52 +00:00
}
2026-11-10 17:12:28 +00:00
if result . Transcript == nil {
2026-11-16 15:00:05 +00:00
return finishProcess ( inv , input , prepared , result . Snapshot , nil , & runner . ExecutionFailure { Phase : "encoding" , Cause : fmt . Errorf ( "processing returned no transcript" ) } )
2026-09-24 17:48:52 +00:00
}
2026-11-10 17:12:28 +00:00
payload , err := encoder . Encoder ( result . Transcript )
2026-09-24 17:48:52 +00:00
if err != nil {
2026-11-16 15:00:05 +00:00
return finishProcess ( inv , input , prepared , result . Snapshot , nil , & runner . ExecutionFailure { Phase : "encoding" , Cause : err } )
2026-09-24 17:48:52 +00:00
}
2026-11-16 03:37:29 +00:00
return finishProcess ( inv , input , prepared , result . Snapshot , payload , nil )
2026-09-24 17:48:52 +00:00
}
2026-11-10 17:12:28 +00:00
// DebugPath returns the retained artifact directory, if its preparation succeeded.
func ( p * preparedProcess ) DebugPath ( ) string {
if p == nil || p . Prepared == nil || p . Prepared . Debug == nil {
return ""
2026-09-24 21:48:18 +00:00
}
2026-11-10 17:12:28 +00:00
return p . Prepared . Debug . Path ( )
2026-09-24 21:48:18 +00:00
}