69 lines
2.4 KiB
Go
69 lines
2.4 KiB
Go
package pipeline
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"strings"
|
||
"unicode/utf8"
|
||
|
||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||
)
|
||
|
||
func artifactProducerCache(input RunInput, typed *preparedTypedLane, class contracts.ExecutionClass, origin contracts.DiagnosticOrigin, doc *source.SourceDocument, chunk *source.Chunk, warnings *[]contracts.ProducerDiagnostic) *contracts.ProducerCache[any] {
|
||
if input.Cache == nil || class != contracts.ExecutionClassLLMBacked {
|
||
return nil
|
||
}
|
||
index := source.NewDocumentIndex(doc)
|
||
return &contracts.ProducerCache[any]{
|
||
Store: input.Cache,
|
||
Encode: typed.codec.encode,
|
||
Decode: func(content []byte) (any, error) {
|
||
value, err := typed.codec.decode(content)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if typed.cacheEvidence != nil {
|
||
refs, err := typed.cacheEvidence(value)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
for _, ref := range refs {
|
||
if err := index.ValidateRef(ref); err != nil {
|
||
return nil, fmt.Errorf("cached artifact source reference: %w", err)
|
||
}
|
||
}
|
||
}
|
||
return value, nil
|
||
},
|
||
Observe: producerCacheObserver(input, origin, chunk, warnings),
|
||
}
|
||
}
|
||
|
||
func producerCacheObserver(input RunInput, origin contracts.DiagnosticOrigin, chunk *source.Chunk, warnings *[]contracts.ProducerDiagnostic) func(context.Context, contracts.CacheObservation) error {
|
||
return func(ctx context.Context, observation contracts.CacheObservation) error {
|
||
if chunk != nil {
|
||
chunkIndex := chunk.Index
|
||
observation.ChunkID, observation.ChunkIndex = chunk.ID, &chunkIndex
|
||
}
|
||
if observation.Err != nil {
|
||
message := strings.ToValidUTF8(fmt.Sprintf("Artifact cache %s: %v", observation.Decision, observation.Err), "<22>")
|
||
if len(message) > contracts.MaxDiagnosticMessageBytes {
|
||
message = message[:contracts.MaxDiagnosticMessageBytes]
|
||
for !utf8.ValidString(message) {
|
||
message = message[:len(message)-1]
|
||
}
|
||
}
|
||
*warnings = append(*warnings, contracts.ProducerDiagnostic{
|
||
Disposition: contracts.DiagnosticDispositionWarning, Category: contracts.DiagnosticCategoryDegradation,
|
||
ReasonCode: "cache_" + observation.Decision, OccurrenceCount: 1,
|
||
Samples: []contracts.DiagnosticSample{{Scope: origin.ModuleKey, Message: message}},
|
||
})
|
||
}
|
||
if input.ObserveCache != nil {
|
||
return input.ObserveCache(ctx, origin, observation)
|
||
}
|
||
return nil
|
||
}
|
||
}
|