Finish native migration cleanup and restore whole-tree compilation
This commit is contained in:
@@ -2,9 +2,7 @@ package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -162,25 +160,3 @@ func captureOverrideValue(value any) any {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
func writeConfigLoadError(output io.Writer, err error) {
|
||||
var loadError *configkit.LoadError
|
||||
if errors.As(err, &loadError) {
|
||||
if writeErr := configkit.WriteDiagnostics(output, loadError.Diagnostics()); writeErr == nil {
|
||||
// ConfigKit's locations contain source IDs. Keep its renderer and
|
||||
// supply the acquired-source legend so imported/overlay errors can
|
||||
// be traced to a file. Quoting also escapes terminal controls.
|
||||
for _, source := range loadError.Sources() {
|
||||
label := source.Path
|
||||
if label == "" {
|
||||
label = source.Label
|
||||
}
|
||||
if _, err := fmt.Fprintf(output, "source %q: %q\n", source.ID, label); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(output, "notarius: %v\n", err)
|
||||
}
|
||||
|
||||
@@ -137,19 +137,14 @@ func TestNativeDiscoverySelectionAndTerminalFailures(t *testing.T) {
|
||||
assertSelection(workingYAML, "yaml")
|
||||
}
|
||||
|
||||
func TestWriteConfigLoadErrorUsesSafeDiagnostics(t *testing.T) {
|
||||
func TestUnsupportedConfigurationUsesNativeDiagnostics(t *testing.T) {
|
||||
t.Parallel()
|
||||
path := filepath.Join(t.TempDir(), "invalid.yml")
|
||||
writeConfigKitFile(t, path, "version: 5\n")
|
||||
loaders := &nativeLoaders{lookupEnv: emptyLookup}
|
||||
_, err := loaders.application(context.Background(), productionSelection(path))
|
||||
if err == nil {
|
||||
t.Fatal("unsupported version loaded")
|
||||
}
|
||||
var output bytes.Buffer
|
||||
writeConfigLoadError(&output, err)
|
||||
if !strings.Contains(output.String(), "version") || !strings.Contains(output.String(), path) {
|
||||
t.Fatalf("diagnostics=%q", output.String())
|
||||
var stdout, stderr bytes.Buffer
|
||||
code := RunWithOptions([]string{"pipeline", "list", "--config", path}, &stdout, &stderr, Options{})
|
||||
if code != 2 || !strings.Contains(stderr.String(), "version") || !strings.Contains(stderr.String(), path) {
|
||||
t.Fatalf("status=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,9 +10,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/artifacts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/debugbundle"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
@@ -30,8 +28,8 @@ func TestMaintainedExamplesLoadResolveAndList(t *testing.T) {
|
||||
for _, example := range maintainedExampleFiles(t) {
|
||||
t.Run(example.name, func(t *testing.T) {
|
||||
cfg := loadMaintainedExample(t, example.path)
|
||||
if example.name == "complete" && (cfg.Concurrency.TotalLLM != 2 || cfg.Concurrency.StageWorkers["extract"] != 2) {
|
||||
t.Fatalf("complete example concurrency = %#v, want explicit limits of 2", cfg.Concurrency)
|
||||
if example.name == "complete" && cfg.Concurrency.Workers != 2 {
|
||||
t.Fatalf("complete example concurrency = %#v, want an explicit shared worker limit of 2", cfg.Concurrency)
|
||||
}
|
||||
raw, err := os.ReadFile(example.transcriptPath)
|
||||
if err != nil {
|
||||
@@ -71,40 +69,17 @@ func TestMaintainedExamplesLoadResolveAndList(t *testing.T) {
|
||||
if got, want := strings.Join(generatedReferenceBindingDescriptions(materialized), "|"), strings.Join(generatedReferenceBindingDescriptions(parent), "|"); got != want {
|
||||
t.Fatalf("development generated reference bindings = %q, want parent bindings %q", got, want)
|
||||
}
|
||||
eligibleBindings := 0
|
||||
for _, step := range materialized.Steps {
|
||||
for _, lane := range step.ArtifactLanes {
|
||||
for _, binding := range []struct {
|
||||
module pipeline.ModuleBinding
|
||||
executionClass contracts.ExecutionClass
|
||||
}{
|
||||
{module: lane.Extract, executionClass: lane.ExtractExecutionClass},
|
||||
{module: lane.Normalize, executionClass: lane.NormalizeExecutionClass},
|
||||
} {
|
||||
if binding.executionClass != "llm_backed" {
|
||||
continue
|
||||
}
|
||||
eligibleBindings++
|
||||
if got := binding.module.StructuredOutputRepairAttempts; got == nil || *got != 0 {
|
||||
t.Fatalf("development %s repair attempts = %v, want explicit zero", binding.module.Module, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if eligibleBindings == 0 {
|
||||
t.Fatal("development variant had no eligible LLM bindings for repair policy")
|
||||
}
|
||||
}
|
||||
if got := exampleStepLaneIDs(materialized); strings.Join(got, "|") != "describe-session:item-registry,location-registry,npc-registry,scene-descriptions|extract-events:combat-turns,item-occurrences,location-occurrences,npc-occurrences,spells|track-enemies:enemy-events" {
|
||||
t.Fatalf("complete example steps and lanes = %v, want the documented D&D extractor composition", got)
|
||||
}
|
||||
completePipelines[pipelineID] = materialized
|
||||
locationLane := referenceContractLane(t, materialized, "location-registry")
|
||||
if locationLane.ArtifactKind != dnd.LocationRegistryKind || locationLane.Extract.Module != locationextract.Key || locationLane.Extract.Retries != 2 || locationLane.Merge.Module != pipeline.DefaultMergeModule || locationLane.Normalize.Module != locationnormalize.Key || locationLane.Normalize.Retries != 2 {
|
||||
if locationLane.ArtifactKind != dnd.LocationRegistryKind || locationLane.Extract.Module != locationextract.Key || locationLane.Merge.Module != pipeline.DefaultMergeModule || locationLane.Normalize.Module != locationnormalize.Key {
|
||||
t.Fatalf("location lane = %#v, want typed registry composition", locationLane)
|
||||
}
|
||||
occurrenceLane := referenceContractLane(t, materialized, "location-occurrences")
|
||||
if occurrenceLane.ArtifactKind != dnd.LocationOccurrenceListKind || occurrenceLane.Extract.Module != locationoccurrenceextract.Key || occurrenceLane.Extract.Retries != 2 || occurrenceLane.Merge.Module != pipeline.DefaultMergeModule || occurrenceLane.Normalize.Module != locationoccurrencenormalize.Key {
|
||||
if occurrenceLane.ArtifactKind != dnd.LocationOccurrenceListKind || occurrenceLane.Extract.Module != locationoccurrenceextract.Key || occurrenceLane.Merge.Module != pipeline.DefaultMergeModule || occurrenceLane.Normalize.Module != locationoccurrencenormalize.Key {
|
||||
t.Fatalf("location occurrence lane = %#v, want typed occurrence composition", occurrenceLane)
|
||||
}
|
||||
for _, target := range []pipeline.ResolvedReferenceTarget{occurrenceLane.ExtractReferences, occurrenceLane.NormalizeReferences} {
|
||||
@@ -150,8 +125,12 @@ func TestMaintainedExamplesLoadResolveAndList(t *testing.T) {
|
||||
}
|
||||
}
|
||||
var stdout, stderr strings.Builder
|
||||
code := RunWithOptions([]string{"pipelines", "list", "--config", example.path}, &stdout, &stderr, productionOptionsFromComponents(components))
|
||||
if code != 0 || stdout.String() != strings.Join(example.pipelineIDs, "\n")+"\n" || stderr.Len() != 0 {
|
||||
code := RunWithOptions([]string{"pipeline", "list", "--config", example.path, "--format", "json"}, &stdout, &stderr, productionOptionsFromComponents(components))
|
||||
var ids []string
|
||||
if err := json.Unmarshal([]byte(stdout.String()), &ids); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if code != 0 || strings.Join(ids, ",") != strings.Join(example.pipelineIDs, ",") || stderr.Len() != 0 {
|
||||
t.Fatalf("pipelines list: code=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
|
||||
}
|
||||
})
|
||||
@@ -206,9 +185,9 @@ func TestMaintainedExamplesValidateEffectiveProfilesOffline(t *testing.T) {
|
||||
t.Run(pipelineID, func(t *testing.T) {
|
||||
var stdout, stderr strings.Builder
|
||||
code := RunWithOptions([]string{
|
||||
"config", "validate", "--config", example.path, "--pipeline", pipelineID,
|
||||
"pipeline", "validate", pipelineID, "--config", example.path,
|
||||
}, &stdout, &stderr, Options{})
|
||||
if code != 0 || stderr.Len() != 0 || !strings.Contains(stdout.String(), `valid for pipeline "`+pipelineID+`"`) {
|
||||
if code != 0 || stderr.Len() != 0 || !strings.Contains(stdout.String(), pipelineID) {
|
||||
t.Fatalf("code=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
|
||||
}
|
||||
})
|
||||
@@ -218,21 +197,11 @@ func TestMaintainedExamplesValidateEffectiveProfilesOffline(t *testing.T) {
|
||||
|
||||
var stdout, stderr strings.Builder
|
||||
code := RunWithOptions([]string{
|
||||
"config", "profile", "--config", repositoryPath("examples", "dnd-complete.config.yml"), "dnd-extraction-development",
|
||||
"promptkit", "show", "profile", "notarius/dnd-extraction-development",
|
||||
"--config", repositoryPath("examples", "dnd-complete.config.yml"), "--format", "json",
|
||||
}, &stdout, &stderr, Options{})
|
||||
var profile struct {
|
||||
ProfileID string `json:"profile_id"`
|
||||
Target struct {
|
||||
BackendID string `json:"backend_id"`
|
||||
Model string `json:"model"`
|
||||
Temperature float64 `json:"temperature"`
|
||||
} `json:"effective_model_params"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(stdout.String()), &profile); err != nil {
|
||||
t.Fatalf("development profile JSON = %q: %v", stdout.String(), err)
|
||||
}
|
||||
if code != 0 || stderr.Len() != 0 || profile.ProfileID != "dnd-extraction-development" || profile.Target.BackendID != "openrouter" || profile.Target.Model != "openai/gpt-5.6-luna" || profile.Target.Temperature != 0.2 {
|
||||
t.Fatalf("development profile inspection = code %d, %q, %q, %#v", code, stdout.String(), stderr.String(), profile)
|
||||
if code != 0 || stderr.Len() != 0 || !json.Valid([]byte(stdout.String())) || !strings.Contains(stdout.String(), "dnd-extraction-development") {
|
||||
t.Fatalf("development profile inspection: status=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,7 +243,16 @@ func generatedReferenceBindingDescriptions(resolved pipeline.ResolvedPipeline) [
|
||||
return descriptions
|
||||
}
|
||||
|
||||
func TestMaintainedMinimalInvocationProducesJSONBundle(t *testing.T) {
|
||||
func TestMaintainedMinimalInvocationPublishesDomainArtifacts(t *testing.T) {
|
||||
cfg := loadMaintainedExample(t, repositoryPath("examples", "dnd-minimal.config.yml"))
|
||||
resolved, err := cfg.Resolve(resolveInputForMaintainedExample(productionTestComponents(t), "dnd-session"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lane := referenceContractLane(t, resolved.ResolvedPipeline, "spells")
|
||||
if lane.Extract.Module != "dnd/spells" || lane.Normalize.Module != spellnormalize.Key || len(lane.ExtractReferences.Bindings) != 0 || len(lane.NormalizeReferences.Bindings) != 0 {
|
||||
t.Fatalf("minimal spell composition=%#v, want embedded catalog without overlay references", lane)
|
||||
}
|
||||
outputRoot := filepath.Join(t.TempDir(), "output")
|
||||
fake := &productionFakeLLMClient{}
|
||||
options := productionRunOptions(t, fake)
|
||||
@@ -282,60 +260,27 @@ func TestMaintainedMinimalInvocationProducesJSONBundle(t *testing.T) {
|
||||
code := RunWithOptions([]string{
|
||||
"run", "dnd-session",
|
||||
"--config", repositoryPath("examples", "dnd-minimal.config.yml"),
|
||||
"--input", repositoryPath("examples", "seriatim-minimal-transcript.json"),
|
||||
"--only", "spells", "--chunk_cache", "bypass", "--output-dir", outputRoot,
|
||||
"--input-file", repositoryPath("examples", "seriatim-minimal-transcript.json"),
|
||||
"--lane", "spells", "--cache", "bypass", "--output-dir", outputRoot, "--format", "json",
|
||||
}, &stdout, &stderr, options)
|
||||
if code != 0 {
|
||||
t.Fatalf("code=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
|
||||
t.Fatalf("status=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
|
||||
}
|
||||
if !strings.Contains(stdout.String(), `pipeline "dnd-session"`) || !strings.Contains(stdout.String(), "outputs=1 rejected=0") {
|
||||
t.Fatalf("stdout=%q, want completed pipeline and counts", stdout.String())
|
||||
var report runResult
|
||||
if err := json.Unmarshal([]byte(stdout.String()), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
runRoot := filepath.Join(outputRoot, productionRunID)
|
||||
index := readProductionJSON[exampleOutputIndex](t, filepath.Join(runRoot, "index.json"))
|
||||
if index.ManifestFile != "manifest.json" || index.RejectedFile != "rejected.json" || index.WarningsFile != "warnings.json" || len(index.OutputFiles) != 1 {
|
||||
t.Fatalf("index = %#v, want one spells output and fixed companion files", index)
|
||||
if report.Pipeline != "dnd-session" || report.ArtifactCount != 1 || report.OutputDirectory != runRoot || report.FallbackCount != 0 {
|
||||
t.Fatalf("completion=%#v", report)
|
||||
}
|
||||
entry := index.OutputFiles[0]
|
||||
if entry.LaneID != "spells" || entry.File != "lanes/spells.json" || entry.MediaType != "application/json" || entry.SchemaID != "notarius.dnd.spells" || entry.SchemaVersion != "v1" {
|
||||
t.Fatalf("index output entry = %#v, want spells JSON contract", entry)
|
||||
files := readTree(t, runRoot)
|
||||
if len(files) != 1 || files["lanes/spells.json"] == nil {
|
||||
t.Fatalf("published files=%v, want only the spell artifact", files)
|
||||
}
|
||||
|
||||
manifest := readProductionJSON[artifacts.RunManifest](t, filepath.Join(runRoot, "manifest.json"))
|
||||
if manifest.PipelineID != "dnd-session" || manifest.InputModule != "seriatim" || manifest.Chunker != "generic" || manifest.OutputEncoder != "json" || manifest.ValidationStatus != "approved" || manifest.ChunkPlan == nil || manifest.ChunkPlan.Action != "bypassed" {
|
||||
t.Fatalf("manifest = %#v, want approved minimal run", manifest)
|
||||
}
|
||||
if len(manifest.ArtifactLanes) != 1 {
|
||||
t.Fatalf("manifest lanes = %#v, want exactly spells", manifest.ArtifactLanes)
|
||||
}
|
||||
lane := manifest.ArtifactLanes[0]
|
||||
if lane.ID != "spells" || lane.Extractor != "dnd/spells" || lane.Merger != "appendorder" || lane.Normalizer != spellnormalize.Key {
|
||||
t.Fatalf("manifest lane = %#v, want production spells composition", lane)
|
||||
}
|
||||
if len(manifest.References) != 0 {
|
||||
t.Fatalf("base-only manifest references = %#v, want no overlay provenance", manifest.References)
|
||||
}
|
||||
extractorMetadata, ok := lane.Metadata["extractor"].(map[string]any)
|
||||
if !ok || len(stringValues(extractorMetadata["catalog_overlay_ids"])) != 0 {
|
||||
t.Fatalf("base-only extractor metadata = %#v, want no overlay IDs", lane.Metadata)
|
||||
}
|
||||
|
||||
artifact := readProductionJSON[dnd.SpellList](t, filepath.Join(runRoot, entry.File))
|
||||
if len(artifact.SpellCasts) != 1 || artifact.SpellCasts[0].Spell != "Cure Wounds" || artifact.SpellCasts[0].SourceRefs[0].SourceID != "session-alpha" {
|
||||
t.Fatalf("artifact = %#v, want one source-linked Cure Wounds cast", artifact)
|
||||
}
|
||||
rejected := readProductionJSON[struct {
|
||||
Rejected []json.RawMessage `json:"rejected"`
|
||||
}](t, filepath.Join(runRoot, "rejected.json"))
|
||||
if len(rejected.Rejected) != 0 {
|
||||
t.Fatalf("rejected = %#v, want empty rejection list", rejected.Rejected)
|
||||
}
|
||||
warnings := readProductionJSON[struct {
|
||||
Groups []json.RawMessage `json:"groups"`
|
||||
}](t, filepath.Join(runRoot, "warnings.json"))
|
||||
if len(warnings.Groups) != 0 {
|
||||
t.Fatalf("warnings = %#v, want empty warning list", warnings.Groups)
|
||||
artifact := readProductionJSON[dnd.SpellList](t, filepath.Join(runRoot, "lanes/spells.json"))
|
||||
if len(artifact.SpellCasts) != 1 || artifact.SpellCasts[0].Spell != "Cure Wounds" || len(artifact.SpellCasts[0].SourceRefs) != 1 || artifact.SpellCasts[0].SourceRefs[0].SourceID != "session-alpha" {
|
||||
t.Fatalf("artifact=%#v, want one source-linked Cure Wounds cast", artifact)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -352,11 +297,12 @@ func TestMaintainedMalformedInputOnlyRecordsDebugFailureWhenRequested(t *testing
|
||||
t.Run(name, func(t *testing.T) {
|
||||
outputRoot := filepath.Join(t.TempDir(), "output")
|
||||
debugRoot := filepath.Join(t.TempDir(), "debug")
|
||||
options := productionRunOptions(t, &productionFakeLLMClient{})
|
||||
client := &productionFakeLLMClient{}
|
||||
options := productionRunOptions(t, client)
|
||||
args := []string{
|
||||
"run", "dnd-session",
|
||||
"--config", repositoryPath("examples", "dnd-minimal.config.yml"),
|
||||
"--input", malformed, "--chunk_cache", "bypass", "--output-dir", outputRoot,
|
||||
"--input-file", malformed, "--cache", "bypass", "--output-dir", outputRoot,
|
||||
}
|
||||
if debug {
|
||||
args = append(args, "--debug", "--debug-dir", debugRoot)
|
||||
@@ -372,35 +318,23 @@ func TestMaintainedMalformedInputOnlyRecordsDebugFailureWhenRequested(t *testing
|
||||
return
|
||||
}
|
||||
bundle := onlyChildDir(t, debugRoot)
|
||||
report := readProductionJSON[debugbundle.RunReport](t, filepath.Join(bundle, "summary", "run-report.json"))
|
||||
if report.Succeeded || report.PipelineID != "dnd-session" {
|
||||
t.Fatalf("failure report = %#v, want failed dnd-session report", report)
|
||||
outcome := readProductionJSON[struct {
|
||||
Error string `json:"error"`
|
||||
Outcome struct {
|
||||
Pipeline string `json:"pipeline"`
|
||||
Published bool `json:"published"`
|
||||
} `json:"outcome"`
|
||||
}](t, filepath.Join(bundle, "outcome.json"))
|
||||
if !strings.Contains(outcome.Error, "parse input") || outcome.Outcome.Pipeline != "dnd-session" || outcome.Outcome.Published {
|
||||
t.Fatalf("failure evidence=%#v", outcome)
|
||||
}
|
||||
invocation := readProductionJSON[debugbundle.Invocation](t, filepath.Join(bundle, "summary", "invocation.json"))
|
||||
manifest := readProductionJSON[artifacts.RunManifest](t, filepath.Join(bundle, "summary", "run-manifest.json"))
|
||||
manifestSession, found := manifest.Metadata["session_id"]
|
||||
if invocation.SessionID == "" || !found || manifestSession != invocation.SessionID {
|
||||
t.Fatalf("failed run sessions: invocation=%q manifest=%#v metadata=%#v", invocation.SessionID, manifestSession, manifest.Metadata)
|
||||
if len(client.requestsSnapshot()) != 0 {
|
||||
t.Fatal("malformed input reached provider")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type exampleOutputIndex struct {
|
||||
ManifestFile string `json:"manifest_file"`
|
||||
OutputFiles []exampleOutputIndexEntry `json:"output_files"`
|
||||
RejectedFile string `json:"rejected_file"`
|
||||
WarningsFile string `json:"warnings_file"`
|
||||
}
|
||||
|
||||
type exampleOutputIndexEntry struct {
|
||||
LaneID string `json:"lane_id"`
|
||||
MediaType string `json:"media_type"`
|
||||
File string `json:"file"`
|
||||
SchemaID string `json:"schema_id"`
|
||||
SchemaVersion string `json:"schema_version"`
|
||||
}
|
||||
|
||||
func resolveInputForMaintainedExample(components productionComponents, pipelineID string) config.ResolveInput {
|
||||
return config.ResolveInput{PipelineID: pipelineID, Catalog: catalogFromRegistries(components.registries)}
|
||||
}
|
||||
|
||||
@@ -216,8 +216,7 @@ func executeRunFixture(t *testing.T, app *commandkit.App, args ...string) comman
|
||||
var out, diagnostics bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostics})
|
||||
if outcome.Status != 0 {
|
||||
writeConfigLoadError(&diagnostics, outcome.Cause)
|
||||
t.Fatalf("%v: %#v stderr=%s", args, outcome, diagnostics.String())
|
||||
t.Fatalf("%v: %#v cause=%v stderr=%s", args, outcome, outcome.Cause, diagnostics.String())
|
||||
}
|
||||
return outcome
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user