Migrate location registry to shared resolver
This commit is contained in:
@@ -7,9 +7,6 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"mime"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
@@ -17,6 +14,7 @@ import (
|
||||
locationcodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/locations"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/locations/identity"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/diagnostics"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared/registryresolver"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -37,38 +35,20 @@ type Registry struct {
|
||||
lookupByID map[string]int
|
||||
}
|
||||
|
||||
// Resolver retains the construction-time registry and memoizes immutable
|
||||
// operation-time registries. It never retains caller-owned reference bytes.
|
||||
// Resolver selects and memoizes immutable location registry views.
|
||||
type Resolver struct {
|
||||
seeded *Registry
|
||||
|
||||
mu sync.Mutex
|
||||
cache map[string]*Registry
|
||||
rawCache map[string]*Registry
|
||||
resolver *registryresolver.Resolver[*Registry]
|
||||
}
|
||||
|
||||
// NewResolver validates a materialized construction-time location reference.
|
||||
// An empty slot is permitted because a generated reference is supplied only at
|
||||
// operation time.
|
||||
func NewResolver(references contracts.ReferenceSet) (*Resolver, error) {
|
||||
seeded, err := Resolve(constructionReferences(references))
|
||||
resolver, err := registryresolver.New(registryResolverConfig(), references)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Resolver{seeded: seeded, cache: make(map[string]*Registry), rawCache: make(map[string]*Registry)}, nil
|
||||
}
|
||||
|
||||
func constructionReferences(references contracts.ReferenceSet) contracts.ReferenceSet {
|
||||
slot, ok := references.Slots[ReferenceSlot]
|
||||
if !ok || len(slot.Items) > 0 {
|
||||
return references
|
||||
}
|
||||
cloned := contracts.ReferenceSet{Slots: make(map[string]contracts.ResolvedReferenceSlot, len(references.Slots))}
|
||||
for name, value := range references.Slots {
|
||||
cloned.Slots[name] = value
|
||||
}
|
||||
delete(cloned.Slots, ReferenceSlot)
|
||||
return cloned
|
||||
return &Resolver{resolver: resolver}, nil
|
||||
}
|
||||
|
||||
// Seeded returns the validated construction-time registry.
|
||||
@@ -76,95 +56,63 @@ func (r *Resolver) Seeded() *Registry {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
return r.seeded
|
||||
return r.resolver.Seeded()
|
||||
}
|
||||
|
||||
// Resolve returns the generated operation-time registry when the locations
|
||||
// slot is present, otherwise it returns the construction-time registry.
|
||||
func (r *Resolver) Resolve(references contracts.ReferenceSet) (*Registry, error) {
|
||||
if r == nil {
|
||||
if r == nil || r.resolver == nil {
|
||||
return Resolve(references)
|
||||
}
|
||||
if _, ok := references.Slots[ReferenceSlot]; !ok {
|
||||
return r.seeded, nil
|
||||
}
|
||||
|
||||
slot := references.Slots[ReferenceSlot]
|
||||
rawKey := ""
|
||||
if len(slot.Items) == 1 {
|
||||
rawKey = strings.ToLower(strings.TrimSpace(slot.Items[0].MediaType)) + "\x00" + semanticDigest(slot.Items[0].Content)
|
||||
}
|
||||
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if rawKey != "" {
|
||||
if cached, ok := r.rawCache[rawKey]; ok {
|
||||
return cached, nil
|
||||
}
|
||||
}
|
||||
resolved, err := Resolve(references)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if sameRegistryIdentity(r.seeded, resolved) {
|
||||
if rawKey != "" {
|
||||
r.rawCache[rawKey] = r.seeded
|
||||
}
|
||||
return r.seeded, nil
|
||||
}
|
||||
if cached, ok := r.cache[resolved.Digest()]; ok {
|
||||
if rawKey != "" {
|
||||
r.rawCache[rawKey] = cached
|
||||
}
|
||||
return cached, nil
|
||||
}
|
||||
r.cache[resolved.Digest()] = resolved
|
||||
if rawKey != "" {
|
||||
r.rawCache[rawKey] = resolved
|
||||
}
|
||||
return resolved, nil
|
||||
}
|
||||
|
||||
func sameRegistryIdentity(first, second *Registry) bool {
|
||||
if first == nil || second == nil {
|
||||
return first == second
|
||||
}
|
||||
return first.bound == second.bound && first.digest == second.digest
|
||||
return r.resolver.Resolve(references)
|
||||
}
|
||||
|
||||
// Resolve validates an optional location-list reference. An absent reference
|
||||
// uses the canonical empty projection and has no durable registry identity.
|
||||
func Resolve(references contracts.ReferenceSet) (*Registry, error) {
|
||||
slot, ok := references.Slots[ReferenceSlot]
|
||||
if !ok {
|
||||
content := []byte(emptyPrompt)
|
||||
projectionDigest := semanticDigest(content)
|
||||
return &Registry{
|
||||
list: dnd.LocationList{Locations: []dnd.Location{}},
|
||||
canonical: append([]byte(nil), content...),
|
||||
projectionDigest: projectionDigest,
|
||||
promptInput: contracts.NewLLMInputMaterial(ReferenceSlot, locationcodec.MediaType, content, projectionDigest, ""),
|
||||
lookupByID: map[string]int{},
|
||||
}, nil
|
||||
}
|
||||
if len(slot.Items) != 1 {
|
||||
return nil, fmt.Errorf("reference slot %q must contain exactly one item", ReferenceSlot)
|
||||
}
|
||||
|
||||
item := slot.Items[0]
|
||||
mediaType, _, err := mime.ParseMediaType(item.MediaType)
|
||||
item, present, err := registryresolver.ResolveOptionalSingleItem(references, locationReferenceSpec())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reference slot %q item media type is invalid", ReferenceSlot)
|
||||
return nil, err
|
||||
}
|
||||
if !strings.EqualFold(mediaType, locationcodec.MediaType) {
|
||||
return nil, fmt.Errorf("reference slot %q item media type must be %s", ReferenceSlot, locationcodec.MediaType)
|
||||
}
|
||||
if len(item.Content) > MaxBytes {
|
||||
return nil, fmt.Errorf("reference slot %q item is %d bytes, limit %d", ReferenceSlot, len(item.Content), MaxBytes)
|
||||
if !present {
|
||||
return emptyRegistry(), nil
|
||||
}
|
||||
return loadRegistry(item.Content)
|
||||
}
|
||||
|
||||
func registryResolverConfig() registryresolver.Config[*Registry] {
|
||||
return registryresolver.Config[*Registry]{
|
||||
Reference: locationReferenceSpec(),
|
||||
Absent: func() (*Registry, error) {
|
||||
return emptyRegistry(), nil
|
||||
},
|
||||
Load: loadRegistry,
|
||||
SemanticIdentity: func(registry *Registry) string {
|
||||
return registry.Digest()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func locationReferenceSpec() registryresolver.ReferenceSpec {
|
||||
return registryresolver.ReferenceSpec{SlotName: ReferenceSlot, AcceptedMediaType: locationcodec.MediaType, MaxBytes: MaxBytes}
|
||||
}
|
||||
|
||||
func emptyRegistry() *Registry {
|
||||
content := []byte(emptyPrompt)
|
||||
projectionDigest := semanticDigest(content)
|
||||
return &Registry{
|
||||
list: dnd.LocationList{Locations: []dnd.Location{}},
|
||||
canonical: append([]byte(nil), content...),
|
||||
projectionDigest: projectionDigest,
|
||||
promptInput: contracts.NewLLMInputMaterial(ReferenceSlot, locationcodec.MediaType, content, projectionDigest, ""),
|
||||
lookupByID: map[string]int{},
|
||||
}
|
||||
}
|
||||
|
||||
func loadRegistry(referenceContent []byte) (*Registry, error) {
|
||||
codec := locationcodec.New()
|
||||
value, err := codec.Decode(item.Content)
|
||||
value, err := codec.Decode(referenceContent)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode location registry: invalid approved location JSON")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user