2026-07-18 00:20:56 +00:00
package pipeline
import (
"context"
"fmt"
"time"
"gitea.maximumdirect.net/eric/notarius/internal/core/artifacts"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
)
type chunkPlanExecution struct {
2026-11-21 23:48:51 +00:00
outcome contracts . ProducerOutcome
2026-08-27 15:29:10 +00:00
accepted bool
chunks [ ] source . Chunk
plan * source . ChunkPlan
diagnostics [ ] contracts . DiagnosticGroup
rejection * contracts . RejectedOutput
lookup ChunkPlanDecision
record * ChunkPlanRecord
action string
summary artifacts . ChunkPlanSummary
validation * artifacts . ValidationSummary
2026-07-18 00:20:56 +00:00
}
func effectiveChunkCacheMode ( mode ChunkCacheMode ) ChunkCacheMode {
if mode == "" {
return ChunkCacheBypass
}
parsed , _ := ParseChunkCacheMode ( string ( mode ) )
return parsed
}
// Chunk-plan lookup and publication happen serially before lane workers start.
// The runner therefore does not add synchronization around the store.
func ( r * Runner ) runChunkPlan ( ctx context . Context , input RunInput , doc * source . SourceDocument , sourceInput contracts . LLMInputMaterial , sessionID string ) ( chunkPlanExecution , error ) {
mode := effectiveChunkCacheMode ( input . ChunkCacheMode )
chunker := input . Prepared . chunker
2026-11-21 23:48:51 +00:00
result := chunkPlanExecution { lookup : ChunkPlanDecision { Reason : "chunk plan lookup skipped" } , summary : artifacts . ChunkPlanSummary { Mode : string ( mode ) , SourceDigest : doc . Digest , RequestedModule : chunker . Key ( ) , LookupStatus : "skipped" , LookupReason : "chunk plan lookup skipped" , ValidationStatus : "not_run" , PublicationStatus : "not_requested" } }
target := typedValidationTarget { stage : StageChunk , moduleKey : chunker . Key ( ) , source : doc , sourceID : doc . ID , sourceInput : sourceInput . Clone ( ) , sessionID : sessionID , references : input . pipeline . ChunkReferences . ReferenceSet , metadata : input . Metadata , execution : input . Execution }
assessChunks := chunkCandidateAssessor ( target , input . Prepared . chunkValidators )
var assessment contracts . CandidateAssessment
operation := contracts . ProducerOperation [ source . ChunkPlan ] { Controls : input . pipeline . ChunkControls , Execution : input . Execution , ReuseEligible : true ,
Assess : func ( ctx context . Context , plan source . ChunkPlan ) ( contracts . CandidateAssessment , error ) {
_ , chunks , err := validateAndMaterializeChunkPlan ( doc , plan )
if err != nil {
if input . pipeline . ChunkExecutionClass == contracts . ExecutionClassDeterministic {
return contracts . CandidateAssessment { } , err
}
assessment = contracts . CandidateAssessment { Validators : [ ] contracts . ValidatorAssessment { { ValidatorName : "chunk materialization" , Decision : contracts . AssessmentFailed , Failure : err } } }
return assessment , nil
}
assessment , err = assessChunks ( ctx , chunks )
return assessment , err
2026-07-18 00:45:01 +00:00
} ,
}
2026-07-18 00:20:56 +00:00
if mode == ChunkCacheAuto {
record , decision , err := input . ChunkPlans . Load ( doc . Digest )
if err != nil {
return result , fmt . Errorf ( "load chunk plan: %w" , err )
}
2026-11-21 23:48:51 +00:00
result . lookup = decision
result . summary . LookupStatus , result . summary . LookupReason = chunkPlanLookupStatus ( decision . Status ) , chunkPlanLookupReason ( decision . Status )
if decision . Status == ChunkPlanHit {
plan , chunks , err := validateAndMaterializeChunkPlan ( doc , record . Plan )
if err == nil {
cachedAssessment , err := assessChunks ( ctx , chunks )
2026-08-27 00:35:37 +00:00
if err != nil {
return result , err
2026-07-17 23:16:16 -05:00
}
2026-11-21 23:48:51 +00:00
if cachedAssessment . Accepted ( ) {
2026-08-27 00:35:37 +00:00
if err := result . setCandidate ( record , "reused" ) ; err != nil {
2026-11-21 23:48:51 +00:00
return result , err
2026-08-27 15:40:45 +00:00
}
2026-11-21 23:48:51 +00:00
result . plan , result . chunks , result . accepted = & plan , chunks , true
result . outcome = contracts . ProducerOutcome { Disposition : contracts . ProducerAccepted , ReuseEligible : true }
result . diagnostics , err = operationDiagnosticGroups ( record . Diagnostics , cachedAssessment , result . outcome , contracts . DiagnosticOrigin { Stage : contracts . DiagnosticOriginStageChunk , ModuleKey : chunker . Key ( ) } , nil )
result . setValidation ( nil , err )
return result , err
2026-08-27 11:50:37 +00:00
}
2026-07-18 00:20:56 +00:00
}
2026-07-18 01:59:10 +00:00
result . lookup = ChunkPlanDecision { Status : ChunkPlanInvalid , Reason : chunkPlanLookupReason ( ChunkPlanInvalid ) }
2026-11-21 23:48:51 +00:00
result . summary . LookupStatus , result . summary . LookupReason = "invalid" , chunkPlanLookupReason ( ChunkPlanInvalid )
} else if decision . Status != ChunkPlanMissing && decision . Status != ChunkPlanInvalid {
2026-07-18 01:59:10 +00:00
return result , fmt . Errorf ( "load chunk plan returned unsupported decision status" )
2026-07-18 00:20:56 +00:00
}
}
2026-11-21 23:48:51 +00:00
metadata , err := cloneMetadata ( input . Metadata )
if err != nil {
return result , err
2026-07-18 00:45:01 +00:00
}
2026-11-21 23:48:51 +00:00
if err := ctx . Err ( ) ; err != nil {
return result , err
2026-08-27 01:27:25 +00:00
}
2026-11-21 23:48:51 +00:00
produced , err := chunker . Plan ( ctx , contracts . ChunkRequest { Operation : operation , Source : doc , SourceInput : sourceInput . Clone ( ) , SessionID : sessionID , References : CloneReferenceSet ( target . references ) , LLMProfile : input . pipeline . Chunk . LLMProfile , Metadata : metadata } )
if err != nil {
return result , fmt . Errorf ( "chunk source with %q: %w" , chunker . Key ( ) , err )
}
plan , outcome , err := completeProducerOperation ( ctx , input . pipeline . ChunkExecutionClass , operation , produced . Plan , produced . Outcome , produced . Fallback )
if err != nil {
return result , fmt . Errorf ( "accept chunk plan with %q: %w" , chunker . Key ( ) , err )
}
plan , chunks , err := validateAndMaterializeChunkPlan ( doc , plan )
if err != nil {
return result , fmt . Errorf ( "materialize final chunk plan: %w" , err )
}
digest , err := source . DigestChunkPlan ( plan )
2026-07-18 00:20:56 +00:00
if err != nil {
return result , err
}
2026-11-21 23:48:51 +00:00
producerMetadata , _ , err := moduleManifestMetadata ( chunker )
if err != nil {
return result , err
2026-08-27 15:29:10 +00:00
}
2026-11-21 23:48:51 +00:00
profile := ""
if input . pipeline . ChunkExecutionClass == contracts . ExecutionClassLLMBacked {
profile = input . pipeline . Chunk . LLMProfile
2026-07-18 00:20:56 +00:00
}
2026-11-21 23:48:51 +00:00
record := ChunkPlanRecord { SchemaVersion : ChunkPlanSchemaVersion , SourceDigest : doc . Digest , PlanDigest : digest , Plan : source . CloneChunkPlan ( plan ) , CreatedAt : time . Now ( ) . UTC ( ) ,
Producer : ChunkPlanProducer { InputModule : input . Prepared . input . Key ( ) , ChunkModule : chunker . Key ( ) , LLMProfile : profile , References : append ( [ ] artifacts . ReferenceProvenance ( nil ) , referenceTargetProvenance ( input . pipeline . ChunkReferences ) ... ) , Metadata : producerMetadata } ,
2026-08-27 00:35:37 +00:00
}
2026-11-21 23:48:51 +00:00
if err := result . setCandidate ( record , "generated" ) ; err != nil {
return result , err
2026-08-27 00:35:37 +00:00
}
if mode == ChunkCacheRefresh {
2026-11-21 23:48:51 +00:00
result . action , result . summary . Action = "refreshed" , "refreshed"
2026-08-27 00:35:37 +00:00
}
if mode == ChunkCacheBypass {
2026-11-21 23:48:51 +00:00
result . action , result . summary . Action = "bypassed" , "bypassed"
2026-08-27 00:35:37 +00:00
}
2026-11-21 23:48:51 +00:00
result . plan , result . chunks , result . accepted , result . outcome = & plan , chunks , true , outcome
2026-08-27 16:40:17 +00:00
result . setValidation ( nil , nil )
2026-11-21 23:48:51 +00:00
if outcome . Disposition == contracts . ProducerFallback {
2026-08-27 00:35:37 +00:00
result . summary . ValidationStatus = "incomplete"
}
2026-11-21 23:48:51 +00:00
result . diagnostics , err = operationDiagnosticGroups ( produced . Diagnostics , assessment , outcome , contracts . DiagnosticOrigin { Stage : contracts . DiagnosticOriginStageChunk , ModuleKey : chunker . Key ( ) } , nil )
if err != nil {
return result , err
}
if mode == ChunkCacheAuto || mode == ChunkCacheRefresh {
result . summary . PublicationStatus = "not_published"
if outcome . ReuseEligible {
record . Diagnostics = contracts . CloneProducerDiagnostics ( produced . Diagnostics )
if err := input . ChunkPlans . Save ( record ) ; err != nil {
return result , fmt . Errorf ( "save chunk plan: %w" , err )
}
result . summary . PublicationStatus = "published"
2026-07-18 00:20:56 +00:00
}
}
return result , nil
}
2026-07-18 00:45:01 +00:00
2026-07-18 01:59:10 +00:00
func chunkPlanLookupStatus ( status ChunkPlanStatus ) string {
switch status {
case ChunkPlanHit :
return "hit"
case ChunkPlanMissing :
return "missing"
case ChunkPlanInvalid :
return "invalid"
default :
return "skipped"
}
}
func chunkPlanLookupReason ( status ChunkPlanStatus ) string {
switch status {
case ChunkPlanHit :
return "stored chunk plan is valid"
case ChunkPlanMissing :
return "chunk plan not found"
case ChunkPlanInvalid :
return "stored chunk plan is invalid"
default :
return "chunk plan lookup skipped"
}
}
2026-07-17 23:16:16 -05:00
func ( result * chunkPlanExecution ) setCandidate ( record ChunkPlanRecord , action string ) error {
cloned , err := cloneChunkPlanRecord ( record )
if err != nil {
return err
}
2026-07-18 00:45:01 +00:00
result . record = & cloned
result . action = action
result . summary . Action = action
result . summary . SourceDigest = record . SourceDigest
result . summary . CandidateDigest = record . PlanDigest
2026-07-17 23:16:16 -05:00
return nil
2026-07-18 00:45:01 +00:00
}
2026-08-27 16:40:17 +00:00
func ( result * chunkPlanExecution ) setValidation ( rejection * contracts . RejectedOutput , err error ) {
2026-07-18 00:45:01 +00:00
switch {
case err != nil :
result . summary . ValidationStatus = "error"
case rejection != nil :
result . summary . ValidationStatus = "rejected"
default :
result . summary . ValidationStatus = "approved"
}
}
2026-07-17 23:16:16 -05:00
func cloneChunkPlanRecord ( record ChunkPlanRecord ) ( ChunkPlanRecord , error ) {
2026-07-18 00:45:01 +00:00
record . Plan = source . CloneChunkPlan ( record . Plan )
record . Producer . References = append ( [ ] artifacts . ReferenceProvenance ( nil ) , record . Producer . References ... )
2026-07-17 23:16:16 -05:00
metadata , err := cloneMetadata ( record . Producer . Metadata )
if err != nil {
return ChunkPlanRecord { } , fmt . Errorf ( "clone chunk plan producer metadata: %w" , err )
}
record . Producer . Metadata = metadata
2026-08-27 15:40:45 +00:00
record . Diagnostics = contracts . CloneProducerDiagnostics ( record . Diagnostics )
2026-07-17 23:16:16 -05:00
return record , nil
2026-07-18 00:45:01 +00:00
}
2026-07-17 23:16:16 -05:00
func applyChunkPlanExecution ( output * RunOutput , result chunkPlanExecution ) error {
2026-07-18 00:45:01 +00:00
if output == nil {
2026-07-17 23:16:16 -05:00
return nil
2026-07-18 00:45:01 +00:00
}
summary := result . summary
output . ChunkPlan = & summary
if output . Manifest . ChunkPlan == nil {
2026-07-17 23:16:16 -05:00
return nil
2026-07-18 00:45:01 +00:00
}
manifest := output . Manifest . ChunkPlan
manifest . Action = result . action
if result . record == nil {
2026-07-17 23:16:16 -05:00
return nil
2026-07-18 00:45:01 +00:00
}
record := result . record
manifest . SourceDigest = record . SourceDigest
manifest . PlanDigest = record . PlanDigest
manifest . PlanSchemaVersion = record . SchemaVersion
manifest . ProducerInputModule = record . Producer . InputModule
manifest . ProducerModule = record . Producer . ChunkModule
manifest . ProducerLLMProfile = record . Producer . LLMProfile
manifest . ProducerReferences = append ( [ ] artifacts . ReferenceProvenance ( nil ) , record . Producer . References ... )
2026-07-17 23:16:16 -05:00
metadata , err := cloneMetadata ( record . Producer . Metadata )
if err != nil {
return fmt . Errorf ( "clone chunk plan manifest producer metadata: %w" , err )
}
manifest . ProducerMetadata = metadata
2026-07-18 00:45:01 +00:00
createdAt := record . CreatedAt
manifest . CreatedAt = & createdAt
2026-07-17 23:16:16 -05:00
return nil
2026-07-18 00:45:01 +00:00
}