Finish native migration cleanup and restore whole-tree compilation

This commit is contained in:
2026-11-22 05:37:15 +00:00
parent 4fa437aee8
commit 3ef55f4668
12 changed files with 123 additions and 445 deletions

View File

@@ -5,7 +5,6 @@ package registry
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
@@ -26,12 +25,11 @@ const (
// Registry is an immutable, validated location registry prepared for prompt
// grounding. All accessors return defensive copies.
type Registry struct {
bound bool
list dnd.LocationRegistry
canonical []byte
digest string
identityDigest string
lookupByID map[string]int
bound bool
list dnd.LocationRegistry
canonical []byte
digest string
lookupByID map[string]int
}
// Resolver selects and memoizes immutable location registry views.
@@ -99,12 +97,10 @@ func locationReferenceSpec() registryresolver.ReferenceSpec {
func emptyRegistry() *Registry {
content := []byte(emptyRegistryContent)
identityDigest := semanticDigest(content)
return &Registry{
list: dnd.LocationRegistry{Locations: []dnd.Location{}},
canonical: append([]byte(nil), content...),
identityDigest: identityDigest,
lookupByID: map[string]int{},
list: dnd.LocationRegistry{Locations: []dnd.Location{}},
canonical: append([]byte(nil), content...),
lookupByID: map[string]int{},
}
}
@@ -127,19 +123,13 @@ func loadRegistry(referenceContent []byte) (*Registry, error) {
for index, location := range list.Locations {
lookupByID[location.ID] = index
}
projection, err := identityProjection(list)
if err != nil {
return nil, fmt.Errorf("encode location prompt projection: %w", err)
}
digest := semanticDigest(content)
identityDigest := semanticDigest(projection)
return &Registry{
bound: true,
list: list,
canonical: append([]byte(nil), content...),
digest: digest,
identityDigest: identityDigest,
lookupByID: lookupByID,
bound: true,
list: list,
canonical: append([]byte(nil), content...),
digest: digest,
lookupByID: lookupByID,
}, nil
}
@@ -179,15 +169,6 @@ func (r *Registry) Digest() string {
return r.digest
}
// IdentityDigest returns the digest of the ordered ID/name identity projection
// used by deterministic consumers.
func (r *Registry) IdentityDigest() string {
if r == nil {
return ""
}
return r.identityDigest
}
// Count returns the number of validated location records.
func (r *Registry) Count() int {
if r == nil {
@@ -219,23 +200,6 @@ func semanticDigest(content []byte) string {
return "sha256:" + hex.EncodeToString(sum[:])
}
type identityProjectedLocation struct {
ID string `json:"id"`
Name string `json:"name"`
}
type identityProjectedLocationRegistry struct {
Locations []identityProjectedLocation `json:"locations"`
}
func identityProjection(list dnd.LocationRegistry) ([]byte, error) {
projection := identityProjectedLocationRegistry{Locations: make([]identityProjectedLocation, len(list.Locations))}
for index, location := range list.Locations {
projection.Locations[index] = identityProjectedLocation{ID: location.ID, Name: location.Name}
}
return json.Marshal(projection)
}
func formatIdentityIssues(issues []identity.Issue) string {
parts := make([]string, len(issues))
for index, issue := range issues {

View File

@@ -22,18 +22,19 @@ func TestResolveUnboundRegistryHasEmptyIdentity(t *testing.T) {
if registry.Bound() || registry.Digest() != "" || registry.Count() != 0 || string(registry.CanonicalBytes()) != emptyRegistryContent {
t.Fatalf("unbound registry = %#v", registry)
}
if registry.IdentityDigest() == "" {
t.Fatal("unbound registry identity digest is empty")
}
}
func TestResolveRetainsSeparateDurableAndIdentityDigests(t *testing.T) {
func TestResolveRetainsCanonicalDurableRegistry(t *testing.T) {
registry := resolveList(t, registryFixture())
if !registry.Bound() || registry.Count() != 2 || registry.Digest() == "" {
t.Fatalf("registry identity = bound %t count %d digest %q", registry.Bound(), registry.Count(), registry.Digest())
}
if registry.IdentityDigest() == "" || registry.Digest() == registry.IdentityDigest() {
t.Fatalf("full/identity digests = %q/%q", registry.Digest(), registry.IdentityDigest())
content, err := locationcodec.New().Encode(registryFixture())
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(content, registry.CanonicalBytes()) {
t.Fatalf("canonical registry=%s, want %s", registry.CanonicalBytes(), content)
}
}
@@ -228,8 +229,8 @@ func TestGroundingProjectsAndResolvesUniqueAndSameNameLocations(t *testing.T) {
if bytes.Contains(grounding.PromptInput().Content, []byte("location:sha256:")) || bytes.Contains(grounding.PromptInput().Content, []byte(`"source_id"`)) {
t.Fatalf("grounding projection exposed durable identity: %s", grounding.PromptInput().Content)
}
if grounding.PromptInput().Digest == "" || grounding.PromptInput().Digest == registry.IdentityDigest() {
t.Fatalf("grounding/identity digests = %q/%q", grounding.PromptInput().Digest, registry.IdentityDigest())
if grounding.PromptInput().Digest == "" {
t.Fatal("grounding projection has no content digest")
}
resolved, ok := grounding.Resolve(Selector{Name: " the tavern ", RegistryRefs: []RegistryRef{{StartUnitID: 30, EndUnitID: 30}, {StartUnitID: 20, EndUnitID: 20}}})
@@ -262,7 +263,7 @@ func TestGroundingProjectsAndResolvesUniqueAndSameNameLocations(t *testing.T) {
}
}
func TestGroundingHandlesEmptyRegistriesAndIdentityFingerprints(t *testing.T) {
func TestGroundingHandlesEmptyRegistriesAndStableUniqueNameProjection(t *testing.T) {
doc := groundingDocument()
empty := resolveList(t, dnd.LocationRegistry{Locations: []dnd.Location{}})
grounding, err := NewGrounding(empty, doc)
@@ -288,8 +289,8 @@ func TestGroundingHandlesEmptyRegistriesAndIdentityFingerprints(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if base.IdentityDigest() != expanded.IdentityDigest() || baseGrounding.PromptInput().Digest != expandedGrounding.PromptInput().Digest {
t.Fatalf("identity/model fingerprints = %q/%q and %q/%q", base.IdentityDigest(), expanded.IdentityDigest(), baseGrounding.PromptInput().Digest, expandedGrounding.PromptInput().Digest)
if base.Locations()[0].ID != expanded.Locations()[0].ID || !bytes.Equal(baseGrounding.PromptInput().Content, expandedGrounding.PromptInput().Content) {
t.Fatalf("additional evidence changed unique location identity or projection: %#v %#v", base.Locations(), expanded.Locations())
}
}

View File

@@ -31,7 +31,6 @@ type Registry struct {
canonical []byte
digest string
projectionDigest string
identityDigest string
promptInput contracts.LLMInputMaterial
lookupByKey map[string]int
lookupByID map[string]int
@@ -110,7 +109,6 @@ func emptyRegistry() *Registry {
list: dnd.NPCRegistry{NPCs: []dnd.NPC{}},
canonical: append([]byte(nil), content...),
projectionDigest: projectionDigest,
identityDigest: projectionDigest,
promptInput: contracts.NewLLMInputMaterial(ReferenceSlot, npccodec.MediaType, content, projectionDigest, ""),
lookupByKey: map[string]int{},
lookupByID: map[string]int{},
@@ -144,18 +142,12 @@ func loadRegistry(referenceContent []byte) (*Registry, error) {
return nil, fmt.Errorf("encode NPC name projection: %w", err)
}
projectionDigest := semanticDigest(projection)
identityProjection, err := identityProjection(list)
if err != nil {
return nil, fmt.Errorf("encode NPC identity projection: %w", err)
}
identityProjectionDigest := semanticDigest(identityProjection)
return &Registry{
bound: true,
list: list,
canonical: append([]byte(nil), content...),
digest: digest,
projectionDigest: projectionDigest,
identityDigest: identityProjectionDigest,
promptInput: contracts.NewLLMInputMaterial(ReferenceSlot, npccodec.MediaType, projection, projectionDigest, ""),
lookupByKey: lookupByKey,
lookupByID: lookupByID,
@@ -207,15 +199,6 @@ func (r *Registry) ProjectionDigest() string {
return r.projectionDigest
}
// IdentityDigest returns the SHA-256 digest of the ordered ID/name identity
// projection. It binds durable identities without exposing them to the model.
func (r *Registry) IdentityDigest() string {
if r == nil {
return ""
}
return r.identityDigest
}
// Count returns the number of validated NPC records.
func (r *Registry) Count() int {
if r == nil {
@@ -267,19 +250,10 @@ type projectedNPC struct {
Name string `json:"name"`
}
type identityProjectedNPC struct {
ID string `json:"id"`
Name string `json:"name"`
}
type projectedNPCRegistry struct {
NPCs []projectedNPC `json:"npcs"`
}
type identityProjectedNPCRegistry struct {
NPCs []identityProjectedNPC `json:"npcs"`
}
func nameProjection(list dnd.NPCRegistry) ([]byte, error) {
projection := projectedNPCRegistry{NPCs: make([]projectedNPC, len(list.NPCs))}
for index, npc := range list.NPCs {
@@ -288,14 +262,6 @@ func nameProjection(list dnd.NPCRegistry) ([]byte, error) {
return json.Marshal(projection)
}
func identityProjection(list dnd.NPCRegistry) ([]byte, error) {
projection := identityProjectedNPCRegistry{NPCs: make([]identityProjectedNPC, len(list.NPCs))}
for index, npc := range list.NPCs {
projection.NPCs[index] = identityProjectedNPC{ID: npc.ID, Name: npc.Name}
}
return json.Marshal(projection)
}
func formatIdentityIssues(issues []identity.Issue) string {
parts := make([]string, len(issues))
for index, issue := range issues {

View File

@@ -19,7 +19,7 @@ func TestResolveUnboundRegistryHasExactEmptyProjection(t *testing.T) {
t.Fatalf("Resolve() error = %v", err)
}
input := registry.PromptInput()
if registry.Bound() || registry.Digest() != "" || registry.Count() != 0 || string(input.Content) != emptyPrompt || registry.IdentityDigest() != registry.ProjectionDigest() {
if registry.Bound() || registry.Digest() != "" || registry.Count() != 0 || string(input.Content) != emptyPrompt {
t.Fatalf("registry = %#v input = %#v, want unbound empty registry", registry, input)
}
if registry.ProjectionDigest() == "" || input.Digest != registry.ProjectionDigest() || input.OriginURI != "" {
@@ -46,16 +46,8 @@ func TestResolveKeepsDurableProvenanceAndProjectsOnlyOrderedNames(t *testing.T)
}
}
func TestIdentityDigestTracksOrderedIDsAndNames(t *testing.T) {
func TestRegistryLookupByID(t *testing.T) {
registry := resolveList(t, registryFixture())
projection, err := identityProjection(registryFixture())
if err != nil {
t.Fatal(err)
}
want := `{"npcs":[{"id":"` + identity.DeriveID("Mira Thorn") + `","name":"Mira Thorn"},{"id":"` + identity.DeriveID("Captain Vale") + `","name":"Captain Vale"}]}`
if string(projection) != want || registry.IdentityDigest() != semanticDigest(projection) || registry.IdentityDigest() == registry.ProjectionDigest() {
t.Fatalf("identity digest = %q, want digest for %s", registry.IdentityDigest(), want)
}
if npc, ok := registry.LookupID(identity.DeriveID("Mira Thorn")); !ok || npc.Name != "Mira Thorn" {
t.Fatalf("LookupID() = %#v, %t", npc, ok)
}
@@ -228,7 +220,7 @@ func TestProjectionIsStableForEquivalentNormalizedRegistries(t *testing.T) {
secondList := registryFixture()
secondList.NPCs[0].SourceRefs = append(secondList.NPCs[0].SourceRefs, source.SourceRef{SourceID: "session-beta", StartUnitID: 8, EndUnitID: 8})
second := resolveList(t, secondList)
if !reflect.DeepEqual(first.PromptInput().Content, second.PromptInput().Content) || first.ProjectionDigest() != second.ProjectionDigest() || first.IdentityDigest() != second.IdentityDigest() || first.Digest() == second.Digest() {
if !reflect.DeepEqual(first.PromptInput().Content, second.PromptInput().Content) || first.ProjectionDigest() != second.ProjectionDigest() || first.Digest() == second.Digest() {
t.Fatalf("projection/full identity mismatch: %#v %#v", first, second)
}
}