Keep cached scene identities aligned with the current plan

This commit is contained in:
2026-11-22 09:37:16 +00:00
parent 8ea96dc7a4
commit 9074547e33
4 changed files with 223 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
package scenedescriptions
import (
"context"
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/framework/artifactcache"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
scenecodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/scenedescriptions"
)
func TestSceneCacheAdaptsOnlyUsableCurrentRange(t *testing.T) {
for _, name := range []string{"stale identity", "wrong range", "multiple scenes", "untrimmed title"} {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
request := extractionRequest()
store, err := artifactcache.NewFilesystemStore(t.TempDir())
if err != nil {
t.Fatal(err)
}
codec := scenecodec.New()
var key string
request.Operation.Controls.Cache = contracts.CacheAuto
request.Operation.Cache = &contracts.ProducerCache[dnd.SceneDescriptionList]{Store: store, Encode: codec.Encode, Decode: codec.Decode,
Observe: func(_ context.Context, observation contracts.CacheObservation) error {
key = observation.MessageContentHash
return nil
},
}
client := &fakeSceneDescriptionsLLMClient{raw: narrativeScene}
extractor := newExtractor(t, client)
first, err := extractor.Extract(ctx, request)
if err != nil {
t.Fatal(err)
}
cached := first.Value
cached.Scenes[0].ID = "old-chunk-identity"
switch name {
case "wrong range":
cached.Scenes[0].SourceRef.EndUnitID = 2
case "multiple scenes":
other := cached.Scenes[0]
other.ID = "another-old-scene"
cached.Scenes = append(cached.Scenes, other)
case "untrimmed title":
cached.Scenes[0].Title = " Untrimmed "
}
content, err := codec.Encode(cached)
if err != nil {
t.Fatal(err)
}
if err := store.Store(ctx, contracts.CacheRefresh, key, content); err != nil {
t.Fatal(err)
}
assessments := 0
request.Operation.Assess = func(context.Context, dnd.SceneDescriptionList) (contracts.CandidateAssessment, error) {
assessments++
return contracts.CandidateAssessment{}, nil
}
result, err := extractor.Extract(ctx, request)
if err != nil {
t.Fatal(err)
}
if len(result.Value.Scenes) != 1 || result.Value.Scenes[0].ID != request.Chunk.ID || result.Value.Scenes[0].SourceRef != request.Chunk.Ref {
t.Fatalf("scene does not match current chunk: %#v", result.Value)
}
wantCalls, wantAssessments := 2, 1
if name == "stale identity" {
wantCalls, wantAssessments = 1, 0
}
if len(client.requests) != wantCalls || assessments != wantAssessments {
t.Fatalf("model calls=%d assessments=%d, want %d/%d", len(client.requests), assessments, wantCalls, wantAssessments)
}
})
}
}

View File

@@ -79,6 +79,24 @@ func (e *Extractor) Extract(ctx context.Context, req contracts.TypedExtractionRe
chunk := source.Chunk{ID: req.Chunk.ID, Ref: req.Chunk.Ref}
index := source.NewDocumentIndex(req.Source)
if cache := req.Operation.Cache; cache != nil && cache.Decode != nil {
owned := *cache
decode := cache.Decode
owned.Decode = func(content []byte) (dnd.SceneDescriptionList, error) {
value, err := decode(content)
if err != nil {
return value, err
}
if len(value.Scenes) != 1 || value.Scenes[0].SourceRef != chunk.Ref {
return dnd.SceneDescriptionList{}, fmt.Errorf("cached scene does not describe the current chunk range")
}
// Chunk ordinals can change without changing the semantic prompt.
value.Scenes = append([]dnd.SceneDescription(nil), value.Scenes...)
value.Scenes[0].ID = chunk.ID
return sceneFallback(&value, chunk, index)
}
req.Operation.Cache = &owned
}
materials := shared.PromptInputs(sourceInput, req.References)
inputs := make(map[string]promptkit.ArtifactRef, len(materials))
for name, material := range materials {