Remove retired runtime wrappers and persistent cache machinery
This commit is contained in:
@@ -197,7 +197,7 @@ pipelines:
|
||||
t.Fatalf("Resolve(child) error = %v", err)
|
||||
}
|
||||
lanes := resolved.ResolvedPipeline.AllArtifactLanes()
|
||||
if len(lanes) != 1 || lanes[0].Extract.MaxAdditionalTurns == nil || *lanes[0].Extract.MaxAdditionalTurns != 0 {
|
||||
if len(lanes) != 1 || lanes[0].ExtractControls.MaxAdditionalTurns != 0 {
|
||||
t.Fatalf("resolved child turn allowance = %#v", lanes)
|
||||
}
|
||||
if !resolved.ResolvedPipeline.Chunk.Validators.Set || len(resolved.ResolvedPipeline.Chunk.Validators.Validators) != 0 ||
|
||||
|
||||
@@ -24,8 +24,8 @@ func TestValidateStateSurfaceRules(t *testing.T) {
|
||||
setup func(*Config)
|
||||
}{
|
||||
{"output", func(c *Config) { c.Output.Directory = "" }},
|
||||
{"cache", func(c *Config) { c.CacheDirectory = "\\x00" }},
|
||||
{"debug", func(c *Config) { c.Debug.Directory = "\\x00" }},
|
||||
{"cache", func(c *Config) { c.CacheDirectory = "\x00" }},
|
||||
{"debug", func(c *Config) { c.Debug.Directory = "\x00" }},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
c := Default()
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package checkpoint
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
)
|
||||
|
||||
func TestFilesystemCheckpointPermissionsFollowUmask(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
umask int
|
||||
dirMode os.FileMode
|
||||
fileMode os.FileMode
|
||||
}{
|
||||
{name: "private", umask: 0o077, dirMode: 0o700, fileMode: 0o600},
|
||||
{name: "group", umask: 0o007, dirMode: 0o770, fileMode: 0o660},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
previousUmask := syscall.Umask(test.umask)
|
||||
defer syscall.Umask(previousUmask)
|
||||
|
||||
root := filepath.Join(t.TempDir(), "checkpoints")
|
||||
identity, err := NewIdentity(representativeIdentityInput())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
recorder, err := NewFilesystemRecorder(root, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := recorder.SourceSucceeded("source-module", sourceDocumentForPermissions()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
relative, err := identity.RelativePath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
identityRoot := filepath.Join(root, filepath.FromSlash(relative))
|
||||
sourceRoot := filepath.Join(identityRoot, "source")
|
||||
for path, want := range map[string]os.FileMode{
|
||||
root: test.dirMode,
|
||||
identityRoot: test.dirMode,
|
||||
sourceRoot: test.dirMode,
|
||||
filepath.Join(sourceRoot, "manifest.json"): test.fileMode,
|
||||
filepath.Join(sourceRoot, "source-document.json"): test.fileMode,
|
||||
} {
|
||||
assertCheckpointPermissions(t, path, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemCheckpointLeavesExistingRootUntouched(t *testing.T) {
|
||||
previousUmask := syscall.Umask(0o077)
|
||||
defer syscall.Umask(previousUmask)
|
||||
|
||||
root := filepath.Join(t.TempDir(), "checkpoints")
|
||||
syscall.Umask(0)
|
||||
if err := os.Mkdir(root, 0o751); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
syscall.Umask(0o077)
|
||||
identity, err := NewIdentity(representativeIdentityInput())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
recorder, err := NewFilesystemRecorder(root, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := recorder.SourceSucceeded("source-module", sourceDocumentForPermissions()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertCheckpointPermissions(t, root, 0o751)
|
||||
}
|
||||
|
||||
func sourceDocumentForPermissions() *source.SourceDocument {
|
||||
document := checkpointDocument()
|
||||
return &document
|
||||
}
|
||||
|
||||
func assertCheckpointPermissions(t *testing.T, path string, want os.FileMode) {
|
||||
t.Helper()
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := info.Mode().Perm(); got != want {
|
||||
t.Fatalf("%s mode = %#o, want %#o", path, got, want)
|
||||
}
|
||||
}
|
||||
@@ -1,568 +0,0 @@
|
||||
package checkpoint
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
)
|
||||
|
||||
type filesystemCheckpointFixture struct {
|
||||
root string
|
||||
identity Identity
|
||||
loader pipeline.CheckpointLoader
|
||||
doc source.SourceDocument
|
||||
extract pipeline.CheckpointArtifact
|
||||
merge pipeline.CheckpointArtifact
|
||||
normalize pipeline.CheckpointArtifact
|
||||
dependencies []pipeline.CheckpointFingerprint
|
||||
rejected []contracts.RejectedOutput
|
||||
}
|
||||
|
||||
func TestFilesystemCheckpointRoundTripsAllStages(t *testing.T) {
|
||||
fixture := seedFilesystemCheckpoints(t)
|
||||
|
||||
// Recording owns its inputs. These mutations must not change the durable values.
|
||||
fixture.doc.Units[0].Text = "caller mutation"
|
||||
fixture.doc.Metadata["owner"] = "caller mutation"
|
||||
fixture.extract.Artifact.Content[0] = 'x'
|
||||
fixture.extract.Artifact.Metadata["content"] = "caller mutation"
|
||||
fixture.extract.Diagnostics[0].Diagnostic.Samples[0].Message = "caller mutation"
|
||||
fixture.merge.Artifact.Content[0] = 'x'
|
||||
fixture.normalize.Artifact.Content[0] = 'x'
|
||||
fixture.rejected[0].Message = "caller mutation"
|
||||
|
||||
t.Run("source", func(t *testing.T) {
|
||||
got, decision := fixture.loader.Source("source-module")
|
||||
if !decision.Reused {
|
||||
t.Fatalf("source decision = %#v", decision)
|
||||
}
|
||||
if got.Document == nil || got.Document.ID != "document-1" || got.Document.Units[0].Text != "original source" || got.Document.Metadata["owner"] != "fixture" {
|
||||
t.Fatalf("source was not restored: %#v", got)
|
||||
}
|
||||
|
||||
got.Document.Units[0].Text = "loaded mutation"
|
||||
got.Document.Metadata["owner"] = "loaded mutation"
|
||||
reloaded, decision := fixture.loader.Source("source-module")
|
||||
if !decision.Reused || reloaded.Document.Units[0].Text != "original source" || reloaded.Document.Metadata["owner"] != "fixture" {
|
||||
t.Fatalf("source reload changed after loaded mutation: %#v decision=%#v", reloaded, decision)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("extract", func(t *testing.T) {
|
||||
got, decision := fixture.loader.Extract("lane-a", "extract-module", fixture.dependencies)
|
||||
if !decision.Reused || len(got.Outputs) != 1 || len(got.Rejected) != 1 || len(got.Outputs[0].Diagnostics) != 1 {
|
||||
t.Fatalf("extract result=%#v decision=%#v", got, decision)
|
||||
}
|
||||
output := got.Outputs[0]
|
||||
if !bytes.Equal(output.Artifact.Content, []byte(`{"spell":"fire"}`)) || output.Artifact.Kind != "spell" || output.Artifact.Schema.ID != "spell-schema" || output.Artifact.Schema.Version != "1" || output.Artifact.MediaType != "application/json" || output.Artifact.Metadata["chunk"] != "chunk-a" || output.ChunkRef.StartUnitID != 1 || output.Diagnostics[0].Diagnostic.ReasonCode != "normalized_record" || got.Rejected[0].ReasonCode != "invalid_source" {
|
||||
t.Fatalf("extract values were not restored: %#v", got)
|
||||
}
|
||||
manifest := readManifest[ExtractLaneManifest](t, filepath.Join(fixture.root, mustRelativePath(t, fixture.identity), "extract", "lane-a", "manifest.json"))
|
||||
if manifest.Status != StatusSucceededWithRejections {
|
||||
t.Fatalf("extract status = %q, want succeeded with rejections", manifest.Status)
|
||||
}
|
||||
|
||||
got.Outputs[0].Artifact.Content[0] = 'y'
|
||||
reloaded, decision := fixture.loader.Extract("lane-a", "extract-module", fixture.dependencies)
|
||||
if !decision.Reused || !bytes.Equal(reloaded.Outputs[0].Artifact.Content, []byte(`{"spell":"fire"}`)) {
|
||||
t.Fatalf("extract reload changed after loaded mutation: %#v decision=%#v", reloaded, decision)
|
||||
}
|
||||
})
|
||||
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
load func() (pipeline.CheckpointArtifact, pipeline.CheckpointDecision)
|
||||
want []byte
|
||||
}{
|
||||
{name: "merge", load: func() (pipeline.CheckpointArtifact, pipeline.CheckpointDecision) {
|
||||
got, decision := fixture.loader.Merge("lane-a", "merge-module", fixture.dependencies)
|
||||
return got.Output, decision
|
||||
}, want: []byte(`{"spells":["fire"]}`)},
|
||||
{name: "normalize", load: func() (pipeline.CheckpointArtifact, pipeline.CheckpointDecision) {
|
||||
got, decision := fixture.loader.Normalize("lane-a", "normalize-module", fixture.dependencies)
|
||||
return got.Output, decision
|
||||
}, want: []byte(`{"spells":["fire"],"normalized":true}`)},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, decision := tt.load()
|
||||
if !decision.Reused || !bytes.Equal(got.Artifact.Content, tt.want) || got.Artifact.Kind != "spell" || got.Artifact.Schema.ID != "spell-schema" || got.Artifact.Schema.Version != "1" || got.Artifact.Metadata["lane"] != "lane-a" || len(got.Diagnostics) != 1 || got.Diagnostics[0].Diagnostic.ReasonCode != "normalized_record" {
|
||||
t.Fatalf("%s result=%#v decision=%#v", tt.name, got, decision)
|
||||
}
|
||||
|
||||
got.Artifact.Content[0] = 'z'
|
||||
reloaded, decision := tt.load()
|
||||
if !decision.Reused || !bytes.Equal(reloaded.Artifact.Content, tt.want) {
|
||||
t.Fatalf("%s reload changed after loaded mutation: %#v decision=%#v", tt.name, reloaded, decision)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFilesystemCheckpointRejectsMissingAndCorruptState(t *testing.T) {
|
||||
for _, stage := range checkpointStages() {
|
||||
t.Run(stage.name+" missing manifest", func(t *testing.T) {
|
||||
fixture := seedFilesystemCheckpoints(t)
|
||||
if err := os.Remove(stage.manifest(fixture)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertStageNotReused(t, stage, fixture, pipeline.CheckpointReasonMissing)
|
||||
})
|
||||
|
||||
t.Run(stage.name+" malformed manifest", func(t *testing.T) {
|
||||
fixture := seedFilesystemCheckpoints(t)
|
||||
if err := os.WriteFile(stage.manifest(fixture), []byte("{"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertStageNotReused(t, stage, fixture, pipeline.CheckpointReasonDecodeFailed)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemCheckpointRejectsIncompatibleManifests(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
edit func(map[string]any)
|
||||
want pipeline.CheckpointReasonCode
|
||||
}{
|
||||
{"v1 schema", func(m map[string]any) { m["workspace_schema_version"] = WorkspaceSchemaVersionV1 }, pipeline.CheckpointReasonWorkspaceSchemaIncompatible},
|
||||
{"v2 schema", func(m map[string]any) { m["workspace_schema_version"] = WorkspaceSchemaVersionV2 }, pipeline.CheckpointReasonWorkspaceSchemaIncompatible},
|
||||
{"v3 schema", func(m map[string]any) { m["workspace_schema_version"] = WorkspaceSchemaVersionV3 }, pipeline.CheckpointReasonWorkspaceSchemaIncompatible},
|
||||
{"unknown schema", func(m map[string]any) { m["workspace_schema_version"] = "notarius.workspace.future" }, pipeline.CheckpointReasonWorkspaceSchemaIncompatible},
|
||||
{"identity", func(m map[string]any) { m["metadata"].(map[string]any)["checkpoint_identity_digest"] = "sha256:other" }, pipeline.CheckpointReasonIdentityMismatch},
|
||||
{"stage", func(m map[string]any) { m["stage"] = string(StageMerge) }, pipeline.CheckpointReasonStageMismatch},
|
||||
{"lane", func(m map[string]any) { m["lane_id"] = "lane-other" }, pipeline.CheckpointReasonLaneMismatch},
|
||||
{"module", func(m map[string]any) { m["module_key"] = "module-other" }, pipeline.CheckpointReasonModuleMismatch},
|
||||
{"dependency", func(m map[string]any) {
|
||||
m["dependency_fingerprints"] = []map[string]string{{"name": "input", "value": "other"}}
|
||||
}, pipeline.CheckpointReasonDependencyMismatch},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
fixture := seedFilesystemCheckpoints(t)
|
||||
editManifest(t, checkpointStages()[1].manifest(fixture), tt.edit)
|
||||
assertStageNotReused(t, checkpointStages()[1], fixture, tt.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemCheckpointRejectsNonTerminalStatuses(t *testing.T) {
|
||||
for _, status := range []StageStatus{StatusRunning, StatusFailed, StatusPending, StatusInvalidated} {
|
||||
t.Run(string(status), func(t *testing.T) {
|
||||
fixture := seedFilesystemCheckpoints(t)
|
||||
editManifest(t, checkpointStages()[1].manifest(fixture), func(m map[string]any) { m["status"] = string(status) })
|
||||
assertStageNotReused(t, checkpointStages()[1], fixture, pipeline.CheckpointReasonStatusNotReusable)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemCheckpointRejectsIncompleteArtifactsAndContent(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
edit func(map[string]any)
|
||||
want pipeline.CheckpointReasonCode
|
||||
}{
|
||||
{"artifact kind", func(m map[string]any) { m["outputs"].([]any)[0].(map[string]any)["artifact_kind"] = "" }, pipeline.CheckpointReasonArtifactCodecIncompatible},
|
||||
{"schema id", func(m map[string]any) { m["outputs"].([]any)[0].(map[string]any)["schema"].(map[string]any)["id"] = "" }, pipeline.CheckpointReasonArtifactCodecIncompatible},
|
||||
{"schema version", func(m map[string]any) {
|
||||
m["outputs"].([]any)[0].(map[string]any)["schema"].(map[string]any)["version"] = ""
|
||||
}, pipeline.CheckpointReasonArtifactCodecIncompatible},
|
||||
{"schema digest", func(m map[string]any) { m["outputs"].([]any)[0].(map[string]any)["schema_digest"] = "" }, pipeline.CheckpointReasonArtifactCodecIncompatible},
|
||||
{"base64", func(m map[string]any) {
|
||||
m["outputs"].([]any)[0].(map[string]any)["content"].(map[string]any)["content_base64"] = "%"
|
||||
}, pipeline.CheckpointReasonArtifactPayloadInvalid},
|
||||
{"content digest", func(m map[string]any) {
|
||||
m["outputs"].([]any)[0].(map[string]any)["content"].(map[string]any)["content_digest"] = "sha256:other"
|
||||
}, pipeline.CheckpointReasonArtifactDigestMismatch},
|
||||
{"diagnostics", func(m map[string]any) {
|
||||
m["outputs"].([]any)[0].(map[string]any)["diagnostics"] = []any{map[string]any{
|
||||
"diagnostic": map[string]any{"disposition": "warning", "category": "configuration", "reason_code": "invalid", "occurrence_count": float64(0)},
|
||||
}}
|
||||
}, pipeline.CheckpointReasonArtifactPayloadInvalid},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
fixture := seedFilesystemCheckpoints(t)
|
||||
editJSON(t, filepath.Join(fixture.root, mustRelativePath(t, fixture.identity), "extract", "lane-a", "outputs.json"), tt.edit)
|
||||
assertStageNotReused(t, checkpointStages()[1], fixture, tt.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemCheckpointRejectsSourceAndOutputDigestMismatches(t *testing.T) {
|
||||
t.Run("invalid source document", func(t *testing.T) {
|
||||
fixture := seedFilesystemCheckpoints(t)
|
||||
editJSON(t, filepath.Join(fixture.root, mustRelativePath(t, fixture.identity), "source", "source-document.json"), func(m map[string]any) {
|
||||
m["document"].(map[string]any)["units"].([]any)[0].(map[string]any)["text"] = ""
|
||||
})
|
||||
assertStageNotReused(t, checkpointStages()[0], fixture, pipeline.CheckpointReasonArtifactPayloadInvalid)
|
||||
})
|
||||
|
||||
t.Run("source output digest", func(t *testing.T) {
|
||||
fixture := seedFilesystemCheckpoints(t)
|
||||
editJSON(t, filepath.Join(fixture.root, mustRelativePath(t, fixture.identity), "source", "source-document.json"), func(m map[string]any) {
|
||||
m["document"].(map[string]any)["digest"] = "sha256:other"
|
||||
})
|
||||
assertStageNotReused(t, checkpointStages()[0], fixture, pipeline.CheckpointReasonArtifactDigestMismatch)
|
||||
})
|
||||
|
||||
for _, stage := range checkpointStages()[1:] {
|
||||
t.Run(stage.name+" output digest", func(t *testing.T) {
|
||||
fixture := seedFilesystemCheckpoints(t)
|
||||
editManifest(t, stage.manifest(fixture), func(m map[string]any) {
|
||||
m["output_digests"].([]any)[0].(map[string]any)["value"] = "sha256:other"
|
||||
})
|
||||
assertStageNotReused(t, stage, fixture, pipeline.CheckpointReasonArtifactDigestMismatch)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemCheckpointReusesExtractWithRejections(t *testing.T) {
|
||||
fixture := seedFilesystemCheckpoints(t)
|
||||
result, decision := fixture.loader.Extract("lane-a", "extract-module", fixture.dependencies)
|
||||
if !decision.Reused || len(result.Rejected) != 1 || result.Rejected[0].Message != "source reference is invalid" {
|
||||
t.Fatalf("result=%#v decision=%#v", result, decision)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemCheckpointDependencyDecisionIsBoundedAndCategorized(t *testing.T) {
|
||||
fixture := seedFilesystemCheckpoints(t)
|
||||
_, decision := fixture.loader.Extract("lane-a", "extract-module", []pipeline.CheckpointFingerprint{{Name: "source", Value: "sha256:changed"}})
|
||||
if decision.Reused || decision.Category != "dependency_invalidated" || decision.ReasonCode != "dependency_mismatch" {
|
||||
t.Fatalf("dependency decision = %#v", decision)
|
||||
}
|
||||
if strings.Contains(decision.Reason, fixture.root) || strings.Contains(decision.Detail, fixture.root) {
|
||||
t.Fatalf("dependency decision leaked checkpoint path: %#v", decision)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemCheckpointDecisionFamiliesAreStableAndSafe(t *testing.T) {
|
||||
const secretSentinel = "do-not-expose-checkpoint-secret"
|
||||
tests := []struct {
|
||||
name string
|
||||
prepare func(*testing.T, filesystemCheckpointFixture) pipeline.CheckpointDecision
|
||||
category pipeline.CheckpointDecisionCategory
|
||||
code pipeline.CheckpointReasonCode
|
||||
}{
|
||||
{"loading disabled", func(t *testing.T, _ filesystemCheckpointFixture) pipeline.CheckpointDecision {
|
||||
_, decision := pipeline.NoopCheckpointLoader().Source("source")
|
||||
return decision
|
||||
}, pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonLoadingDisabled},
|
||||
{"checkpoint unavailable", func(t *testing.T, fixture filesystemCheckpointFixture) pipeline.CheckpointDecision {
|
||||
if err := os.Remove(checkpointStages()[1].manifest(fixture)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return checkpointStages()[1].load(fixture)
|
||||
}, pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonMissing},
|
||||
{"workspace identity", func(t *testing.T, fixture filesystemCheckpointFixture) pipeline.CheckpointDecision {
|
||||
editManifest(t, checkpointStages()[1].manifest(fixture), func(m map[string]any) {
|
||||
m["workspace_schema_version"] = secretSentinel
|
||||
})
|
||||
return checkpointStages()[1].load(fixture)
|
||||
}, pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonWorkspaceSchemaIncompatible},
|
||||
{"manifest scope", func(t *testing.T, fixture filesystemCheckpointFixture) pipeline.CheckpointDecision {
|
||||
editManifest(t, checkpointStages()[1].manifest(fixture), func(m map[string]any) { m["module_key"] = secretSentinel })
|
||||
return checkpointStages()[1].load(fixture)
|
||||
}, pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonModuleMismatch},
|
||||
{"dependency invalidated", func(t *testing.T, fixture filesystemCheckpointFixture) pipeline.CheckpointDecision {
|
||||
_, decision := fixture.loader.Extract("lane-a", "extract-module", []pipeline.CheckpointFingerprint{{Name: "input", Value: secretSentinel}})
|
||||
return decision
|
||||
}, pipeline.CheckpointDecisionDependencyInvalidated, pipeline.CheckpointReasonDependencyMismatch},
|
||||
{"artifact payload", func(t *testing.T, fixture filesystemCheckpointFixture) pipeline.CheckpointDecision {
|
||||
editJSON(t, filepath.Join(fixture.root, mustRelativePath(t, fixture.identity), "extract", "lane-a", "outputs.json"), func(m map[string]any) {
|
||||
m["outputs"].([]any)[0].(map[string]any)["artifact_kind"] = ""
|
||||
m["outputs"].([]any)[0].(map[string]any)["source_id"] = secretSentinel
|
||||
})
|
||||
return checkpointStages()[1].load(fixture)
|
||||
}, pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonArtifactCodecIncompatible},
|
||||
{"checkpoint reused", func(t *testing.T, fixture filesystemCheckpointFixture) pipeline.CheckpointDecision {
|
||||
return checkpointStages()[1].load(fixture)
|
||||
}, pipeline.CheckpointDecisionReused, pipeline.CheckpointReasonReused},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
fixture := seedFilesystemCheckpoints(t)
|
||||
decision := test.prepare(t, fixture)
|
||||
if decision.Category != test.category || decision.ReasonCode != test.code {
|
||||
t.Fatalf("decision = %#v, want category %q and code %q", decision, test.category, test.code)
|
||||
}
|
||||
if len([]byte(decision.Detail)) > 512 || !utf8.ValidString(decision.Detail) {
|
||||
t.Fatalf("decision detail is not bounded valid UTF-8: %#v", decision)
|
||||
}
|
||||
for _, forbidden := range []string{secretSentinel, fixture.root} {
|
||||
if strings.Contains(decision.Detail, forbidden) || strings.Contains(decision.Reason, forbidden) {
|
||||
t.Fatalf("decision leaked %q: %#v", forbidden, decision)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemLoaderReadsAcceptedNormalizeWithoutStageDependencies(t *testing.T) {
|
||||
fixture := seedAcceptedNormalizeCheckpoint(t)
|
||||
identityRoot := filepath.Join(fixture.root, mustRelativePath(t, fixture.identity))
|
||||
for _, stage := range []string{"extract", "merge"} {
|
||||
if _, err := os.Stat(filepath.Join(identityRoot, laneManifestPath(stage, "step-1", "lane-a"))); !os.IsNotExist(err) {
|
||||
t.Fatalf("%s checkpoint stat error = %v, want absent prerequisite", stage, err)
|
||||
}
|
||||
}
|
||||
checkpoint, decision := fixture.loader.AcceptedNormalize("step-1", "lane-a", "normalize-module")
|
||||
if !decision.Reused || decision.Category != pipeline.CheckpointDecisionReused || decision.ReasonCode != pipeline.CheckpointReasonAcceptedArtifactReused {
|
||||
t.Fatalf("accepted normalize decision = %#v", decision)
|
||||
}
|
||||
if checkpoint.Output.Artifact.Content == nil || string(checkpoint.Output.Artifact.Content) != string(fixture.normalize.Artifact.Content) {
|
||||
t.Fatalf("accepted normalize output = %#v, want recorded artifact", checkpoint.Output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemLoaderRejectsInvalidAcceptedNormalize(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mutate func(*testing.T, *filesystemCheckpointFixture)
|
||||
code pipeline.CheckpointReasonCode
|
||||
}{
|
||||
{"missing", func(t *testing.T, fixture *filesystemCheckpointFixture) {
|
||||
if err := os.Remove(acceptedNormalizeManifest(t, *fixture)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}, pipeline.CheckpointReasonMissing},
|
||||
{"rejected status", func(t *testing.T, fixture *filesystemCheckpointFixture) {
|
||||
editManifest(t, acceptedNormalizeManifest(t, *fixture), func(m map[string]any) { m["status"] = string(StatusSucceededWithRejections) })
|
||||
}, pipeline.CheckpointReasonStatusNotReusable},
|
||||
{"corrupt payload", func(t *testing.T, fixture *filesystemCheckpointFixture) {
|
||||
if err := os.WriteFile(acceptedNormalizePayload(t, *fixture), []byte("{"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}, pipeline.CheckpointReasonDecodeFailed},
|
||||
{"wrong codec identity", func(t *testing.T, fixture *filesystemCheckpointFixture) {
|
||||
editJSON(t, acceptedNormalizePayload(t, *fixture), func(m map[string]any) { m["output"].(map[string]any)["artifact_kind"] = "" })
|
||||
}, pipeline.CheckpointReasonArtifactCodecIncompatible},
|
||||
{"wrong content digest", func(t *testing.T, fixture *filesystemCheckpointFixture) {
|
||||
editJSON(t, acceptedNormalizePayload(t, *fixture), func(m map[string]any) {
|
||||
m["output"].(map[string]any)["content"].(map[string]any)["content_digest"] = "sha256:wrong"
|
||||
})
|
||||
}, pipeline.CheckpointReasonArtifactDigestMismatch},
|
||||
{"unverifiable identity", func(t *testing.T, fixture *filesystemCheckpointFixture) {
|
||||
loader := fixture.loader.(*FilesystemLoader)
|
||||
fixture.loader = &FilesystemLoader{root: loader.root}
|
||||
}, pipeline.CheckpointReasonIdentityMismatch},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
fixture := seedAcceptedNormalizeCheckpoint(t)
|
||||
test.mutate(t, &fixture)
|
||||
_, decision := fixture.loader.AcceptedNormalize("step-1", "lane-a", "normalize-module")
|
||||
if decision.Reused || decision.ReasonCode != test.code {
|
||||
t.Fatalf("accepted normalize decision = %#v, want %q", decision, test.code)
|
||||
}
|
||||
if strings.Contains(decision.Detail, fixture.root) || strings.Contains(decision.Detail, string(fixture.normalize.Artifact.Content)) {
|
||||
t.Fatalf("accepted normalize decision leaked path or content: %#v", decision)
|
||||
}
|
||||
if _, err := os.Stat(acceptedNormalizePayload(t, fixture)); err != nil {
|
||||
t.Fatalf("accepted normalize payload was removed after rejection: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func seedAcceptedNormalizeCheckpoint(t *testing.T) filesystemCheckpointFixture {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
identity := testIdentity(t)
|
||||
artifact := checkpointArtifact("normalize-module", `{"accepted":true}`)
|
||||
recorder, err := NewFilesystemRecorder(root, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stepRecorder := recorder.(pipeline.StepCheckpointRecorder)
|
||||
if err := stepRecorder.NormalizeSucceededForStep("step-1", "lane-a", "normalize-module", []pipeline.CheckpointFingerprint{{Name: "merge", Value: "sha256:unavailable"}}, artifact); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loader, err := NewFilesystemLoader(root, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return filesystemCheckpointFixture{root: root, identity: identity, loader: loader, normalize: artifact}
|
||||
}
|
||||
|
||||
func acceptedNormalizeManifest(t *testing.T, fixture filesystemCheckpointFixture) string {
|
||||
t.Helper()
|
||||
return filepath.Join(fixture.root, mustRelativePath(t, fixture.identity), laneManifestPath("normalize", "step-1", "lane-a"))
|
||||
}
|
||||
|
||||
func acceptedNormalizePayload(t *testing.T, fixture filesystemCheckpointFixture) string {
|
||||
t.Helper()
|
||||
return filepath.Join(fixture.root, mustRelativePath(t, fixture.identity), lanePayloadPath("normalize", "step-1", "lane-a", "output.json"))
|
||||
}
|
||||
|
||||
type checkpointStage struct {
|
||||
name string
|
||||
manifest func(filesystemCheckpointFixture) string
|
||||
load func(filesystemCheckpointFixture) pipeline.CheckpointDecision
|
||||
}
|
||||
|
||||
func checkpointStages() []checkpointStage {
|
||||
return []checkpointStage{
|
||||
{
|
||||
name: "source",
|
||||
manifest: func(f filesystemCheckpointFixture) string {
|
||||
return filepath.Join(f.root, mustRelativePathForTest(f.identity), "source", "manifest.json")
|
||||
},
|
||||
load: func(f filesystemCheckpointFixture) pipeline.CheckpointDecision {
|
||||
_, d := f.loader.Source("source-module")
|
||||
return d
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "extract",
|
||||
manifest: func(f filesystemCheckpointFixture) string {
|
||||
return filepath.Join(f.root, mustRelativePathForTest(f.identity), "extract", "lane-a", "manifest.json")
|
||||
},
|
||||
load: func(f filesystemCheckpointFixture) pipeline.CheckpointDecision {
|
||||
_, d := f.loader.Extract("lane-a", "extract-module", f.dependencies)
|
||||
return d
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "merge",
|
||||
manifest: func(f filesystemCheckpointFixture) string {
|
||||
return filepath.Join(f.root, mustRelativePathForTest(f.identity), "merge", "lane-a", "manifest.json")
|
||||
},
|
||||
load: func(f filesystemCheckpointFixture) pipeline.CheckpointDecision {
|
||||
_, d := f.loader.Merge("lane-a", "merge-module", f.dependencies)
|
||||
return d
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "normalize",
|
||||
manifest: func(f filesystemCheckpointFixture) string {
|
||||
return filepath.Join(f.root, mustRelativePathForTest(f.identity), "normalize", "lane-a", "manifest.json")
|
||||
},
|
||||
load: func(f filesystemCheckpointFixture) pipeline.CheckpointDecision {
|
||||
_, d := f.loader.Normalize("lane-a", "normalize-module", f.dependencies)
|
||||
return d
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func seedFilesystemCheckpoints(t *testing.T) filesystemCheckpointFixture {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
identity, err := NewIdentity(representativeIdentityInput())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
recorder, err := NewFilesystemRecorder(root, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fixture := filesystemCheckpointFixture{
|
||||
root: root,
|
||||
identity: identity,
|
||||
doc: checkpointDocument(),
|
||||
extract: checkpointArtifact("extract", `{"spell":"fire"}`),
|
||||
merge: checkpointArtifact("merge", `{"spells":["fire"]}`),
|
||||
normalize: checkpointArtifact("normalize", `{"spells":["fire"],"normalized":true}`),
|
||||
dependencies: []pipeline.CheckpointFingerprint{{Name: "source", Value: "sha256:source"}, {Name: "chunk-plan", Value: "sha256:plan"}},
|
||||
rejected: []contracts.RejectedOutput{{Stage: "extract", LaneID: "lane-a", ModuleKey: "extract-module", ChunkID: "chunk-a", ValidatorName: "source_refs", ReasonCode: "invalid_source", Message: "source reference is invalid", AttemptCount: 1}},
|
||||
}
|
||||
if err := recorder.SourceSucceeded("source-module", &fixture.doc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := recorder.ExtractSucceeded("lane-a", "extract-module", fixture.dependencies, []pipeline.CheckpointArtifact{fixture.extract}, fixture.rejected); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := recorder.MergeSucceeded("lane-a", "merge-module", fixture.dependencies, fixture.merge); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := recorder.NormalizeSucceeded("lane-a", "normalize-module", fixture.dependencies, fixture.normalize); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fixture.loader, err = NewFilesystemLoader(root, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return fixture
|
||||
}
|
||||
|
||||
func checkpointDocument() source.SourceDocument {
|
||||
return source.SourceDocument{
|
||||
ID: "document-1", Kind: "transcript", Format: "text", Digest: "sha256:document", Metadata: map[string]any{"owner": "fixture", "number": float64(1)},
|
||||
Units: []source.SourceUnit{{ID: 1, Kind: "line", Text: "original source", Ref: source.SourceRef{SourceID: "document-1", StartUnitID: 1, EndUnitID: 1}, Metadata: map[string]any{"speaker": "narrator"}}},
|
||||
}
|
||||
}
|
||||
|
||||
func checkpointArtifact(module, content string) pipeline.CheckpointArtifact {
|
||||
return pipeline.CheckpointArtifact{
|
||||
LaneID: "lane-a", ModuleKey: module, SourceID: "document-1", ChunkID: "chunk-a", ChunkIndex: 0,
|
||||
ChunkRef: source.SourceRef{SourceID: "document-1", StartUnitID: 1, EndUnitID: 1}, SchemaDigest: "sha256:schema",
|
||||
Artifact: contracts.SerializedArtifact{Kind: "spell", Schema: contracts.ArtifactSchema{ID: "spell-schema", Name: "Spell", Version: "1", JSONSchema: []byte(`{"type":"object"}`)}, MediaType: "application/json", Content: []byte(content), Metadata: map[string]any{"chunk": "chunk-a", "lane": "lane-a"}},
|
||||
Diagnostics: []pipeline.CheckpointDiagnostic{{Diagnostic: contracts.ProducerDiagnostic{Disposition: contracts.DiagnosticDispositionObservation, Category: contracts.DiagnosticCategoryNormalization, ReasonCode: "normalized_record", OccurrenceCount: 1, Samples: []contracts.DiagnosticSample{{Scope: "fixture", Message: "record normalized"}}}}},
|
||||
}
|
||||
}
|
||||
|
||||
func assertStageNotReused(t *testing.T, stage checkpointStage, fixture filesystemCheckpointFixture, want pipeline.CheckpointReasonCode) {
|
||||
t.Helper()
|
||||
decision := stage.load(fixture)
|
||||
if decision.Reused || decision.ReasonCode != want {
|
||||
t.Fatalf("%s decision=%#v, want non-reused reason code %q", stage.name, decision, want)
|
||||
}
|
||||
}
|
||||
|
||||
func editManifest(t *testing.T, path string, edit func(map[string]any)) {
|
||||
t.Helper()
|
||||
editJSON(t, path, edit)
|
||||
}
|
||||
|
||||
func editJSON(t *testing.T, path string, edit func(map[string]any)) {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var value map[string]any
|
||||
if err := json.Unmarshal(data, &value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
edit(value)
|
||||
data, err = json.Marshal(value)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, data, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func readManifest[T any](t *testing.T, path string) T {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var value T
|
||||
if err := json.Unmarshal(data, &value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func mustRelativePath(t *testing.T, identity Identity) string {
|
||||
t.Helper()
|
||||
return mustRelativePathForTest(identity)
|
||||
}
|
||||
|
||||
func mustRelativePathForTest(identity Identity) string {
|
||||
path, err := identity.RelativePath()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
@@ -1,198 +0,0 @@
|
||||
package checkpoint
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/artifacts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
)
|
||||
|
||||
const digestPrefixLength = 16
|
||||
|
||||
type Fingerprint struct {
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
type IdentityInput struct {
|
||||
Pipeline pipeline.ResolvedPipeline
|
||||
InputKey, RawInputDigest, SourceDigest string
|
||||
SelectedLanes []string
|
||||
RuntimeOverrides []Fingerprint
|
||||
References []artifacts.ReferenceProvenance
|
||||
ProvenanceFingerprints []Fingerprint
|
||||
}
|
||||
type Identity struct {
|
||||
Digest string `json:"digest"`
|
||||
PipelineID string `json:"pipeline_id"`
|
||||
PipelineDigest string `json:"pipeline_digest"`
|
||||
PipelineTopology []string `json:"pipeline_topology,omitempty"`
|
||||
InputKey string `json:"input_key"`
|
||||
RawInputDigest string `json:"raw_input_digest,omitempty"`
|
||||
SourceDigest string `json:"source_digest,omitempty"`
|
||||
SelectedLanes []string `json:"selected_lanes,omitempty"`
|
||||
RuntimeOverrides []Fingerprint `json:"runtime_overrides,omitempty"`
|
||||
ReferenceDigests []Fingerprint `json:"reference_digests,omitempty"`
|
||||
ProvenanceFingerprints []Fingerprint `json:"provenance_fingerprints,omitempty"`
|
||||
}
|
||||
|
||||
func NewIdentity(input IdentityInput) (Identity, error) {
|
||||
pipelineID, pipelineDigest, inputKey := strings.TrimSpace(input.Pipeline.ID), strings.TrimSpace(input.Pipeline.Digest), strings.TrimSpace(input.InputKey)
|
||||
if pipelineID == "" {
|
||||
return Identity{}, fmt.Errorf("checkpoint identity pipeline id must not be empty")
|
||||
}
|
||||
if pipelineDigest == "" {
|
||||
return Identity{}, fmt.Errorf("checkpoint identity pipeline digest must not be empty")
|
||||
}
|
||||
if inputKey == "" {
|
||||
inputKey = strings.TrimSpace(input.Pipeline.Input.Module)
|
||||
}
|
||||
if inputKey == "" {
|
||||
return Identity{}, fmt.Errorf("checkpoint identity input key must not be empty")
|
||||
}
|
||||
if strings.TrimSpace(input.RawInputDigest) == "" && strings.TrimSpace(input.SourceDigest) == "" {
|
||||
return Identity{}, fmt.Errorf("checkpoint identity raw input digest or source digest must be set")
|
||||
}
|
||||
v := Identity{PipelineID: pipelineID, PipelineDigest: pipelineDigest, PipelineTopology: pipelineTopology(input.Pipeline), InputKey: inputKey, RawInputDigest: strings.TrimSpace(input.RawInputDigest), SourceDigest: strings.TrimSpace(input.SourceDigest), SelectedLanes: normalizedLanes(input.SelectedLanes, input.Pipeline.AllArtifactLanes()), RuntimeOverrides: normalizeIdentityFingerprints(input.RuntimeOverrides), ReferenceDigests: referenceFingerprints(input.References), ProvenanceFingerprints: normalizeIdentityFingerprints(input.ProvenanceFingerprints)}
|
||||
data, err := json.Marshal(Identity{PipelineID: v.PipelineID, PipelineDigest: v.PipelineDigest, PipelineTopology: v.PipelineTopology, InputKey: v.InputKey, RawInputDigest: v.RawInputDigest, SourceDigest: v.SourceDigest, SelectedLanes: v.SelectedLanes, RuntimeOverrides: v.RuntimeOverrides, ReferenceDigests: v.ReferenceDigests, ProvenanceFingerprints: v.ProvenanceFingerprints})
|
||||
if err != nil {
|
||||
return Identity{}, fmt.Errorf("marshal checkpoint identity: %w", err)
|
||||
}
|
||||
sum := sha256.Sum256(data)
|
||||
v.Digest = "sha256:" + hex.EncodeToString(sum[:])
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func pipelineTopology(resolved pipeline.ResolvedPipeline) []string {
|
||||
var topology []string
|
||||
for _, step := range resolved.Steps {
|
||||
for _, lane := range step.ArtifactLanes {
|
||||
topology = append(topology, strings.TrimSpace(step.ID)+"\x00"+strings.TrimSpace(lane.ID))
|
||||
}
|
||||
}
|
||||
if len(topology) == 0 {
|
||||
return nil
|
||||
}
|
||||
return topology
|
||||
}
|
||||
func (i Identity) RelativePath() (string, error) {
|
||||
p, err := safeComponent(i.PipelineID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("checkpoint identity pipeline id: %w", err)
|
||||
}
|
||||
k, err := safeComponent(i.InputKey)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("checkpoint identity input key: %w", err)
|
||||
}
|
||||
s := digestPrefix(i.SourceDigest)
|
||||
if s == "" {
|
||||
s = digestPrefix(i.RawInputDigest)
|
||||
}
|
||||
d := digestPrefix(i.PipelineDigest)
|
||||
x := digestPrefix(i.Digest)
|
||||
if s == "" || d == "" || x == "" {
|
||||
return "", fmt.Errorf("checkpoint identity digest prefix must not be empty")
|
||||
}
|
||||
s, err = safeComponent(s)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("checkpoint identity source digest: %w", err)
|
||||
}
|
||||
d, err = safeComponent(d)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("checkpoint identity pipeline digest: %w", err)
|
||||
}
|
||||
x, err = safeComponent(x)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("checkpoint identity digest: %w", err)
|
||||
}
|
||||
return filepath.ToSlash(filepath.Join(p, k+"-"+s, d, x)), nil
|
||||
}
|
||||
func normalizedLanes(selected []string, resolved []pipeline.ResolvedArtifactLane) []string {
|
||||
if len(selected) == 0 {
|
||||
for _, lane := range resolved {
|
||||
selected = append(selected, lane.ID)
|
||||
}
|
||||
}
|
||||
return normalizeStrings(selected)
|
||||
}
|
||||
func normalizeIdentityFingerprints(values []Fingerprint) []Fingerprint {
|
||||
by := map[string]string{}
|
||||
for _, v := range values {
|
||||
if n, x := strings.TrimSpace(v.Name), strings.TrimSpace(v.Value); n != "" && x != "" {
|
||||
by[n] = x
|
||||
}
|
||||
}
|
||||
names := make([]string, 0, len(by))
|
||||
for n := range by {
|
||||
names = append(names, n)
|
||||
}
|
||||
sort.Strings(names)
|
||||
out := make([]Fingerprint, 0, len(names))
|
||||
for _, n := range names {
|
||||
out = append(out, Fingerprint{Name: n, Value: by[n]})
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
func referenceFingerprints(refs []artifacts.ReferenceProvenance) []Fingerprint {
|
||||
var values []Fingerprint
|
||||
for _, r := range refs {
|
||||
if d := strings.TrimSpace(r.Digest); d != "" {
|
||||
values = append(values, Fingerprint{Name: strings.Join([]string{strings.TrimSpace(r.Stage), strings.TrimSpace(r.LaneID), strings.TrimSpace(r.SlotName), strings.TrimSpace(r.OriginURI)}, ":"), Value: d})
|
||||
}
|
||||
}
|
||||
return normalizeIdentityFingerprints(values)
|
||||
}
|
||||
func normalizeStrings(values []string) []string {
|
||||
seen := map[string]struct{}{}
|
||||
for _, v := range values {
|
||||
if v = strings.TrimSpace(v); v != "" {
|
||||
seen[v] = struct{}{}
|
||||
}
|
||||
}
|
||||
out := make([]string, 0, len(seen))
|
||||
for v := range seen {
|
||||
out = append(out, v)
|
||||
}
|
||||
sort.Strings(out)
|
||||
if len(out) == 0 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
func digestPrefix(v string) string {
|
||||
v = strings.TrimSpace(v)
|
||||
if n := strings.Index(v, ":"); n >= 0 {
|
||||
v = v[n+1:]
|
||||
}
|
||||
if len(v) > digestPrefixLength {
|
||||
return v[:digestPrefixLength]
|
||||
}
|
||||
return v
|
||||
}
|
||||
func safeComponent(v string) (string, error) {
|
||||
v = strings.TrimSpace(v)
|
||||
if v == "" {
|
||||
return "", fmt.Errorf("must not be empty")
|
||||
}
|
||||
var b strings.Builder
|
||||
for _, r := range v {
|
||||
if r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '-' || r == '_' || r == '.' {
|
||||
b.WriteRune(r)
|
||||
} else {
|
||||
b.WriteString(fmt.Sprintf("~%x", r))
|
||||
}
|
||||
}
|
||||
out := b.String()
|
||||
if out == "." || out == ".." || strings.Contains(out, "..") || strings.ContainsAny(out, `/\\`) {
|
||||
return "", fmt.Errorf("%q is not filesystem safe", v)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -1,213 +0,0 @@
|
||||
package checkpoint
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/artifacts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
)
|
||||
|
||||
func TestNewIdentityNormalizesOrderAndEmptyValues(t *testing.T) {
|
||||
base := representativeIdentityInput()
|
||||
identity, err := NewIdentity(base)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
mutate func(*IdentityInput)
|
||||
}{
|
||||
{"selected lanes", func(v *IdentityInput) { v.SelectedLanes = []string{"lane-a", "lane-b"} }},
|
||||
{"runtime fingerprints", func(v *IdentityInput) {
|
||||
v.RuntimeOverrides = []Fingerprint{{Name: "model", Value: "large"}, {Name: "timeout", Value: "30s"}}
|
||||
}},
|
||||
{"references", func(v *IdentityInput) {
|
||||
v.References = []artifacts.ReferenceProvenance{v.References[1], v.References[0]}
|
||||
}},
|
||||
{"provenance fingerprints", func(v *IdentityInput) {
|
||||
v.ProvenanceFingerprints = []Fingerprint{{Name: "source", Value: "v2"}, {Name: "runner", Value: "v1"}}
|
||||
}},
|
||||
{"resolved lanes", func(v *IdentityInput) {
|
||||
steps := append([]pipeline.ResolvedPipelineStep(nil), v.Pipeline.Steps...)
|
||||
steps[0].ArtifactLanes = []pipeline.ResolvedArtifactLane{steps[0].ArtifactLanes[1], steps[0].ArtifactLanes[0]}
|
||||
v.Pipeline.Steps = steps
|
||||
}},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
changed := cloneIdentityInput(base)
|
||||
tt.mutate(&changed)
|
||||
got, err := NewIdentity(changed)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tt.name == "resolved lanes" {
|
||||
if reflect.DeepEqual(identity, got) {
|
||||
t.Fatal("reordered topology did not change checkpoint identity")
|
||||
}
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(identity, got) {
|
||||
t.Fatalf("reordered identity differs:\nbase=%#v\ngot=%#v", identity, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("duplicates and blanks are ignored", func(t *testing.T) {
|
||||
changed := base
|
||||
changed.SelectedLanes = []string{" ", "lane-b", "lane-a", "lane-a", ""}
|
||||
changed.RuntimeOverrides = append(changed.RuntimeOverrides, Fingerprint{}, Fingerprint{Name: " ", Value: "ignored"}, Fingerprint{Name: "timeout", Value: "30s"})
|
||||
changed.ProvenanceFingerprints = append(changed.ProvenanceFingerprints, Fingerprint{}, Fingerprint{Name: "", Value: "ignored"})
|
||||
|
||||
got, err := NewIdentity(changed)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(identity, got) {
|
||||
t.Fatalf("empty or duplicate values changed identity:\nbase=%#v\ngot=%#v", identity, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewIdentityChangesForMeaningfulInputs(t *testing.T) {
|
||||
base := representativeIdentityInput()
|
||||
original, err := NewIdentity(base)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cases := map[string]func(*IdentityInput){
|
||||
"pipeline id": func(v *IdentityInput) { v.Pipeline.ID = "another-pipeline" },
|
||||
"pipeline digest": func(v *IdentityInput) { v.Pipeline.Digest = "sha256:pipeline-digest-2" },
|
||||
"input key": func(v *IdentityInput) { v.InputKey = "another-input" },
|
||||
"raw input digest": func(v *IdentityInput) { v.RawInputDigest = "sha256:raw-input-2" },
|
||||
"source digest": func(v *IdentityInput) { v.SourceDigest = "sha256:source-2" },
|
||||
"selected lanes": func(v *IdentityInput) { v.SelectedLanes = []string{"lane-a"} },
|
||||
"runtime override": func(v *IdentityInput) { v.RuntimeOverrides[0].Value = "60s" },
|
||||
"reference digest": func(v *IdentityInput) { v.References[0].Digest = "sha256:reference-2" },
|
||||
"reference identity": func(v *IdentityInput) { v.References[0].OriginURI = "file:///other-reference" },
|
||||
"provenance": func(v *IdentityInput) { v.ProvenanceFingerprints[0].Value = "v3" },
|
||||
}
|
||||
|
||||
for name, mutate := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
changed := cloneIdentityInput(base)
|
||||
mutate(&changed)
|
||||
got, err := NewIdentity(changed)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Digest == original.Digest {
|
||||
t.Fatalf("meaningful %s input did not change digest %q", name, got.Digest)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewIdentityUsesResolvedInputWhenKeyIsOmitted(t *testing.T) {
|
||||
input := representativeIdentityInput()
|
||||
input.InputKey = ""
|
||||
|
||||
identity, err := NewIdentity(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if identity.InputKey != input.Pipeline.Input.Module {
|
||||
t.Fatalf("input key = %q, want resolved module %q", identity.InputKey, input.Pipeline.Input.Module)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewIdentityRejectsMissingRequiredInputs(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
mutate func(*IdentityInput)
|
||||
want string
|
||||
}{
|
||||
{"pipeline id", func(v *IdentityInput) { v.Pipeline.ID = "" }, "pipeline id"},
|
||||
{"pipeline digest", func(v *IdentityInput) { v.Pipeline.Digest = "" }, "pipeline digest"},
|
||||
{"input key", func(v *IdentityInput) { v.InputKey = ""; v.Pipeline.Input = pipeline.Binding("") }, "input key"},
|
||||
{"input digests", func(v *IdentityInput) { v.RawInputDigest = ""; v.SourceDigest = "" }, "raw input digest or source digest"},
|
||||
}
|
||||
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
input := representativeIdentityInput()
|
||||
tt.mutate(&input)
|
||||
_, err := NewIdentity(input)
|
||||
if err == nil || !strings.Contains(err.Error(), tt.want) {
|
||||
t.Fatalf("error = %v, want category containing %q", err, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdentityRelativePathIsDeterministicAndConfined(t *testing.T) {
|
||||
identity, err := NewIdentity(representativeIdentityInput())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
first, err := identity.RelativePath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := identity.RelativePath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first != second {
|
||||
t.Fatalf("relative path is not deterministic: %q != %q", first, second)
|
||||
}
|
||||
if filepath.IsAbs(first) || filepath.Clean(first) != first || filepath.ToSlash(first) != first {
|
||||
t.Fatalf("path is not a clean relative slash-separated path: %q", first)
|
||||
}
|
||||
if strings.Contains(first, "../") || strings.HasPrefix(first, "../") || strings.Contains(first, `\\`) {
|
||||
t.Fatalf("path escapes its root: %q", first)
|
||||
}
|
||||
parts := strings.Split(first, "/")
|
||||
if len(parts) != 4 || parts[0] != "pipeline" || !strings.HasPrefix(parts[1], "input-") {
|
||||
t.Fatalf("path does not contain the documented identity hierarchy: %q", first)
|
||||
}
|
||||
if !strings.Contains(parts[1], "source-digest") || !strings.Contains(parts[2], "pipeline-digest") || parts[3] == "" {
|
||||
t.Fatalf("path omits digest-derived hierarchy: %q", first)
|
||||
}
|
||||
}
|
||||
|
||||
func representativeIdentityInput() IdentityInput {
|
||||
return IdentityInput{
|
||||
Pipeline: pipeline.ResolvedPipeline{
|
||||
ID: "pipeline",
|
||||
Digest: "sha256:pipeline-digest-000000000000",
|
||||
Input: pipeline.Binding("input"),
|
||||
Steps: []pipeline.ResolvedPipelineStep{{
|
||||
ID: "default",
|
||||
ArtifactLanes: []pipeline.ResolvedArtifactLane{
|
||||
{ID: "lane-b"},
|
||||
{ID: "lane-a"},
|
||||
},
|
||||
}},
|
||||
},
|
||||
InputKey: "input",
|
||||
RawInputDigest: "sha256:raw-input-000000000000",
|
||||
SourceDigest: "sha256:source-digest-000000000000",
|
||||
SelectedLanes: []string{"lane-b", "lane-a"},
|
||||
RuntimeOverrides: []Fingerprint{{Name: "timeout", Value: "30s"}, {Name: "model", Value: "large"}},
|
||||
References: []artifacts.ReferenceProvenance{
|
||||
{Stage: "chunk", SlotName: "glossary", OriginURI: "file:///glossary", Digest: "sha256:reference-1"},
|
||||
{Stage: "extract", LaneID: "lane-a", SlotName: "party", OriginURI: "file:///party", Digest: "sha256:reference-2"},
|
||||
},
|
||||
ProvenanceFingerprints: []Fingerprint{{Name: "runner", Value: "v1"}, {Name: "source", Value: "v2"}},
|
||||
}
|
||||
}
|
||||
|
||||
func cloneIdentityInput(input IdentityInput) IdentityInput {
|
||||
input.SelectedLanes = append([]string(nil), input.SelectedLanes...)
|
||||
input.RuntimeOverrides = append([]Fingerprint(nil), input.RuntimeOverrides...)
|
||||
input.References = append([]artifacts.ReferenceProvenance(nil), input.References...)
|
||||
input.ProvenanceFingerprints = append([]Fingerprint(nil), input.ProvenanceFingerprints...)
|
||||
input.Pipeline.Steps[0].ArtifactLanes = append([]pipeline.ResolvedArtifactLane(nil), input.Pipeline.Steps[0].ArtifactLanes...)
|
||||
return input
|
||||
}
|
||||
@@ -1,358 +0,0 @@
|
||||
package checkpoint
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/fileio"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
)
|
||||
|
||||
type FilesystemLoader struct {
|
||||
root string
|
||||
identityDigest string
|
||||
}
|
||||
|
||||
func NewFilesystemLoader(root string, identity Identity) (pipeline.CheckpointLoader, error) {
|
||||
root = strings.TrimSpace(root)
|
||||
if root == "" {
|
||||
return pipeline.NoopCheckpointLoader(), nil
|
||||
}
|
||||
relative, err := identity.RelativePath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
target, err := fileio.SafePath(root, relative)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &FilesystemLoader{root: target, identityDigest: identity.Digest}, nil
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) Enabled() bool {
|
||||
return l != nil && strings.TrimSpace(l.root) != ""
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) Source(moduleKey string) (pipeline.SourceCheckpoint, pipeline.CheckpointDecision) {
|
||||
var manifest SourceManifest
|
||||
if decision := l.readJSON("source/manifest.json", &manifest); !decision.Reused {
|
||||
return pipeline.SourceCheckpoint{}, decision
|
||||
}
|
||||
if decision := l.validateManifest(manifest.StageManifest, StageSource, "", moduleKey, StatusSucceeded, nil); !decision.Reused {
|
||||
return pipeline.SourceCheckpoint{}, decision
|
||||
}
|
||||
var payload sourceDocumentEnvelope
|
||||
if decision := l.readJSON("source/source-document.json", &payload); !decision.Reused {
|
||||
return pipeline.SourceCheckpoint{}, decision
|
||||
}
|
||||
doc := cloneSourceDocument(payload.Document)
|
||||
if err := source.ValidateDocument(&doc); err != nil {
|
||||
return pipeline.SourceCheckpoint{}, decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonArtifactPayloadInvalid)
|
||||
}
|
||||
if strings.TrimSpace(manifest.SourceID) != "" && manifest.SourceID != doc.ID {
|
||||
return pipeline.SourceCheckpoint{}, decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonArtifactPayloadInvalid)
|
||||
}
|
||||
if !fingerprintsEqual(checkpointToPipelineFingerprints(manifest.OutputDigests), digestFingerprints("source_document", doc.Digest)) {
|
||||
return pipeline.SourceCheckpoint{}, decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonArtifactDigestMismatch)
|
||||
}
|
||||
return pipeline.SourceCheckpoint{Document: &doc}, reusedDecision()
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) Extract(laneID, moduleKey string, dependencies []pipeline.CheckpointFingerprint) (pipeline.ExtractCheckpoint, pipeline.CheckpointDecision) {
|
||||
return l.ExtractForStep("", laneID, moduleKey, dependencies)
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) ExtractForStep(stepID, laneID, moduleKey string, dependencies []pipeline.CheckpointFingerprint) (pipeline.ExtractCheckpoint, pipeline.CheckpointDecision) {
|
||||
var manifest ExtractLaneManifest
|
||||
if d := l.readJSON(laneManifestPath("extract", stepID, laneID), &manifest); !d.Reused {
|
||||
return pipeline.ExtractCheckpoint{}, d
|
||||
}
|
||||
if d := l.validateLaneManifest(manifest.StageManifest, StageExtract, stepID, laneID, moduleKey, dependencies, StatusSucceeded, StatusSucceededWithRejections); !d.Reused {
|
||||
return pipeline.ExtractCheckpoint{}, d
|
||||
}
|
||||
var payload artifactExtractEnvelope
|
||||
if d := l.readJSON(lanePayloadPath("extract", stepID, laneID, "outputs.json"), &payload); !d.Reused {
|
||||
return pipeline.ExtractCheckpoint{}, d
|
||||
}
|
||||
outputs, err := artifactCheckpointOutputs(payload.Outputs)
|
||||
if err != nil {
|
||||
return pipeline.ExtractCheckpoint{}, artifactDecision(err, "extract checkpoint artifact is invalid")
|
||||
}
|
||||
if !fingerprintsEqual(checkpointToPipelineFingerprints(manifest.OutputDigests), artifactOutputDigests(outputs)) {
|
||||
return pipeline.ExtractCheckpoint{}, decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonArtifactDigestMismatch)
|
||||
}
|
||||
return pipeline.ExtractCheckpoint{Outputs: outputs, Rejected: cloneRejectedOutputs(payload.Rejected)}, reusedDecision()
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) Merge(laneID, moduleKey string, dependencies []pipeline.CheckpointFingerprint) (pipeline.MergeCheckpoint, pipeline.CheckpointDecision) {
|
||||
return l.MergeForStep("", laneID, moduleKey, dependencies)
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) MergeForStep(stepID, laneID, moduleKey string, dependencies []pipeline.CheckpointFingerprint) (pipeline.MergeCheckpoint, pipeline.CheckpointDecision) {
|
||||
var manifest MergeLaneManifest
|
||||
if d := l.readJSON(laneManifestPath("merge", stepID, laneID), &manifest); !d.Reused {
|
||||
return pipeline.MergeCheckpoint{}, d
|
||||
}
|
||||
if d := l.validateLaneManifest(manifest.StageManifest, StageMerge, stepID, laneID, moduleKey, dependencies, StatusSucceeded); !d.Reused {
|
||||
return pipeline.MergeCheckpoint{}, d
|
||||
}
|
||||
var payload artifactSingleEnvelope
|
||||
if d := l.readJSON(lanePayloadPath("merge", stepID, laneID, "output.json"), &payload); !d.Reused {
|
||||
return pipeline.MergeCheckpoint{}, d
|
||||
}
|
||||
values, err := artifactCheckpointOutputs([]artifactCheckpointEnvelope{payload.Output})
|
||||
if err != nil {
|
||||
return pipeline.MergeCheckpoint{}, artifactDecision(err, "merge checkpoint artifact is invalid")
|
||||
}
|
||||
if !fingerprintsEqual(checkpointToPipelineFingerprints(manifest.OutputDigests), artifactOutputDigests(values)) {
|
||||
return pipeline.MergeCheckpoint{}, decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonArtifactDigestMismatch)
|
||||
}
|
||||
return pipeline.MergeCheckpoint{Output: values[0]}, reusedDecision()
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) Normalize(laneID, moduleKey string, dependencies []pipeline.CheckpointFingerprint) (pipeline.NormalizeCheckpoint, pipeline.CheckpointDecision) {
|
||||
return l.NormalizeForStep("", laneID, moduleKey, dependencies)
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) NormalizeForStep(stepID, laneID, moduleKey string, dependencies []pipeline.CheckpointFingerprint) (pipeline.NormalizeCheckpoint, pipeline.CheckpointDecision) {
|
||||
var manifest NormalizeLaneManifest
|
||||
if d := l.readJSON(laneManifestPath("normalize", stepID, laneID), &manifest); !d.Reused {
|
||||
return pipeline.NormalizeCheckpoint{}, d
|
||||
}
|
||||
if d := l.validateLaneManifest(manifest.StageManifest, StageNormalize, stepID, laneID, moduleKey, dependencies, StatusSucceeded); !d.Reused {
|
||||
return pipeline.NormalizeCheckpoint{}, d
|
||||
}
|
||||
var payload artifactSingleEnvelope
|
||||
if d := l.readJSON(lanePayloadPath("normalize", stepID, laneID, "output.json"), &payload); !d.Reused {
|
||||
return pipeline.NormalizeCheckpoint{}, d
|
||||
}
|
||||
values, err := artifactCheckpointOutputs([]artifactCheckpointEnvelope{payload.Output})
|
||||
if err != nil {
|
||||
return pipeline.NormalizeCheckpoint{}, artifactDecision(err, "normalize checkpoint artifact is invalid")
|
||||
}
|
||||
if !fingerprintsEqual(checkpointToPipelineFingerprints(manifest.OutputDigests), artifactOutputDigests(values)) {
|
||||
return pipeline.NormalizeCheckpoint{}, decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonArtifactDigestMismatch)
|
||||
}
|
||||
return pipeline.NormalizeCheckpoint{Output: values[0]}, reusedDecision()
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) AcceptedNormalize(stepID, laneID, moduleKey string) (pipeline.NormalizeCheckpoint, pipeline.CheckpointDecision) {
|
||||
var manifest NormalizeLaneManifest
|
||||
if d := l.readJSON(laneManifestPath("normalize", stepID, laneID), &manifest); !d.Reused {
|
||||
return pipeline.NormalizeCheckpoint{}, d
|
||||
}
|
||||
if d := l.validateAcceptedNormalizeManifest(manifest.StageManifest, stepID, laneID, moduleKey); !d.Reused {
|
||||
return pipeline.NormalizeCheckpoint{}, d
|
||||
}
|
||||
var payload artifactSingleEnvelope
|
||||
if d := l.readJSON(lanePayloadPath("normalize", stepID, laneID, "output.json"), &payload); !d.Reused {
|
||||
return pipeline.NormalizeCheckpoint{}, d
|
||||
}
|
||||
values, err := artifactCheckpointOutputs([]artifactCheckpointEnvelope{payload.Output})
|
||||
if err != nil {
|
||||
return pipeline.NormalizeCheckpoint{}, artifactDecision(err, "accepted normalize checkpoint artifact is invalid")
|
||||
}
|
||||
if len(values) != 1 {
|
||||
return pipeline.NormalizeCheckpoint{}, decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonArtifactPayloadInvalid)
|
||||
}
|
||||
if !fingerprintsEqual(checkpointToPipelineFingerprints(manifest.OutputDigests), artifactOutputDigests(values)) {
|
||||
return pipeline.NormalizeCheckpoint{}, decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonArtifactDigestMismatch)
|
||||
}
|
||||
return pipeline.NormalizeCheckpoint{Output: values[0]}, decision(pipeline.CheckpointDecisionReused, pipeline.CheckpointReasonAcceptedArtifactReused)
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) validateAcceptedNormalizeManifest(manifest StageManifest, stepID, laneID, moduleKey string) pipeline.CheckpointDecision {
|
||||
if manifest.WorkspaceSchemaVersion != WorkspaceSchemaVersion {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonWorkspaceSchemaIncompatible)
|
||||
}
|
||||
identity := strings.TrimSpace(l.identityDigest)
|
||||
if identity == "" || strings.TrimSpace(manifest.Metadata["checkpoint_identity_digest"]) == "" || manifest.Metadata["checkpoint_identity_digest"] != identity {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonIdentityMismatch)
|
||||
}
|
||||
if manifest.Stage != StageNormalize {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonStageMismatch)
|
||||
}
|
||||
if strings.TrimSpace(stepID) == "" || manifest.StepID != stepID {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonStepMismatch)
|
||||
}
|
||||
if strings.TrimSpace(laneID) == "" || manifest.LaneID != laneID {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonLaneMismatch)
|
||||
}
|
||||
if strings.TrimSpace(moduleKey) == "" || manifest.ModuleKey != moduleKey {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonModuleMismatch)
|
||||
}
|
||||
if manifest.Status != StatusSucceeded {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonStatusNotReusable)
|
||||
}
|
||||
return reusedDecision()
|
||||
}
|
||||
|
||||
func artifactCheckpointOutputs(values []artifactCheckpointEnvelope) ([]pipeline.CheckpointArtifact, error) {
|
||||
if len(values) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
out := make([]pipeline.CheckpointArtifact, 0, len(values))
|
||||
for _, v := range values {
|
||||
content, err := contentFromEnvelope(v.Content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.TrimSpace(string(v.Kind)) == "" || strings.TrimSpace(v.Schema.ID) == "" || strings.TrimSpace(v.Schema.Version) == "" || strings.TrimSpace(v.SchemaDigest) == "" {
|
||||
return nil, &artifactPayloadError{code: pipeline.CheckpointReasonArtifactCodecIncompatible, err: fmt.Errorf("artifact codec identity is incomplete")}
|
||||
}
|
||||
diagnostics, err := cloneAndValidateCheckpointDiagnostics(v.Diagnostics)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, pipeline.CheckpointArtifact{LaneID: v.LaneID, ModuleKey: v.ModuleKey, SourceID: v.SourceID, ChunkID: v.ChunkID, ChunkIndex: v.ChunkIndex, ChunkRef: v.ChunkRef, SchemaDigest: v.SchemaDigest, Artifact: contracts.SerializedArtifact{Kind: v.Kind, Schema: v.Schema, MediaType: v.Content.MediaType, Content: content, Metadata: cloneMetadata(v.Content.Metadata)}, Diagnostics: diagnostics})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func cloneAndValidateCheckpointDiagnostics(values []pipeline.CheckpointDiagnostic) ([]pipeline.CheckpointDiagnostic, error) {
|
||||
if len(values) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
diagnostics := make([]contracts.ProducerDiagnostic, len(values))
|
||||
for index, value := range values {
|
||||
diagnostics[index] = value.Diagnostic
|
||||
}
|
||||
if err := contracts.ValidateProducerDiagnostics(diagnostics); err != nil {
|
||||
return nil, &artifactPayloadError{code: pipeline.CheckpointReasonArtifactPayloadInvalid, err: fmt.Errorf("checkpoint diagnostics: %w", err)}
|
||||
}
|
||||
cloned := make([]pipeline.CheckpointDiagnostic, len(values))
|
||||
for index, value := range values {
|
||||
cloned[index] = pipeline.CheckpointDiagnostic{Diagnostic: contracts.CloneProducerDiagnostics([]contracts.ProducerDiagnostic{value.Diagnostic})[0], ValidatorKey: value.ValidatorKey}
|
||||
}
|
||||
return cloned, nil
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) readJSON(name string, out any) pipeline.CheckpointDecision {
|
||||
if !l.Enabled() {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonLoadingDisabled)
|
||||
}
|
||||
target, err := fileio.SafePath(l.root, name)
|
||||
if err != nil {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonPathInvalid)
|
||||
}
|
||||
data, err := os.ReadFile(target)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonMissing)
|
||||
}
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonReadFailed)
|
||||
}
|
||||
if err := json.Unmarshal(data, out); err != nil {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonDecodeFailed)
|
||||
}
|
||||
return reusedDecision()
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) validateManifest(manifest StageManifest, stage StageName, laneID string, moduleKey string, status StageStatus, dependencies []pipeline.CheckpointFingerprint) pipeline.CheckpointDecision {
|
||||
return l.validateLaneManifest(manifest, stage, "", laneID, moduleKey, dependencies, status)
|
||||
}
|
||||
|
||||
func (l *FilesystemLoader) validateLaneManifest(manifest StageManifest, stage StageName, stepID string, laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint, statuses ...StageStatus) pipeline.CheckpointDecision {
|
||||
if manifest.WorkspaceSchemaVersion == WorkspaceSchemaVersionV1 {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonWorkspaceSchemaIncompatible)
|
||||
}
|
||||
if manifest.WorkspaceSchemaVersion == WorkspaceSchemaVersionV2 {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonWorkspaceSchemaIncompatible)
|
||||
}
|
||||
if manifest.WorkspaceSchemaVersion != WorkspaceSchemaVersion {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonWorkspaceSchemaIncompatible)
|
||||
}
|
||||
if strings.TrimSpace(l.identityDigest) != "" && manifest.Metadata["checkpoint_identity_digest"] != l.identityDigest {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonIdentityMismatch)
|
||||
}
|
||||
if manifest.Stage != stage {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonStageMismatch)
|
||||
}
|
||||
if strings.TrimSpace(stepID) != "" && manifest.StepID != stepID {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonStepMismatch)
|
||||
}
|
||||
if strings.TrimSpace(laneID) != "" && manifest.LaneID != laneID {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonLaneMismatch)
|
||||
}
|
||||
if strings.TrimSpace(moduleKey) != "" && manifest.ModuleKey != moduleKey {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonModuleMismatch)
|
||||
}
|
||||
statusOK := false
|
||||
for _, status := range statuses {
|
||||
if manifest.Status == status {
|
||||
statusOK = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !statusOK {
|
||||
return decision(pipeline.CheckpointDecisionExecuted, pipeline.CheckpointReasonStatusNotReusable)
|
||||
}
|
||||
if !fingerprintsEqual(checkpointToPipelineFingerprints(manifest.DependencyFingerprints), dependencies) {
|
||||
return decision(pipeline.CheckpointDecisionDependencyInvalidated, pipeline.CheckpointReasonDependencyMismatch)
|
||||
}
|
||||
return reusedDecision()
|
||||
}
|
||||
|
||||
func contentFromEnvelope(value binaryEnvelope) ([]byte, error) {
|
||||
content, err := base64.StdEncoding.DecodeString(value.ContentBase64)
|
||||
if err != nil {
|
||||
return nil, &artifactPayloadError{code: pipeline.CheckpointReasonArtifactPayloadInvalid, err: fmt.Errorf("decode content_base64: %w", err)}
|
||||
}
|
||||
if digest := strings.TrimSpace(value.ContentDigest); digest != "" && digest != contentDigest(content) {
|
||||
return nil, &artifactPayloadError{code: pipeline.CheckpointReasonArtifactDigestMismatch, err: fmt.Errorf("content digest mismatch")}
|
||||
}
|
||||
return content, nil
|
||||
}
|
||||
|
||||
func checkpointToPipelineFingerprints(values []Fingerprint) []pipeline.CheckpointFingerprint {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]pipeline.CheckpointFingerprint, 0, len(values))
|
||||
for _, value := range values {
|
||||
out = append(out, pipeline.CheckpointFingerprint{Name: value.Name, Value: value.Value})
|
||||
}
|
||||
return normalizeFingerprints(out)
|
||||
}
|
||||
|
||||
func fingerprintsEqual(a []pipeline.CheckpointFingerprint, b []pipeline.CheckpointFingerprint) bool {
|
||||
a = normalizeFingerprints(a)
|
||||
b = normalizeFingerprints(b)
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func reusedDecision() pipeline.CheckpointDecision {
|
||||
return decision(pipeline.CheckpointDecisionReused, pipeline.CheckpointReasonReused)
|
||||
}
|
||||
|
||||
func decision(category pipeline.CheckpointDecisionCategory, code pipeline.CheckpointReasonCode) pipeline.CheckpointDecision {
|
||||
return pipeline.NewCheckpointDecision(category, code)
|
||||
}
|
||||
|
||||
type artifactPayloadError struct {
|
||||
code pipeline.CheckpointReasonCode
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *artifactPayloadError) Error() string { return e.err.Error() }
|
||||
|
||||
func artifactDecision(err error, detail string) pipeline.CheckpointDecision {
|
||||
code := pipeline.CheckpointReasonArtifactPayloadInvalid
|
||||
if payloadErr, ok := err.(*artifactPayloadError); ok {
|
||||
code = payloadErr.code
|
||||
}
|
||||
return decision(pipeline.CheckpointDecisionExecuted, code)
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package checkpoint
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
WorkspaceSchemaVersion = "notarius.workspace.v4"
|
||||
WorkspaceSchemaVersionV3 = "notarius.workspace.v3"
|
||||
WorkspaceSchemaVersionV2 = "notarius.workspace.v2"
|
||||
WorkspaceSchemaVersionV1 = "notarius.workspace.v1"
|
||||
)
|
||||
|
||||
type StageName string
|
||||
|
||||
const (
|
||||
StageSource StageName = "source"
|
||||
StageExtract StageName = "extract"
|
||||
StageMerge StageName = "merge"
|
||||
StageNormalize StageName = "normalize"
|
||||
)
|
||||
|
||||
type StageStatus string
|
||||
|
||||
const (
|
||||
StatusPending StageStatus = "pending"
|
||||
StatusRunning StageStatus = "running"
|
||||
StatusSucceeded StageStatus = "succeeded"
|
||||
StatusSucceededWithRejections StageStatus = "succeeded_with_rejections"
|
||||
StatusFailed StageStatus = "failed"
|
||||
StatusInvalidated StageStatus = "invalidated"
|
||||
)
|
||||
|
||||
type StageManifest struct {
|
||||
WorkspaceSchemaVersion string `json:"workspace_schema_version"`
|
||||
Stage StageName `json:"stage"`
|
||||
StepID string `json:"step_id,omitempty"`
|
||||
LaneID string `json:"lane_id,omitempty"`
|
||||
ModuleKey string `json:"module_key,omitempty"`
|
||||
DependencyFingerprints []Fingerprint `json:"dependency_fingerprints,omitempty"`
|
||||
Status StageStatus `json:"status"`
|
||||
OutputDigests []Fingerprint `json:"output_digests,omitempty"`
|
||||
ValidationStatus string `json:"validation_status,omitempty"`
|
||||
Rejections []RejectionSummary `json:"rejections,omitempty"`
|
||||
StartedAt *time.Time `json:"started_at,omitempty"`
|
||||
CompletedAt *time.Time `json:"completed_at,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
}
|
||||
type RejectionSummary struct {
|
||||
ValidatorName string `json:"validator_name,omitempty"`
|
||||
ReasonCode string `json:"reason_code,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Count int `json:"count,omitempty"`
|
||||
}
|
||||
type SourceManifest struct {
|
||||
StageManifest
|
||||
SourceID string `json:"source_id,omitempty"`
|
||||
}
|
||||
type ExtractLaneManifest struct {
|
||||
StageManifest
|
||||
ChunkCount int `json:"chunk_count,omitempty"`
|
||||
OutputCount int `json:"output_count,omitempty"`
|
||||
}
|
||||
type MergeLaneManifest struct {
|
||||
StageManifest
|
||||
InputCount int `json:"input_count,omitempty"`
|
||||
}
|
||||
type NormalizeLaneManifest struct {
|
||||
StageManifest
|
||||
InputCount int `json:"input_count,omitempty"`
|
||||
}
|
||||
|
||||
func NewStageManifest(stage StageName, status StageStatus) StageManifest {
|
||||
return StageManifest{WorkspaceSchemaVersion: WorkspaceSchemaVersion, Stage: stage, Status: status}
|
||||
}
|
||||
@@ -1,500 +0,0 @@
|
||||
package checkpoint
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/artifacts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/fileio"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
)
|
||||
|
||||
type FilesystemRecorder struct {
|
||||
root string
|
||||
identityDigest string
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func NewFilesystemRecorder(root string, identity Identity) (pipeline.CheckpointRecorder, error) {
|
||||
root = strings.TrimSpace(root)
|
||||
if root == "" {
|
||||
return pipeline.NoopCheckpointRecorder(), nil
|
||||
}
|
||||
relative, err := identity.RelativePath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
target, err := fileio.SafePath(root, relative)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &FilesystemRecorder{root: target, identityDigest: identity.Digest, now: time.Now}, nil
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) SourceRunning(moduleKey string) error {
|
||||
manifest := r.newStageManifest(StageSource, StatusRunning)
|
||||
manifest.ModuleKey = moduleKey
|
||||
manifest.StartedAt = timePtr(r.timestamp())
|
||||
return r.writeManifest("source/manifest.json", SourceManifest{StageManifest: manifest})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) SourceSucceeded(moduleKey string, doc *source.SourceDocument) error {
|
||||
if doc == nil {
|
||||
return fmt.Errorf("checkpoint source document must not be nil")
|
||||
}
|
||||
if err := r.writePayload("source/source-document.json", sourceDocumentEnvelope{Document: cloneSourceDocument(*doc)}); err != nil {
|
||||
return err
|
||||
}
|
||||
manifest := r.newStageManifest(StageSource, StatusSucceeded)
|
||||
manifest.ModuleKey = moduleKey
|
||||
manifest.OutputDigests = checkpointFingerprints(digestFingerprints("source_document", doc.Digest))
|
||||
manifest.CompletedAt = timePtr(r.timestamp())
|
||||
return r.writeManifest("source/manifest.json", SourceManifest{
|
||||
StageManifest: manifest,
|
||||
SourceID: doc.ID,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) SourceFailed(moduleKey string, err error) error {
|
||||
manifest := r.newStageManifest(StageSource, StatusFailed)
|
||||
manifest.ModuleKey = moduleKey
|
||||
manifest.CompletedAt = timePtr(r.timestamp())
|
||||
manifest.Metadata = errorMetadata(err)
|
||||
return r.writeManifest("source/manifest.json", SourceManifest{StageManifest: manifest})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) ExtractRunning(laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint) error {
|
||||
return r.ExtractRunningForStep("", laneID, moduleKey, dependencies)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) ExtractRunningForStep(stepID, laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint) error {
|
||||
manifest := r.laneManifest(StageExtract, StatusRunning, stepID, laneID, moduleKey, dependencies)
|
||||
manifest.StartedAt = timePtr(r.timestamp())
|
||||
return r.writeManifest(laneManifestPath("extract", stepID, laneID), ExtractLaneManifest{StageManifest: manifest})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) ExtractSucceeded(laneID, moduleKey string, dependencies []pipeline.CheckpointFingerprint, outputs []pipeline.CheckpointArtifact, rejected []contracts.RejectedOutput) error {
|
||||
return r.ExtractSucceededForStep("", laneID, moduleKey, dependencies, outputs, rejected)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) ExtractSucceededForStep(stepID, laneID, moduleKey string, dependencies []pipeline.CheckpointFingerprint, outputs []pipeline.CheckpointArtifact, rejected []contracts.RejectedOutput) error {
|
||||
payload := artifactExtractEnvelope{Outputs: artifactCheckpointEnvelopes(outputs), Rejected: cloneRejectedOutputs(rejected)}
|
||||
if err := r.writePayload(lanePayloadPath("extract", stepID, laneID, "outputs.json"), payload); err != nil {
|
||||
return err
|
||||
}
|
||||
manifest := r.laneManifest(StageExtract, statusForRejected(rejected), stepID, laneID, moduleKey, dependencies)
|
||||
manifest.OutputDigests = checkpointFingerprints(artifactOutputDigests(outputs))
|
||||
manifest.ValidationStatus = validationStatusString(rejected)
|
||||
manifest.Rejections = rejectionSummaries(rejected)
|
||||
manifest.CompletedAt = timePtr(r.timestamp())
|
||||
return r.writeManifest(laneManifestPath("extract", stepID, laneID), ExtractLaneManifest{StageManifest: manifest, ChunkCount: len(outputs) + len(rejected), OutputCount: len(outputs)})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) ExtractFailed(laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint, err error) error {
|
||||
return r.ExtractFailedForStep("", laneID, moduleKey, dependencies, err)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) ExtractFailedForStep(stepID, laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint, err error) error {
|
||||
manifest := r.laneManifest(StageExtract, StatusFailed, stepID, laneID, moduleKey, dependencies)
|
||||
manifest.CompletedAt = timePtr(r.timestamp())
|
||||
manifest.Metadata = errorMetadata(err)
|
||||
return r.writeManifest(laneManifestPath("extract", stepID, laneID), ExtractLaneManifest{StageManifest: manifest})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) MergeRunning(laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint) error {
|
||||
return r.MergeRunningForStep("", laneID, moduleKey, dependencies)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) MergeRunningForStep(stepID, laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint) error {
|
||||
manifest := r.laneManifest(StageMerge, StatusRunning, stepID, laneID, moduleKey, dependencies)
|
||||
manifest.StartedAt = timePtr(r.timestamp())
|
||||
return r.writeManifest(laneManifestPath("merge", stepID, laneID), MergeLaneManifest{StageManifest: manifest})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) MergeSucceeded(laneID, moduleKey string, dependencies []pipeline.CheckpointFingerprint, output pipeline.CheckpointArtifact) error {
|
||||
return r.MergeSucceededForStep("", laneID, moduleKey, dependencies, output)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) MergeSucceededForStep(stepID, laneID, moduleKey string, dependencies []pipeline.CheckpointFingerprint, output pipeline.CheckpointArtifact) error {
|
||||
if err := r.writePayload(lanePayloadPath("merge", stepID, laneID, "output.json"), artifactSingleEnvelope{Output: artifactCheckpointEnvelopeFromOutput(output)}); err != nil {
|
||||
return err
|
||||
}
|
||||
manifest := r.laneManifest(StageMerge, StatusSucceeded, stepID, laneID, moduleKey, dependencies)
|
||||
manifest.OutputDigests = checkpointFingerprints(artifactOutputDigests([]pipeline.CheckpointArtifact{output}))
|
||||
manifest.ValidationStatus = validationStatusString(nil)
|
||||
manifest.CompletedAt = timePtr(r.timestamp())
|
||||
return r.writeManifest(laneManifestPath("merge", stepID, laneID), MergeLaneManifest{StageManifest: manifest, InputCount: len(dependencies)})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) MergeRejected(laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint, rejected contracts.RejectedOutput) error {
|
||||
return r.MergeRejectedForStep("", laneID, moduleKey, dependencies, rejected)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) MergeRejectedForStep(stepID, laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint, rejected contracts.RejectedOutput) error {
|
||||
manifest := r.laneManifest(StageMerge, StatusSucceededWithRejections, stepID, laneID, moduleKey, dependencies)
|
||||
manifest.ValidationStatus = "rejected"
|
||||
manifest.Rejections = rejectionSummaries([]contracts.RejectedOutput{rejected})
|
||||
manifest.CompletedAt = timePtr(r.timestamp())
|
||||
return r.writeManifest(laneManifestPath("merge", stepID, laneID), MergeLaneManifest{StageManifest: manifest, InputCount: len(dependencies)})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) MergeFailed(laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint, err error) error {
|
||||
return r.MergeFailedForStep("", laneID, moduleKey, dependencies, err)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) MergeFailedForStep(stepID, laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint, err error) error {
|
||||
manifest := r.laneManifest(StageMerge, StatusFailed, stepID, laneID, moduleKey, dependencies)
|
||||
manifest.CompletedAt = timePtr(r.timestamp())
|
||||
manifest.Metadata = errorMetadata(err)
|
||||
return r.writeManifest(laneManifestPath("merge", stepID, laneID), MergeLaneManifest{StageManifest: manifest})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) NormalizeRunning(laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint) error {
|
||||
return r.NormalizeRunningForStep("", laneID, moduleKey, dependencies)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) NormalizeRunningForStep(stepID, laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint) error {
|
||||
manifest := r.laneManifest(StageNormalize, StatusRunning, stepID, laneID, moduleKey, dependencies)
|
||||
manifest.StartedAt = timePtr(r.timestamp())
|
||||
return r.writeManifest(laneManifestPath("normalize", stepID, laneID), NormalizeLaneManifest{StageManifest: manifest})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) NormalizeSucceeded(laneID, moduleKey string, dependencies []pipeline.CheckpointFingerprint, output pipeline.CheckpointArtifact) error {
|
||||
return r.NormalizeSucceededForStep("", laneID, moduleKey, dependencies, output)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) NormalizeSucceededForStep(stepID, laneID, moduleKey string, dependencies []pipeline.CheckpointFingerprint, output pipeline.CheckpointArtifact) error {
|
||||
if err := r.writePayload(lanePayloadPath("normalize", stepID, laneID, "output.json"), artifactSingleEnvelope{Output: artifactCheckpointEnvelopeFromOutput(output)}); err != nil {
|
||||
return err
|
||||
}
|
||||
manifest := r.laneManifest(StageNormalize, StatusSucceeded, stepID, laneID, moduleKey, dependencies)
|
||||
manifest.OutputDigests = checkpointFingerprints(artifactOutputDigests([]pipeline.CheckpointArtifact{output}))
|
||||
manifest.ValidationStatus = validationStatusString(nil)
|
||||
manifest.CompletedAt = timePtr(r.timestamp())
|
||||
return r.writeManifest(laneManifestPath("normalize", stepID, laneID), NormalizeLaneManifest{StageManifest: manifest, InputCount: len(dependencies)})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) NormalizeRejected(laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint, rejected contracts.RejectedOutput) error {
|
||||
return r.NormalizeRejectedForStep("", laneID, moduleKey, dependencies, rejected)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) NormalizeRejectedForStep(stepID, laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint, rejected contracts.RejectedOutput) error {
|
||||
manifest := r.laneManifest(StageNormalize, StatusSucceededWithRejections, stepID, laneID, moduleKey, dependencies)
|
||||
manifest.ValidationStatus = "rejected"
|
||||
manifest.Rejections = rejectionSummaries([]contracts.RejectedOutput{rejected})
|
||||
manifest.CompletedAt = timePtr(r.timestamp())
|
||||
return r.writeManifest(laneManifestPath("normalize", stepID, laneID), NormalizeLaneManifest{StageManifest: manifest, InputCount: len(dependencies)})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) NormalizeFailed(laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint, err error) error {
|
||||
return r.NormalizeFailedForStep("", laneID, moduleKey, dependencies, err)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) NormalizeFailedForStep(stepID, laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint, err error) error {
|
||||
manifest := r.laneManifest(StageNormalize, StatusFailed, stepID, laneID, moduleKey, dependencies)
|
||||
manifest.CompletedAt = timePtr(r.timestamp())
|
||||
manifest.Metadata = errorMetadata(err)
|
||||
return r.writeManifest(laneManifestPath("normalize", stepID, laneID), NormalizeLaneManifest{StageManifest: manifest})
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) writeManifest(name string, payload any) error {
|
||||
return r.writeJSON(name, payload)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) writePayload(name string, payload any) error {
|
||||
return r.writeJSON(name, payload)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) writeJSON(name string, payload any) error {
|
||||
if r == nil || strings.TrimSpace(r.root) == "" {
|
||||
return nil
|
||||
}
|
||||
return fileio.WriteJSONWithProcessPermissions(r.root, name, payload)
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) timestamp() time.Time {
|
||||
if r == nil || r.now == nil {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
return r.now().UTC()
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) newStageManifest(stage StageName, status StageStatus) StageManifest {
|
||||
manifest := NewStageManifest(stage, status)
|
||||
if strings.TrimSpace(r.identityDigest) != "" {
|
||||
manifest.Metadata = map[string]string{"checkpoint_identity_digest": r.identityDigest}
|
||||
}
|
||||
return manifest
|
||||
}
|
||||
|
||||
func (r *FilesystemRecorder) laneManifest(stage StageName, status StageStatus, stepID string, laneID string, moduleKey string, dependencies []pipeline.CheckpointFingerprint) StageManifest {
|
||||
manifest := r.newStageManifest(stage, status)
|
||||
manifest.StepID = stepID
|
||||
manifest.LaneID = laneID
|
||||
manifest.ModuleKey = moduleKey
|
||||
manifest.DependencyFingerprints = checkpointFingerprints(dependencies)
|
||||
return manifest
|
||||
}
|
||||
|
||||
type sourceDocumentEnvelope struct {
|
||||
Document source.SourceDocument `json:"document"`
|
||||
}
|
||||
|
||||
type binaryEnvelope struct {
|
||||
ContentBase64 string `json:"content_base64,omitempty"`
|
||||
ContentDigest string `json:"content_digest,omitempty"`
|
||||
MediaType string `json:"media_type,omitempty"`
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
type artifactCheckpointEnvelope struct {
|
||||
LaneID string `json:"lane_id"`
|
||||
ModuleKey string `json:"module_key"`
|
||||
SourceID string `json:"source_id,omitempty"`
|
||||
ChunkID string `json:"chunk_id,omitempty"`
|
||||
ChunkIndex int `json:"chunk_index,omitempty"`
|
||||
ChunkRef source.SourceRef `json:"chunk_ref,omitempty"`
|
||||
Kind contracts.ArtifactKind `json:"artifact_kind"`
|
||||
Schema contracts.ArtifactSchema `json:"schema"`
|
||||
SchemaDigest string `json:"schema_digest"`
|
||||
Content binaryEnvelope `json:"content"`
|
||||
Diagnostics []pipeline.CheckpointDiagnostic `json:"diagnostics,omitempty"`
|
||||
}
|
||||
type artifactExtractEnvelope struct {
|
||||
Outputs []artifactCheckpointEnvelope `json:"outputs"`
|
||||
Rejected []contracts.RejectedOutput `json:"rejected,omitempty"`
|
||||
}
|
||||
type artifactSingleEnvelope struct {
|
||||
Output artifactCheckpointEnvelope `json:"output"`
|
||||
}
|
||||
|
||||
func artifactCheckpointEnvelopeFromOutput(output pipeline.CheckpointArtifact) artifactCheckpointEnvelope {
|
||||
schema := contracts.CloneArtifactSchema(output.Artifact.Schema)
|
||||
schema.JSONSchema = nil
|
||||
return artifactCheckpointEnvelope{LaneID: output.LaneID, ModuleKey: output.ModuleKey, SourceID: output.SourceID, ChunkID: output.ChunkID, ChunkIndex: output.ChunkIndex, ChunkRef: output.ChunkRef, Kind: output.Artifact.Kind, Schema: schema, SchemaDigest: output.SchemaDigest, Content: binaryEnvelopeFromContent(output.Artifact.Content, output.Artifact.MediaType, output.Artifact.Metadata), Diagnostics: cloneCheckpointDiagnostics(output.Diagnostics)}
|
||||
}
|
||||
func artifactCheckpointEnvelopes(outputs []pipeline.CheckpointArtifact) []artifactCheckpointEnvelope {
|
||||
if len(outputs) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]artifactCheckpointEnvelope, 0, len(outputs))
|
||||
for _, v := range outputs {
|
||||
out = append(out, artifactCheckpointEnvelopeFromOutput(v))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneCheckpointDiagnostics(values []pipeline.CheckpointDiagnostic) []pipeline.CheckpointDiagnostic {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
cloned := make([]pipeline.CheckpointDiagnostic, len(values))
|
||||
for index, value := range values {
|
||||
cloned[index] = pipeline.CheckpointDiagnostic{
|
||||
Diagnostic: contracts.CloneProducerDiagnostics([]contracts.ProducerDiagnostic{value.Diagnostic})[0],
|
||||
ValidatorKey: value.ValidatorKey,
|
||||
}
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
func artifactOutputDigests(outputs []pipeline.CheckpointArtifact) []pipeline.CheckpointFingerprint {
|
||||
values := make([]pipeline.CheckpointFingerprint, 0, len(outputs))
|
||||
for i, v := range outputs {
|
||||
values = append(values, pipeline.CheckpointFingerprint{Name: fmt.Sprintf("artifact[%d]", i), Value: contentDigest(v.Artifact.Content)})
|
||||
}
|
||||
return normalizeFingerprints(values)
|
||||
}
|
||||
|
||||
func binaryEnvelopeFromContent(content []byte, mediaType string, metadata map[string]any) binaryEnvelope {
|
||||
return binaryEnvelope{
|
||||
ContentBase64: base64.StdEncoding.EncodeToString(content),
|
||||
ContentDigest: contentDigest(content),
|
||||
MediaType: mediaType,
|
||||
Metadata: cloneMetadata(metadata),
|
||||
}
|
||||
}
|
||||
|
||||
func cloneSourceDocument(doc source.SourceDocument) source.SourceDocument {
|
||||
doc.Units = cloneSourceUnits(doc.Units)
|
||||
doc.Metadata = cloneMetadata(doc.Metadata)
|
||||
return doc
|
||||
}
|
||||
|
||||
func cloneSourceUnits(units []source.SourceUnit) []source.SourceUnit {
|
||||
if len(units) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]source.SourceUnit, 0, len(units))
|
||||
for _, unit := range units {
|
||||
out = append(out, source.SourceUnit{
|
||||
ID: unit.ID,
|
||||
Kind: unit.Kind,
|
||||
Text: unit.Text,
|
||||
Ref: unit.Ref,
|
||||
Metadata: cloneMetadata(unit.Metadata),
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneRejectedOutputs(rejected []contracts.RejectedOutput) []contracts.RejectedOutput {
|
||||
if len(rejected) == 0 {
|
||||
return nil
|
||||
}
|
||||
cloned := make([]contracts.RejectedOutput, len(rejected))
|
||||
for index, item := range rejected {
|
||||
cloned[index] = item
|
||||
if item.Validation != nil {
|
||||
summary := artifacts.CloneValidationSummary(*item.Validation)
|
||||
cloned[index].Validation = &summary
|
||||
}
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
func cloneMetadata(metadata map[string]any) map[string]any {
|
||||
if len(metadata) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]any, len(metadata))
|
||||
for key, value := range metadata {
|
||||
out[key] = value
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func digestFingerprints(name string, digest string) []pipeline.CheckpointFingerprint {
|
||||
digest = strings.TrimSpace(digest)
|
||||
if digest == "" {
|
||||
return nil
|
||||
}
|
||||
return []pipeline.CheckpointFingerprint{{Name: name, Value: digest}}
|
||||
}
|
||||
|
||||
func checkpointFingerprints(values []pipeline.CheckpointFingerprint) []Fingerprint {
|
||||
normalized := normalizeFingerprints(values)
|
||||
if len(normalized) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]Fingerprint, 0, len(normalized))
|
||||
for _, value := range normalized {
|
||||
out = append(out, Fingerprint{Name: value.Name, Value: value.Value})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func normalizeFingerprints(values []pipeline.CheckpointFingerprint) []pipeline.CheckpointFingerprint {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
byName := make(map[string]string, len(values))
|
||||
for _, value := range values {
|
||||
name := strings.TrimSpace(value.Name)
|
||||
fingerprint := strings.TrimSpace(value.Value)
|
||||
if name == "" || fingerprint == "" {
|
||||
continue
|
||||
}
|
||||
byName[name] = fingerprint
|
||||
}
|
||||
if len(byName) == 0 {
|
||||
return nil
|
||||
}
|
||||
names := make([]string, 0, len(byName))
|
||||
for name := range byName {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
out := make([]pipeline.CheckpointFingerprint, 0, len(names))
|
||||
for _, name := range names {
|
||||
out = append(out, pipeline.CheckpointFingerprint{Name: name, Value: byName[name]})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func rejectionSummaries(rejected []contracts.RejectedOutput) []RejectionSummary {
|
||||
if len(rejected) == 0 {
|
||||
return nil
|
||||
}
|
||||
type key struct {
|
||||
validatorName string
|
||||
reasonCode string
|
||||
message string
|
||||
}
|
||||
counts := make(map[key]int, len(rejected))
|
||||
for _, item := range rejected {
|
||||
k := key{validatorName: item.ValidatorName, reasonCode: item.ReasonCode, message: item.Message}
|
||||
counts[k]++
|
||||
}
|
||||
keys := make([]key, 0, len(counts))
|
||||
for k := range counts {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Slice(keys, func(i, j int) bool {
|
||||
if keys[i].validatorName != keys[j].validatorName {
|
||||
return keys[i].validatorName < keys[j].validatorName
|
||||
}
|
||||
if keys[i].reasonCode != keys[j].reasonCode {
|
||||
return keys[i].reasonCode < keys[j].reasonCode
|
||||
}
|
||||
return keys[i].message < keys[j].message
|
||||
})
|
||||
out := make([]RejectionSummary, 0, len(keys))
|
||||
for _, k := range keys {
|
||||
out = append(out, RejectionSummary{
|
||||
ValidatorName: k.validatorName,
|
||||
ReasonCode: k.reasonCode,
|
||||
Message: k.message,
|
||||
Count: counts[k],
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func statusForRejected(rejected []contracts.RejectedOutput) StageStatus {
|
||||
if len(rejected) > 0 {
|
||||
return StatusSucceededWithRejections
|
||||
}
|
||||
return StatusSucceeded
|
||||
}
|
||||
|
||||
func validationStatusString(rejected []contracts.RejectedOutput) string {
|
||||
if len(rejected) > 0 {
|
||||
return "rejected"
|
||||
}
|
||||
return "approved"
|
||||
}
|
||||
|
||||
func errorMetadata(err error) map[string]string {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return map[string]string{"error": err.Error()}
|
||||
}
|
||||
|
||||
func laneManifestPath(stage string, stepID string, laneID string) string {
|
||||
return lanePayloadPath(stage, stepID, laneID, "manifest.json")
|
||||
}
|
||||
|
||||
func lanePayloadPath(stage string, stepID string, laneID string, file string) string {
|
||||
if strings.TrimSpace(stepID) == "" {
|
||||
return path.Join(stage, fileio.EncodePathComponent(laneID), file)
|
||||
}
|
||||
return path.Join(stage, fileio.EncodePathComponent(stepID), fileio.EncodePathComponent(laneID), file)
|
||||
}
|
||||
|
||||
func contentDigest(content []byte) string {
|
||||
sum := sha256.Sum256(content)
|
||||
return "sha256:" + hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
func timePtr(t time.Time) *time.Time {
|
||||
return &t
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
package checkpoint
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
)
|
||||
|
||||
func TestRootBasedRecorderOutputIsReusable(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
identity := testIdentity(t)
|
||||
recorder, err := NewFilesystemRecorder(root, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := recorder.ExtractSucceeded("lane", "module", nil, nil, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loader, err := NewFilesystemLoader(root, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, decision := loader.Extract("lane", "module", nil)
|
||||
if !decision.Reused || len(result.Outputs) != 0 {
|
||||
t.Fatalf("load result=%#v decision=%#v", result, decision)
|
||||
}
|
||||
relative, err := identity.RelativePath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, relative, "extract", "lane", "manifest.json")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStepAwareRecorderAndLoaderIsolateLaneState(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
identity := testIdentity(t)
|
||||
recorder, err := NewFilesystemRecorder(root, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := recorder.(pipeline.StepCheckpointRecorder).ExtractSucceededForStep("step-a", "lane", "module", nil, nil, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loader, err := NewFilesystemLoader(root, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stepLoader := loader.(pipeline.StepCheckpointLoader)
|
||||
if _, decision := stepLoader.ExtractForStep("step-b", "lane", "module", nil); decision.Reused {
|
||||
t.Fatal("checkpoint from another step was reused")
|
||||
}
|
||||
loaded, decision := stepLoader.ExtractForStep("step-a", "lane", "module", nil)
|
||||
if !decision.Reused || len(loaded.Outputs) != 0 {
|
||||
t.Fatalf("step-aware load = %#v, decision=%#v", loaded, decision)
|
||||
}
|
||||
relative, err := identity.RelativePath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, relative, "extract", "step-a", "lane", "manifest.json")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStepAwareCheckpointPreservesDistinctDotIdentities(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
identity := testIdentity(t)
|
||||
recorder, err := NewFilesystemRecorder(root, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stepRecorder := recorder.(pipeline.StepCheckpointRecorder)
|
||||
for _, test := range []struct {
|
||||
stepID string
|
||||
content string
|
||||
}{
|
||||
{stepID: ".", content: `{"identity":"dot"}`},
|
||||
{stepID: "..", content: `{"identity":"dot-dot"}`},
|
||||
} {
|
||||
artifact := pipeline.CheckpointArtifact{
|
||||
LaneID: "lane", ModuleKey: "normalize-module", SourceID: "source", ChunkID: "chunk", ChunkRef: source.SourceRef{SourceID: "source", StartUnitID: 1, EndUnitID: 1}, SchemaDigest: "sha256:schema",
|
||||
Artifact: contracts.SerializedArtifact{Kind: "kind", Schema: contracts.ArtifactSchema{ID: "schema", Name: "Schema", Version: "1"}, MediaType: "application/json", Content: []byte(test.content)},
|
||||
}
|
||||
if err := stepRecorder.NormalizeSucceededForStep(test.stepID, "lane", "normalize-module", nil, artifact); err != nil {
|
||||
t.Fatalf("record %q: %v", test.stepID, err)
|
||||
}
|
||||
}
|
||||
|
||||
loader, err := NewFilesystemLoader(root, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, test := range []struct {
|
||||
stepID string
|
||||
content string
|
||||
path string
|
||||
}{
|
||||
{stepID: ".", content: `{"identity":"dot"}`, path: "%2E"},
|
||||
{stepID: "..", content: `{"identity":"dot-dot"}`, path: "%2E%2E"},
|
||||
} {
|
||||
loaded, decision := loader.AcceptedNormalize(test.stepID, "lane", "normalize-module")
|
||||
if !decision.Reused || string(loaded.Output.Artifact.Content) != test.content {
|
||||
t.Errorf("load %q = %#v, decision=%#v", test.stepID, loaded, decision)
|
||||
}
|
||||
relative, err := identity.RelativePath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, relative, "normalize", test.path, "lane", "manifest.json")); err != nil {
|
||||
t.Errorf("checkpoint for %q: %v", test.stepID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckpointSchemaCompatibilityIdentifiers(t *testing.T) {
|
||||
if WorkspaceSchemaVersion != "notarius.workspace.v4" || WorkspaceSchemaVersionV3 != "notarius.workspace.v3" || WorkspaceSchemaVersionV2 != "notarius.workspace.v2" || WorkspaceSchemaVersionV1 != "notarius.workspace.v1" {
|
||||
t.Fatal("checkpoint schema identifiers are incorrect")
|
||||
}
|
||||
}
|
||||
|
||||
func testIdentity(t *testing.T) Identity {
|
||||
t.Helper()
|
||||
identity, err := NewIdentity(IdentityInput{Pipeline: pipeline.ResolvedPipeline{ID: "pipeline", Digest: "sha256:aaaaaaaaaaaaaaaa", Input: pipeline.Binding("input")}, RawInputDigest: "sha256:bbbbbbbbbbbbbbbb"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return identity
|
||||
}
|
||||
@@ -1,447 +0,0 @@
|
||||
package chunkplan
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
)
|
||||
|
||||
const SchemaVersion = pipeline.ChunkPlanSchemaVersion
|
||||
|
||||
const planFileName = "plan.json"
|
||||
|
||||
type filesystemStore struct {
|
||||
root string
|
||||
write func(*os.Root, string, []byte) error
|
||||
}
|
||||
|
||||
func NewFilesystemStore(root string) (pipeline.ChunkPlanStore, error) {
|
||||
root = strings.TrimSpace(root)
|
||||
if root == "" {
|
||||
return nil, fmt.Errorf("chunk plan root must not be empty")
|
||||
}
|
||||
if strings.ContainsRune(root, '\x00') {
|
||||
return nil, fmt.Errorf("chunk plan root must not contain NUL")
|
||||
}
|
||||
return &filesystemStore{root: filepath.Clean(root), write: writeAtomic}, nil
|
||||
}
|
||||
|
||||
func (s *filesystemStore) Load(sourceDigest string) (pipeline.ChunkPlanRecord, pipeline.ChunkPlanDecision, error) {
|
||||
digestDir, err := digestPathSegment(sourceDigest)
|
||||
if err != nil {
|
||||
return pipeline.ChunkPlanRecord{}, pipeline.ChunkPlanDecision{}, err
|
||||
}
|
||||
root, err := s.openRoot(false)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return pipeline.ChunkPlanRecord{}, pipeline.ChunkPlanDecision{Status: pipeline.ChunkPlanMissing, Reason: lookupReason(pipeline.ChunkPlanMissing)}, nil
|
||||
}
|
||||
return pipeline.ChunkPlanRecord{}, pipeline.ChunkPlanDecision{}, fmt.Errorf("open chunk plan root: %w", err)
|
||||
}
|
||||
defer root.Close()
|
||||
|
||||
digestRoot, state, err := openDigestRoot(root, digestDir, false)
|
||||
if err != nil {
|
||||
return pipeline.ChunkPlanRecord{}, pipeline.ChunkPlanDecision{}, fmt.Errorf("inspect chunk plan directory: %w", err)
|
||||
}
|
||||
if state == entryMissing {
|
||||
return pipeline.ChunkPlanRecord{}, pipeline.ChunkPlanDecision{Status: pipeline.ChunkPlanMissing, Reason: lookupReason(pipeline.ChunkPlanMissing)}, nil
|
||||
}
|
||||
if state == entryRejected {
|
||||
return invalidDecision()
|
||||
}
|
||||
defer digestRoot.Close()
|
||||
|
||||
data, state, err := readPlan(digestRoot)
|
||||
if err != nil {
|
||||
return pipeline.ChunkPlanRecord{}, pipeline.ChunkPlanDecision{}, fmt.Errorf("read chunk plan: %w", err)
|
||||
}
|
||||
if state == entryMissing {
|
||||
return pipeline.ChunkPlanRecord{}, pipeline.ChunkPlanDecision{Status: pipeline.ChunkPlanMissing, Reason: lookupReason(pipeline.ChunkPlanMissing)}, nil
|
||||
}
|
||||
if state == entryRejected {
|
||||
return invalidDecision()
|
||||
}
|
||||
|
||||
var record pipeline.ChunkPlanRecord
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.DisallowUnknownFields()
|
||||
decoder.UseNumber()
|
||||
if err := decoder.Decode(&record); err != nil {
|
||||
return invalidDecision()
|
||||
}
|
||||
var trailing any
|
||||
if err := decoder.Decode(&trailing); err != io.EOF {
|
||||
return invalidDecision()
|
||||
}
|
||||
if err := validateRecord(record, sourceDigest); err != nil {
|
||||
return invalidDecision()
|
||||
}
|
||||
return record, pipeline.ChunkPlanDecision{Status: pipeline.ChunkPlanHit, Reason: lookupReason(pipeline.ChunkPlanHit)}, nil
|
||||
}
|
||||
|
||||
func (s *filesystemStore) Save(record pipeline.ChunkPlanRecord) error {
|
||||
if err := validateRecord(record, record.SourceDigest); err != nil {
|
||||
return fmt.Errorf("validate chunk plan record: %w", err)
|
||||
}
|
||||
digestDir, err := digestPathSegment(record.SourceDigest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.Marshal(record)
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode chunk plan record: %w", err)
|
||||
}
|
||||
data = append(data, '\n')
|
||||
root, err := s.openRoot(true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open chunk plan root: %w", err)
|
||||
}
|
||||
defer root.Close()
|
||||
digestRoot, state, err := openDigestRoot(root, digestDir, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("prepare chunk plan directory: %w", err)
|
||||
}
|
||||
if state == entryRejected {
|
||||
return fmt.Errorf("chunk plan directory has an unsupported type")
|
||||
}
|
||||
defer digestRoot.Close()
|
||||
state, err = inspectPlan(digestRoot, planFileName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("inspect chunk plan file: %w", err)
|
||||
}
|
||||
if state == entryRejected {
|
||||
return fmt.Errorf("chunk plan file has an unsupported type")
|
||||
}
|
||||
writer := s.write
|
||||
if writer == nil {
|
||||
writer = writeAtomic
|
||||
}
|
||||
if err := writer(digestRoot, planFileName, data); err != nil {
|
||||
return fmt.Errorf("write chunk plan: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *filesystemStore) openRoot(create bool) (*os.Root, error) {
|
||||
if s == nil || strings.TrimSpace(s.root) == "" {
|
||||
return nil, fmt.Errorf("chunk plan store must not be nil")
|
||||
}
|
||||
if create {
|
||||
if err := os.MkdirAll(s.root, 0o777); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
root, err := os.OpenRoot(s.root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return root, nil
|
||||
}
|
||||
|
||||
type entryState uint8
|
||||
|
||||
const (
|
||||
entryPresent entryState = iota
|
||||
entryMissing
|
||||
entryRejected
|
||||
)
|
||||
|
||||
func inspectDirectory(root *os.Root, digestDir string) (entryState, os.FileInfo, error) {
|
||||
info, err := root.Lstat(digestDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return entryMissing, nil, nil
|
||||
}
|
||||
return entryPresent, nil, err
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return entryRejected, info, nil
|
||||
}
|
||||
return entryPresent, info, nil
|
||||
}
|
||||
|
||||
type digestOpenHooks struct {
|
||||
BeforeOpen func() error
|
||||
}
|
||||
|
||||
func openDigestRoot(root *os.Root, digestDir string, create bool) (*os.Root, entryState, error) {
|
||||
return openDigestRootWithHooks(root, digestDir, create, digestOpenHooks{})
|
||||
}
|
||||
|
||||
func openDigestRootWithHooks(root *os.Root, digestDir string, create bool, hooks digestOpenHooks) (*os.Root, entryState, error) {
|
||||
for {
|
||||
state, before, err := inspectDirectory(root, digestDir)
|
||||
if err != nil {
|
||||
return nil, entryPresent, err
|
||||
}
|
||||
switch state {
|
||||
case entryRejected:
|
||||
return nil, entryRejected, nil
|
||||
case entryMissing:
|
||||
if !create {
|
||||
return nil, entryMissing, nil
|
||||
}
|
||||
if err := root.Mkdir(digestDir, 0o777); err != nil && !errors.Is(err, os.ErrExist) {
|
||||
return nil, entryPresent, err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if hooks.BeforeOpen != nil {
|
||||
if err := hooks.BeforeOpen(); err != nil {
|
||||
return nil, entryPresent, err
|
||||
}
|
||||
}
|
||||
digestRoot, err := root.OpenRoot(digestDir)
|
||||
if err != nil {
|
||||
return nil, entryPresent, err
|
||||
}
|
||||
opened, statErr := digestRoot.Stat(".")
|
||||
afterState, after, afterErr := inspectDirectory(root, digestDir)
|
||||
if statErr != nil || afterErr != nil {
|
||||
_ = digestRoot.Close()
|
||||
if statErr != nil {
|
||||
return nil, entryPresent, statErr
|
||||
}
|
||||
return nil, entryPresent, afterErr
|
||||
}
|
||||
if afterState != entryPresent || !os.SameFile(before, after) || !os.SameFile(opened, after) {
|
||||
_ = digestRoot.Close()
|
||||
if afterState == entryRejected {
|
||||
return nil, entryRejected, nil
|
||||
}
|
||||
continue
|
||||
}
|
||||
return digestRoot, entryPresent, nil
|
||||
}
|
||||
}
|
||||
|
||||
func inspectPlan(root *os.Root, target string) (entryState, error) {
|
||||
info, err := root.Lstat(target)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return entryMissing, nil
|
||||
}
|
||||
return entryPresent, err
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
||||
return entryRejected, nil
|
||||
}
|
||||
return entryPresent, nil
|
||||
}
|
||||
|
||||
type planReadHooks struct {
|
||||
BeforeOpen func() error
|
||||
}
|
||||
|
||||
func readPlan(root *os.Root) ([]byte, entryState, error) {
|
||||
return readPlanWithHooks(root, planReadHooks{})
|
||||
}
|
||||
|
||||
func readPlanWithHooks(root *os.Root, hooks planReadHooks) ([]byte, entryState, error) {
|
||||
state, err := inspectPlan(root, planFileName)
|
||||
if err != nil || state != entryPresent {
|
||||
return nil, state, err
|
||||
}
|
||||
if hooks.BeforeOpen != nil {
|
||||
if err := hooks.BeforeOpen(); err != nil {
|
||||
return nil, entryPresent, err
|
||||
}
|
||||
}
|
||||
file, err := root.OpenFile(planFileName, os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, entryMissing, nil
|
||||
}
|
||||
if errors.Is(err, syscall.ELOOP) {
|
||||
return nil, entryRejected, nil
|
||||
}
|
||||
return nil, entryPresent, err
|
||||
}
|
||||
defer file.Close()
|
||||
opened, err := file.Stat()
|
||||
if err != nil {
|
||||
return nil, entryPresent, err
|
||||
}
|
||||
if !opened.Mode().IsRegular() {
|
||||
return nil, entryRejected, nil
|
||||
}
|
||||
currentState, currentErr := inspectPlan(root, planFileName)
|
||||
if currentErr != nil {
|
||||
return nil, entryPresent, currentErr
|
||||
}
|
||||
if currentState == entryRejected {
|
||||
return nil, entryRejected, nil
|
||||
}
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return nil, entryPresent, err
|
||||
}
|
||||
return data, entryPresent, nil
|
||||
}
|
||||
|
||||
func digestPathSegment(digest string) (string, error) {
|
||||
if !strings.HasPrefix(digest, "sha256:") {
|
||||
return "", fmt.Errorf("source digest must use sha256:<64 lowercase hex> format")
|
||||
}
|
||||
hexDigest := strings.TrimPrefix(digest, "sha256:")
|
||||
if len(hexDigest) != 64 || strings.ToLower(hexDigest) != hexDigest {
|
||||
return "", fmt.Errorf("source digest must use sha256:<64 lowercase hex> format")
|
||||
}
|
||||
decoded, err := hex.DecodeString(hexDigest)
|
||||
if err != nil || len(decoded) != 32 {
|
||||
return "", fmt.Errorf("source digest must use sha256:<64 lowercase hex> format")
|
||||
}
|
||||
return hexDigest, nil
|
||||
}
|
||||
|
||||
func validateRecord(record pipeline.ChunkPlanRecord, requestedDigest string) error {
|
||||
if record.SchemaVersion != SchemaVersion {
|
||||
return fmt.Errorf("schema_version %q is not supported", record.SchemaVersion)
|
||||
}
|
||||
if err := contracts.ValidateProducerDiagnostics(record.Diagnostics); err != nil {
|
||||
return fmt.Errorf("diagnostics: %w", err)
|
||||
}
|
||||
if _, err := digestPathSegment(requestedDigest); err != nil {
|
||||
return err
|
||||
}
|
||||
if record.SourceDigest != requestedDigest {
|
||||
return fmt.Errorf("source_digest does not match requested source")
|
||||
}
|
||||
if record.Plan.SourceDigest != record.SourceDigest {
|
||||
return fmt.Errorf("plan source_digest does not match record source_digest")
|
||||
}
|
||||
if len(record.Plan.Ranges) == 0 {
|
||||
return fmt.Errorf("plan ranges must not be empty")
|
||||
}
|
||||
if err := source.ValidateChunkAnnotations(record.Plan.Annotations); err != nil {
|
||||
return fmt.Errorf("plan annotations: %w", err)
|
||||
}
|
||||
seenStarts := make(map[int]struct{}, len(record.Plan.Ranges))
|
||||
for i, chunkRange := range record.Plan.Ranges {
|
||||
if chunkRange.StartUnitID <= 0 || chunkRange.EndUnitID <= 0 {
|
||||
return fmt.Errorf("plan range[%d] boundaries must be positive", i)
|
||||
}
|
||||
if _, exists := seenStarts[chunkRange.StartUnitID]; exists {
|
||||
return fmt.Errorf("plan range[%d] duplicates start_unit_id %d", i, chunkRange.StartUnitID)
|
||||
}
|
||||
seenStarts[chunkRange.StartUnitID] = struct{}{}
|
||||
if err := source.ValidateChunkAnnotations(chunkRange.Annotations); err != nil {
|
||||
return fmt.Errorf("plan range[%d] annotations: %w", i, err)
|
||||
}
|
||||
}
|
||||
wantDigest, err := source.DigestChunkPlan(record.Plan)
|
||||
if err != nil {
|
||||
return fmt.Errorf("digest plan: %w", err)
|
||||
}
|
||||
if record.PlanDigest != wantDigest {
|
||||
return fmt.Errorf("plan_digest does not match plan")
|
||||
}
|
||||
if strings.TrimSpace(record.Producer.InputModule) == "" {
|
||||
return fmt.Errorf("producer input_module must not be empty")
|
||||
}
|
||||
if strings.TrimSpace(record.Producer.ChunkModule) == "" {
|
||||
return fmt.Errorf("producer chunk_module must not be empty")
|
||||
}
|
||||
if record.CreatedAt.IsZero() {
|
||||
return fmt.Errorf("created_at must not be zero")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func invalidDecision() (pipeline.ChunkPlanRecord, pipeline.ChunkPlanDecision, error) {
|
||||
return pipeline.ChunkPlanRecord{}, pipeline.ChunkPlanDecision{Status: pipeline.ChunkPlanInvalid, Reason: lookupReason(pipeline.ChunkPlanInvalid)}, nil
|
||||
}
|
||||
|
||||
func lookupReason(status pipeline.ChunkPlanStatus) string {
|
||||
switch status {
|
||||
case pipeline.ChunkPlanHit:
|
||||
return "stored chunk plan is valid"
|
||||
case pipeline.ChunkPlanMissing:
|
||||
return "chunk plan not found"
|
||||
case pipeline.ChunkPlanInvalid:
|
||||
return "stored chunk plan is invalid"
|
||||
default:
|
||||
return "chunk plan lookup skipped"
|
||||
}
|
||||
}
|
||||
|
||||
type atomicWriteHooks struct {
|
||||
BeforeCreateTemp func() error
|
||||
BeforeRename func() error
|
||||
}
|
||||
|
||||
func writeAtomic(root *os.Root, target string, data []byte) error {
|
||||
return writeAtomicWithHooks(root, target, data, atomicWriteHooks{})
|
||||
}
|
||||
|
||||
func writeAtomicWithHooks(root *os.Root, target string, data []byte, hooks atomicWriteHooks) error {
|
||||
if hooks.BeforeCreateTemp != nil {
|
||||
if err := hooks.BeforeCreateTemp(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
temp, tempPath, err := createTemporaryFile(root, target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
removeTemp := true
|
||||
defer func() {
|
||||
if removeTemp {
|
||||
_ = root.Remove(tempPath)
|
||||
}
|
||||
}()
|
||||
if _, err := temp.Write(data); err != nil {
|
||||
_ = temp.Close()
|
||||
return err
|
||||
}
|
||||
if err := temp.Sync(); err != nil {
|
||||
_ = temp.Close()
|
||||
return err
|
||||
}
|
||||
if err := temp.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if hooks.BeforeRename != nil {
|
||||
if err := hooks.BeforeRename(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := root.Rename(tempPath, target); err != nil {
|
||||
return err
|
||||
}
|
||||
removeTemp = false
|
||||
return nil
|
||||
}
|
||||
|
||||
func createTemporaryFile(root *os.Root, target string) (*os.File, string, error) {
|
||||
for attempt := 0; attempt < 32; attempt++ {
|
||||
var suffix [16]byte
|
||||
if _, err := rand.Read(suffix[:]); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
path := strings.TrimSuffix(target, planFileName) + ".plan.json.tmp-" + hex.EncodeToString(suffix[:])
|
||||
file, err := root.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o666)
|
||||
if errors.Is(err, os.ErrExist) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return file, path, nil
|
||||
}
|
||||
return nil, "", fmt.Errorf("create unique temporary chunk plan file")
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package chunkplan
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFilesystemStorePermissions(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
umask int
|
||||
dirMode os.FileMode
|
||||
fileMode os.FileMode
|
||||
}{
|
||||
{name: "private", umask: 0o077, dirMode: 0o700, fileMode: 0o600},
|
||||
{name: "group", umask: 0o007, dirMode: 0o770, fileMode: 0o660},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
previousUmask := syscall.Umask(test.umask)
|
||||
defer syscall.Umask(previousUmask)
|
||||
|
||||
root := filepath.Join(t.TempDir(), "plans")
|
||||
store := newStore(t, root)
|
||||
if err := store.Save(testRecord(t, 1)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
digestDir := filepath.Join(root, strings.TrimPrefix(testSourceDigest, "sha256:"))
|
||||
for path, want := range map[string]os.FileMode{
|
||||
root: test.dirMode,
|
||||
digestDir: test.dirMode,
|
||||
filepath.Join(digestDir, "plan.json"): test.fileMode,
|
||||
} {
|
||||
assertStorePermissions(t, path, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemStoreLeavesExistingDirectoriesUntouched(t *testing.T) {
|
||||
previousUmask := syscall.Umask(0o077)
|
||||
defer syscall.Umask(previousUmask)
|
||||
|
||||
root := filepath.Join(t.TempDir(), "plans")
|
||||
digestDir := filepath.Join(root, strings.TrimPrefix(testSourceDigest, "sha256:"))
|
||||
syscall.Umask(0)
|
||||
if err := os.Mkdir(root, 0o751); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Mkdir(digestDir, 0o753); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
syscall.Umask(0o077)
|
||||
|
||||
store := newStore(t, root)
|
||||
if err := store.Save(testRecord(t, 1)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertStorePermissions(t, root, 0o751)
|
||||
assertStorePermissions(t, digestDir, 0o753)
|
||||
}
|
||||
|
||||
func assertStorePermissions(t *testing.T, path string, want os.FileMode) {
|
||||
t.Helper()
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := info.Mode().Perm(); got != want {
|
||||
t.Fatalf("%s mode = %#o, want %#o", path, got, want)
|
||||
}
|
||||
}
|
||||
@@ -1,547 +0,0 @@
|
||||
package chunkplan
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/artifacts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
)
|
||||
|
||||
const testSourceDigest = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
|
||||
func TestFilesystemStoreRoundTripAndExactPath(t *testing.T) {
|
||||
root := filepath.Join(t.TempDir(), "plans")
|
||||
store := newStore(t, root)
|
||||
record := testRecord(t, 1)
|
||||
if err := store.Save(record); err != nil {
|
||||
t.Fatalf("Save() error = %v", err)
|
||||
}
|
||||
|
||||
target := filepath.Join(root, strings.TrimPrefix(testSourceDigest, "sha256:"), "plan.json")
|
||||
data, err := os.ReadFile(target)
|
||||
if err != nil {
|
||||
t.Fatalf("read exact plan path: %v", err)
|
||||
}
|
||||
if bytes.Contains(data, []byte("RAW REFERENCE CONTENT")) {
|
||||
t.Fatal("stored record contains raw reference content")
|
||||
}
|
||||
var envelope struct {
|
||||
Producer struct {
|
||||
References []map[string]any `json:"references"`
|
||||
} `json:"producer"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &envelope); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(envelope.Producer.References) != 1 {
|
||||
t.Fatalf("stored references = %#v", envelope.Producer.References)
|
||||
}
|
||||
if _, exists := envelope.Producer.References[0]["content"]; exists {
|
||||
t.Fatalf("stored reference contains content field: %#v", envelope.Producer.References[0])
|
||||
}
|
||||
|
||||
got, decision, err := store.Load(testSourceDigest)
|
||||
if err != nil || decision.Status != pipeline.ChunkPlanHit {
|
||||
t.Fatalf("Load() decision=%#v error=%v", decision, err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, record) {
|
||||
t.Fatalf("round trip record = %#v, want %#v", got, record)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemStoreMissingAndOperationalErrors(t *testing.T) {
|
||||
root := filepath.Join(t.TempDir(), "plans")
|
||||
store := newStore(t, root)
|
||||
_, decision, err := store.Load(testSourceDigest)
|
||||
if err != nil || decision.Status != pipeline.ChunkPlanMissing {
|
||||
t.Fatalf("missing decision=%#v error=%v", decision, err)
|
||||
}
|
||||
if _, err := os.Stat(root); !os.IsNotExist(err) {
|
||||
t.Fatalf("Load() created missing root: %v", err)
|
||||
}
|
||||
|
||||
target := filepath.Join(root, strings.TrimPrefix(testSourceDigest, "sha256:"), "plan.json")
|
||||
if err := os.MkdirAll(target, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, decision, err = store.Load(testSourceDigest)
|
||||
if err != nil || decision.Status != pipeline.ChunkPlanInvalid {
|
||||
t.Fatalf("Load(plan.json directory) decision=%#v error=%v", decision, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemStoreRejectsSymlinkedEntries(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func(t *testing.T, root, outside string)
|
||||
}{
|
||||
{
|
||||
name: "digest directory",
|
||||
setup: func(t *testing.T, root, outside string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(outside, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeFile(t, filepath.Join(outside, "plan.json"), []byte("outside plan"), 0o640)
|
||||
createSymlink(t, outside, filepath.Join(root, strings.TrimPrefix(testSourceDigest, "sha256:")))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "plan file",
|
||||
setup: func(t *testing.T, root, outside string) {
|
||||
t.Helper()
|
||||
digestDir := filepath.Join(root, strings.TrimPrefix(testSourceDigest, "sha256:"))
|
||||
if err := os.MkdirAll(digestDir, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeFile(t, outside, []byte("outside plan"), 0o640)
|
||||
createSymlink(t, outside, filepath.Join(digestDir, "plan.json"))
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
root := filepath.Join(t.TempDir(), "plans")
|
||||
if err := os.MkdirAll(root, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
outside := filepath.Join(t.TempDir(), "outside")
|
||||
tc.setup(t, root, outside)
|
||||
before := readFile(t, outsidePlanPath(tc.name, outside))
|
||||
beforeMode := fileMode(t, outsidePlanPath(tc.name, outside))
|
||||
|
||||
store := newStore(t, root)
|
||||
_, decision, err := store.Load(testSourceDigest)
|
||||
if err != nil || decision.Status != pipeline.ChunkPlanInvalid {
|
||||
t.Fatalf("Load() decision=%#v error=%v", decision, err)
|
||||
}
|
||||
if err := store.Save(testRecord(t, 1)); err == nil {
|
||||
t.Fatal("Save() error = nil")
|
||||
}
|
||||
outsidePlan := outsidePlanPath(tc.name, outside)
|
||||
if got := readFile(t, outsidePlan); !bytes.Equal(got, before) {
|
||||
t.Fatalf("outside content = %q, want %q", got, before)
|
||||
}
|
||||
if got := fileMode(t, outsidePlan); got != beforeMode {
|
||||
t.Fatalf("outside mode = %04o, want %04o", got, beforeMode)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenDigestRootRejectsEntryReplacedDuringOpen(t *testing.T) {
|
||||
rootPath := t.TempDir()
|
||||
digestDir := strings.TrimPrefix(testSourceDigest, "sha256:")
|
||||
if err := os.Mkdir(filepath.Join(rootPath, digestDir), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Mkdir(filepath.Join(rootPath, "redirect"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root, err := os.OpenRoot(rootPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer root.Close()
|
||||
|
||||
traced := false
|
||||
opened, state, err := openDigestRootWithHooks(root, digestDir, false, digestOpenHooks{BeforeOpen: func() error {
|
||||
if traced {
|
||||
return nil
|
||||
}
|
||||
traced = true
|
||||
if err := os.Rename(filepath.Join(rootPath, digestDir), filepath.Join(rootPath, "original")); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Symlink("redirect", filepath.Join(rootPath, digestDir))
|
||||
}})
|
||||
if opened != nil {
|
||||
_ = opened.Close()
|
||||
}
|
||||
if err != nil || state != entryRejected {
|
||||
t.Fatalf("openDigestRootWithHooks() root=%v state=%v error=%v, want nil/rejected/nil", opened, state, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadPlanRejectsEntryReplacedDuringOpen(t *testing.T) {
|
||||
rootPath := t.TempDir()
|
||||
writeFile(t, filepath.Join(rootPath, planFileName), []byte("original"), 0o600)
|
||||
writeFile(t, filepath.Join(rootPath, "redirect.json"), []byte("redirect"), 0o600)
|
||||
root, err := os.OpenRoot(rootPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer root.Close()
|
||||
|
||||
data, state, err := readPlanWithHooks(root, planReadHooks{BeforeOpen: func() error {
|
||||
if err := os.Remove(filepath.Join(rootPath, planFileName)); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Symlink("redirect.json", filepath.Join(rootPath, planFileName))
|
||||
}})
|
||||
if err != nil || state != entryRejected || data != nil {
|
||||
t.Fatalf("readPlanWithHooks() data=%q state=%v error=%v, want nil/rejected/nil", data, state, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemStoreRejectsUnexpectedEntryTypes(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func(t *testing.T, root string)
|
||||
}{
|
||||
{
|
||||
name: "digest file",
|
||||
setup: func(t *testing.T, root string) {
|
||||
writeFile(t, filepath.Join(root, strings.TrimPrefix(testSourceDigest, "sha256:")), []byte("not a directory"), 0o600)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "plan directory",
|
||||
setup: func(t *testing.T, root string) {
|
||||
if err := os.MkdirAll(filepath.Join(root, strings.TrimPrefix(testSourceDigest, "sha256:"), "plan.json"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
tc.setup(t, root)
|
||||
store := newStore(t, root)
|
||||
_, decision, err := store.Load(testSourceDigest)
|
||||
if err != nil || decision.Status != pipeline.ChunkPlanInvalid {
|
||||
t.Fatalf("Load() decision=%#v error=%v", decision, err)
|
||||
}
|
||||
if err := store.Save(testRecord(t, 1)); err == nil {
|
||||
t.Fatal("Save() error = nil")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemStoreRejectsMalformedSourceDigests(t *testing.T) {
|
||||
store := newStore(t, t.TempDir())
|
||||
for _, digest := range []string{"", "sha1:" + strings.Repeat("a", 64), "sha256:../escape", "sha256:" + strings.Repeat("A", 64), "sha256:" + strings.Repeat("a", 63)} {
|
||||
t.Run(digest, func(t *testing.T) {
|
||||
if _, _, err := store.Load(digest); err == nil {
|
||||
t.Fatal("Load() error = nil")
|
||||
}
|
||||
record := testRecord(t, 1)
|
||||
record.SourceDigest = digest
|
||||
record.Plan.SourceDigest = digest
|
||||
record.PlanDigest, _ = source.DigestChunkPlan(record.Plan)
|
||||
if err := store.Save(record); err == nil {
|
||||
t.Fatal("Save() error = nil")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemStoreReportsInvalidRecordsAsRecoverable(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mutate func([]byte) []byte
|
||||
}{
|
||||
{name: "unknown field", mutate: func(data []byte) []byte {
|
||||
return bytes.Replace(data, []byte(`{"schema_version"`), []byte(`{"SENTINEL_UNKNOWN_FIELD":true,"schema_version"`), 1)
|
||||
}},
|
||||
{name: "truncated JSON", mutate: func(data []byte) []byte { return data[:len(data)/2] }},
|
||||
{name: "legacy v2 record", mutate: replaceJSON(`notarius.chunk-plan.v3`, `notarius.chunk-plan.v2`)},
|
||||
{name: "source mismatch", mutate: replaceJSON(testSourceDigest, "sha256:"+strings.Repeat("b", 64))},
|
||||
{name: "plan digest mismatch", mutate: func(data []byte) []byte {
|
||||
prefix := []byte(`"plan_digest":"sha256:`)
|
||||
index := bytes.Index(data, prefix)
|
||||
if index >= 0 {
|
||||
data[index+len(prefix)] = '0'
|
||||
}
|
||||
return data
|
||||
}},
|
||||
{name: "noncanonical annotation", mutate: func(data []byte) []byte {
|
||||
return bytes.Replace(data, []byte(`"test/value"`), []byte(`"SENTINEL_ANNOTATION_NAMESPACE"`), 1)
|
||||
}},
|
||||
{name: "bad boundary", mutate: func(data []byte) []byte {
|
||||
return bytes.Replace(data, []byte(`"start_unit_id":1`), []byte(`"start_unit_id":0`), 1)
|
||||
}},
|
||||
{name: "timestamp", mutate: replaceJSON(`2026-07-18T12:00:00Z`, `SENTINEL_TIMESTAMP`)},
|
||||
{name: "trailing JSON", mutate: func(data []byte) []byte { return append(data, []byte(` {"SENTINEL_TRAILING":true}`)...) }},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
store := newStore(t, root)
|
||||
record := testRecord(t, 1)
|
||||
if err := store.Save(record); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(root, strings.TrimPrefix(testSourceDigest, "sha256:"), "plan.json")
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, tc.mutate(data), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, decision, err := store.Load(testSourceDigest)
|
||||
if err != nil || decision.Status != pipeline.ChunkPlanInvalid || !reflect.DeepEqual(got, pipeline.ChunkPlanRecord{}) || decision.Reason != "stored chunk plan is invalid" {
|
||||
t.Fatalf("record=%#v decision=%#v error=%v", got, decision, err)
|
||||
}
|
||||
for _, sentinel := range []string{"SENTINEL_UNKNOWN_FIELD", "SENTINEL_SCHEMA_VALUE", "SENTINEL_ANNOTATION_NAMESPACE", "SENTINEL_TIMESTAMP", "SENTINEL_TRAILING"} {
|
||||
if strings.Contains(decision.Reason, sentinel) {
|
||||
t.Fatalf("decision leaked %q: %#v", sentinel, decision)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemStoreAtomicallyReplacesAndPreservesValidRecordOnFailure(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
store := newStore(t, root)
|
||||
first := testRecord(t, 1)
|
||||
second := testRecord(t, 2)
|
||||
if err := store.Save(first); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
invalid := second
|
||||
invalid.PlanDigest = "sha256:" + strings.Repeat("0", 64)
|
||||
if err := store.Save(invalid); err == nil {
|
||||
t.Fatal("Save(invalid) error = nil")
|
||||
}
|
||||
got, _, _ := store.Load(testSourceDigest)
|
||||
if !reflect.DeepEqual(got, first) {
|
||||
t.Fatalf("record after failed replacement = %#v", got)
|
||||
}
|
||||
if err := store.Save(second); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _, _ = store.Load(testSourceDigest)
|
||||
if !reflect.DeepEqual(got, second) {
|
||||
t.Fatalf("record after replacement = %#v", got)
|
||||
}
|
||||
assertNoTemps(t, filepath.Join(root, strings.TrimPrefix(testSourceDigest, "sha256:")))
|
||||
}
|
||||
|
||||
func TestFilesystemStoreConcurrentWritersExposeCompleteRecord(t *testing.T) {
|
||||
store := newStore(t, t.TempDir())
|
||||
const writers = 24
|
||||
records := make([]pipeline.ChunkPlanRecord, writers)
|
||||
for i := range records {
|
||||
records[i] = testRecord(t, i+1)
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
errs := make(chan error, writers)
|
||||
for i := range records {
|
||||
wg.Add(1)
|
||||
go func(record pipeline.ChunkPlanRecord) {
|
||||
defer wg.Done()
|
||||
errs <- store.Save(record)
|
||||
}(records[i])
|
||||
}
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
for err := range errs {
|
||||
if err != nil {
|
||||
t.Fatalf("concurrent Save() error = %v", err)
|
||||
}
|
||||
}
|
||||
got, decision, err := store.Load(testSourceDigest)
|
||||
if err != nil || decision.Status != pipeline.ChunkPlanHit {
|
||||
t.Fatalf("Load() decision=%#v error=%v", decision, err)
|
||||
}
|
||||
var annotation struct {
|
||||
Value int `json:"value"`
|
||||
}
|
||||
if err := json.Unmarshal(got.Plan.Annotations["test/value"], &annotation); err != nil || annotation.Value < 1 || annotation.Value > writers {
|
||||
t.Fatalf("final annotation=%#v error=%v", annotation, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemStoreReadersObserveOnlyCompleteRecordsDuringWrites(t *testing.T) {
|
||||
store := newStore(t, t.TempDir())
|
||||
if err := store.Save(testRecord(t, 1)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const writers = 12
|
||||
const readers = 12
|
||||
errs := make(chan error, writers+readers)
|
||||
start := make(chan struct{})
|
||||
var writersDone sync.WaitGroup
|
||||
for i := 0; i < writers; i++ {
|
||||
writersDone.Add(1)
|
||||
go func(value int) {
|
||||
defer writersDone.Done()
|
||||
<-start
|
||||
errs <- store.Save(testRecord(t, value+2))
|
||||
}(i)
|
||||
}
|
||||
for i := 0; i < readers; i++ {
|
||||
go func() {
|
||||
<-start
|
||||
for attempt := 0; attempt < 50; attempt++ {
|
||||
record, decision, err := store.Load(testSourceDigest)
|
||||
if err != nil || decision.Status != pipeline.ChunkPlanHit {
|
||||
errs <- fmt.Errorf("Load() decision=%#v error=%v", decision, err)
|
||||
return
|
||||
}
|
||||
if err := validateRecord(record, testSourceDigest); err != nil {
|
||||
errs <- fmt.Errorf("reader observed invalid record: %w", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
errs <- nil
|
||||
}()
|
||||
}
|
||||
close(start)
|
||||
writersDone.Wait()
|
||||
for i := 0; i < readers+writers; i++ {
|
||||
if err := <-errs; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemStoreInterruptedWritesPreservePreviousRecord(t *testing.T) {
|
||||
store := newStore(t, t.TempDir()).(*filesystemStore)
|
||||
first := testRecord(t, 1)
|
||||
if err := store.Save(first); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
hooks atomicWriteHooks
|
||||
}{
|
||||
{name: "before temporary file", hooks: atomicWriteHooks{BeforeCreateTemp: func() error { return errors.New("interrupted before temporary file") }}},
|
||||
{name: "before rename", hooks: atomicWriteHooks{BeforeRename: func() error { return errors.New("interrupted before rename") }}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
store.write = func(root *os.Root, target string, data []byte) error {
|
||||
return writeAtomicWithHooks(root, target, data, tc.hooks)
|
||||
}
|
||||
if err := store.Save(testRecord(t, 2)); err == nil {
|
||||
t.Fatal("Save() error = nil")
|
||||
}
|
||||
store.write = writeAtomic
|
||||
got, decision, err := store.Load(testSourceDigest)
|
||||
if err != nil || decision.Status != pipeline.ChunkPlanHit || !reflect.DeepEqual(got, first) {
|
||||
t.Fatalf("record after interruption=%#v decision=%#v error=%v", got, decision, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newStore(t *testing.T, root string) pipeline.ChunkPlanStore {
|
||||
t.Helper()
|
||||
store, err := NewFilesystemStore(root)
|
||||
if err != nil {
|
||||
t.Fatalf("NewFilesystemStore() error = %v", err)
|
||||
}
|
||||
return store
|
||||
}
|
||||
|
||||
func testRecord(t *testing.T, value int) pipeline.ChunkPlanRecord {
|
||||
t.Helper()
|
||||
annotation, err := json.Marshal(map[string]int{"value": value})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plan := source.ChunkPlan{
|
||||
SourceDigest: testSourceDigest,
|
||||
Ranges: []source.ChunkRange{{StartUnitID: 1, EndUnitID: 2, Annotations: source.ChunkAnnotations{"test/range": json.RawMessage(`{"range":true}`)}}},
|
||||
Annotations: source.ChunkAnnotations{"test/value": annotation},
|
||||
}
|
||||
planDigest, err := source.DigestChunkPlan(plan)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return pipeline.ChunkPlanRecord{
|
||||
SchemaVersion: SchemaVersion,
|
||||
SourceDigest: testSourceDigest,
|
||||
PlanDigest: planDigest,
|
||||
Plan: plan,
|
||||
Producer: pipeline.ChunkPlanProducer{
|
||||
InputModule: "input/test", ChunkModule: "chunk/test", LLMProfile: "profile/test",
|
||||
References: []artifacts.ReferenceProvenance{{Stage: "chunk", SlotName: "guide", OriginType: "file", OriginURI: "file:///guide.txt", Digest: "sha256:reference"}},
|
||||
Metadata: map[string]any{"prompt_id": "test/prompt", "enabled": true},
|
||||
},
|
||||
Diagnostics: []contracts.ProducerDiagnostic{{
|
||||
Disposition: contracts.DiagnosticDispositionWarning,
|
||||
Category: contracts.DiagnosticCategoryConfiguration,
|
||||
ReasonCode: "empty_reference",
|
||||
OccurrenceCount: 1,
|
||||
Samples: []contracts.DiagnosticSample{{Scope: "reference", Message: "Reference was empty."}},
|
||||
}},
|
||||
CreatedAt: time.Date(2026, 7, 18, 12, 0, 0, 0, time.UTC),
|
||||
}
|
||||
}
|
||||
|
||||
func replaceJSON(old, replacement string) func([]byte) []byte {
|
||||
return func(data []byte) []byte { return bytes.Replace(data, []byte(old), []byte(replacement), 1) }
|
||||
}
|
||||
|
||||
func assertNoTemps(t *testing.T, dir string) {
|
||||
t.Helper()
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if strings.Contains(entry.Name(), ".tmp-") {
|
||||
t.Fatalf("temporary file remains: %s", entry.Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func createSymlink(t *testing.T, target, link string) {
|
||||
t.Helper()
|
||||
if err := os.Symlink(target, link); err != nil {
|
||||
t.Skipf("create symlink: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeFile(t *testing.T, path string, data []byte, mode os.FileMode) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(path, data, mode); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chmod(path, mode); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func readFile(t *testing.T, path string) []byte {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func fileMode(t *testing.T, path string) os.FileMode {
|
||||
t.Helper()
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return info.Mode().Perm()
|
||||
}
|
||||
|
||||
func outsidePlanPath(name, outside string) string {
|
||||
if name == "digest directory" {
|
||||
return filepath.Join(outside, "plan.json")
|
||||
}
|
||||
return outside
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package contracts
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// DebugStructuredCompletionRequest is the content-safe representation of a
|
||||
// structured completion request for ordinary diagnostics and summaries.
|
||||
// Detailed prompt material remains available only through the explicitly
|
||||
// requested LLM debug trace.
|
||||
type DebugStructuredCompletionRequest struct {
|
||||
StageName string `json:"stage_name,omitempty"`
|
||||
PromptID string `json:"prompt_id,omitempty"`
|
||||
PromptVersion string `json:"prompt_version,omitempty"`
|
||||
ProfileID string `json:"profile_id,omitempty"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
InputCount int `json:"input_count"`
|
||||
VariableCount int `json:"variable_count"`
|
||||
StructuredOutputRepairAttempts *int `json:"structured_output_repair_attempts,omitempty"`
|
||||
Correction *DebugSemanticCorrection `json:"correction,omitempty"`
|
||||
}
|
||||
|
||||
// DebugSemanticCorrection records only safe correction metadata. It never
|
||||
// exposes the assistant response or user guidance text.
|
||||
type DebugSemanticCorrection struct {
|
||||
AssistantResponseBytes int `json:"assistant_response_bytes"`
|
||||
AssistantResponseDigest string `json:"assistant_response_digest"`
|
||||
UserGuidanceBytes int `json:"user_guidance_bytes"`
|
||||
UserGuidanceDigest string `json:"user_guidance_digest"`
|
||||
}
|
||||
|
||||
// DebugSummary returns a content-safe representation suitable for ordinary
|
||||
// diagnostics. It does not validate or retain correction content.
|
||||
func (request StructuredCompletionRequest) DebugSummary() DebugStructuredCompletionRequest {
|
||||
summary := DebugStructuredCompletionRequest{
|
||||
StageName: request.StageName,
|
||||
PromptID: request.PromptID,
|
||||
PromptVersion: request.PromptVersion,
|
||||
ProfileID: request.ProfileID,
|
||||
SessionID: request.SessionID,
|
||||
InputCount: len(request.Inputs),
|
||||
VariableCount: len(request.Vars),
|
||||
}
|
||||
if request.StructuredOutputRepairAttempts != nil {
|
||||
attempts := *request.StructuredOutputRepairAttempts
|
||||
summary.StructuredOutputRepairAttempts = &attempts
|
||||
}
|
||||
if request.Correction != nil {
|
||||
summary.Correction = request.Correction.DebugSummary()
|
||||
}
|
||||
return summary
|
||||
}
|
||||
|
||||
// DebugSummary returns content-safe correction metadata suitable for ordinary
|
||||
// diagnostics.
|
||||
func (correction *SemanticCorrection) DebugSummary() *DebugSemanticCorrection {
|
||||
if correction == nil {
|
||||
return nil
|
||||
}
|
||||
return &DebugSemanticCorrection{
|
||||
AssistantResponseBytes: len(correction.AssistantResponse),
|
||||
AssistantResponseDigest: debugContentDigest(correction.AssistantResponse),
|
||||
UserGuidanceBytes: len(correction.UserGuidance),
|
||||
UserGuidanceDigest: debugContentDigest([]byte(correction.UserGuidance)),
|
||||
}
|
||||
}
|
||||
|
||||
// String prevents ordinary request formatting from exposing correction
|
||||
// content. Use the explicitly requested debug trace for complete messages.
|
||||
func (request StructuredCompletionRequest) String() string {
|
||||
return fmt.Sprintf("%+v", request.DebugSummary())
|
||||
}
|
||||
|
||||
// GoString gives %#v formatting the same content-safe behavior as String.
|
||||
func (request StructuredCompletionRequest) GoString() string {
|
||||
return request.String()
|
||||
}
|
||||
|
||||
func debugContentDigest(content []byte) string {
|
||||
sum := sha256.Sum256(content)
|
||||
return "sha256:" + hex.EncodeToString(sum[:])
|
||||
}
|
||||
@@ -7,122 +7,6 @@ import (
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
)
|
||||
|
||||
type StructuredCompletionRequest struct {
|
||||
StageName string `json:"stage_name"`
|
||||
PromptID string `json:"prompt_id,omitempty"`
|
||||
PromptVersion string `json:"prompt_version,omitempty"`
|
||||
ProfileID string `json:"profile_id,omitempty"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
Inputs LLMInputSet `json:"inputs,omitempty"`
|
||||
Vars map[string]any `json:"vars,omitempty"`
|
||||
StructuredOutputRepairAttempts *int `json:"structured_output_repair_attempts,omitempty"`
|
||||
Correction *SemanticCorrection `json:"-"`
|
||||
}
|
||||
|
||||
type StructuredCompletionResponse struct {
|
||||
Content json.RawMessage `json:"content"`
|
||||
Provider string `json:"provider,omitempty"`
|
||||
Model string `json:"model,omitempty"`
|
||||
ProfileID string `json:"profile_id,omitempty"`
|
||||
PromptTokens int `json:"prompt_tokens,omitempty"`
|
||||
CompletionTokens int `json:"completion_tokens,omitempty"`
|
||||
TotalTokens int `json:"total_tokens,omitempty"`
|
||||
RepairAttempts int `json:"repair_attempts,omitempty"`
|
||||
Debug *LLMDebugMaterial `json:"debug,omitempty"`
|
||||
Execution *LLMExecutionSummary `json:"execution,omitempty"`
|
||||
}
|
||||
|
||||
type LLMObservedTokenCount struct {
|
||||
Value int `json:"value"`
|
||||
Known bool `json:"known"`
|
||||
Complete bool `json:"complete"`
|
||||
}
|
||||
|
||||
type LLMObservedTokenUsage struct {
|
||||
PromptTokens LLMObservedTokenCount `json:"prompt_tokens"`
|
||||
CompletionTokens LLMObservedTokenCount `json:"completion_tokens"`
|
||||
TotalTokens LLMObservedTokenCount `json:"total_tokens"`
|
||||
CachedTokens LLMObservedTokenCount `json:"cached_tokens"`
|
||||
CacheWriteTokens LLMObservedTokenCount `json:"cache_write_tokens"`
|
||||
}
|
||||
|
||||
type LLMExecutionSummary struct {
|
||||
Attempts uint64 `json:"attempts"`
|
||||
TransientRetries uint64 `json:"transient_retries"`
|
||||
RepairCalls uint64 `json:"repair_calls"`
|
||||
CapacityWaitMS int64 `json:"capacity_wait_ms"`
|
||||
GenerationMS int64 `json:"generation_ms"`
|
||||
RetryDelayMS int64 `json:"retry_delay_ms"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
Outcome string `json:"outcome"`
|
||||
FailureKind string `json:"failure_kind"`
|
||||
StatusCode int `json:"status_code"`
|
||||
RetriesExhausted bool `json:"retries_exhausted"`
|
||||
Usage LLMObservedTokenUsage `json:"usage"`
|
||||
}
|
||||
|
||||
type StructuredLLMClient interface {
|
||||
CompleteStructured(ctx context.Context, req StructuredCompletionRequest, out any) (StructuredCompletionResponse, error)
|
||||
}
|
||||
|
||||
type LLMDebugMaterial struct {
|
||||
Prompt *LLMDebugPrompt `json:"prompt,omitempty"`
|
||||
Response *LLMDebugResponse `json:"response,omitempty"`
|
||||
}
|
||||
|
||||
type LLMDebugPrompt struct {
|
||||
PromptID string `json:"prompt_id,omitempty"`
|
||||
PromptVersion string `json:"prompt_version,omitempty"`
|
||||
PromptHash string `json:"prompt_hash,omitempty"`
|
||||
SelectedProfileID string `json:"selected_profile_id,omitempty"`
|
||||
SelectedBackendID string `json:"selected_backend_id,omitempty"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
RenderedPromptHash string `json:"rendered_prompt_hash,omitempty"`
|
||||
Messages []LLMDebugMessage `json:"messages,omitempty"`
|
||||
EffectiveModelParams map[string]any `json:"effective_model_params,omitempty"`
|
||||
OutputContract map[string]any `json:"output_contract,omitempty"`
|
||||
StructuredOutput map[string]any `json:"structured_output,omitempty"`
|
||||
InputHashes map[string]string `json:"input_hashes,omitempty"`
|
||||
}
|
||||
|
||||
type LLMDebugMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
CacheControl map[string]any `json:"cache_control,omitempty"`
|
||||
}
|
||||
|
||||
type LLMDebugResponse struct {
|
||||
Content string `json:"content,omitempty"`
|
||||
RunID string `json:"run_id,omitempty"`
|
||||
PromptID string `json:"prompt_id,omitempty"`
|
||||
PromptVersion string `json:"prompt_version,omitempty"`
|
||||
PromptHash string `json:"prompt_hash,omitempty"`
|
||||
RenderedPromptHash string `json:"rendered_prompt_hash,omitempty"`
|
||||
SelectedProfileID string `json:"selected_profile_id,omitempty"`
|
||||
ModelName string `json:"model_name,omitempty"`
|
||||
Endpoint string `json:"endpoint,omitempty"`
|
||||
EffectiveModelParams map[string]any `json:"effective_model_params,omitempty"`
|
||||
InputHashes map[string]string `json:"input_hashes,omitempty"`
|
||||
Validation map[string]any `json:"validation,omitempty"`
|
||||
Usage LLMDebugUsage `json:"usage,omitempty"`
|
||||
ProviderError *LLMDebugProviderError `json:"provider_error,omitempty"`
|
||||
}
|
||||
|
||||
type LLMDebugProviderError struct {
|
||||
StatusCode int `json:"status_code,omitempty"`
|
||||
Code string `json:"code,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
type LLMDebugUsage struct {
|
||||
PromptTokens int `json:"prompt_tokens,omitempty"`
|
||||
CompletionTokens int `json:"completion_tokens,omitempty"`
|
||||
TotalTokens int `json:"total_tokens,omitempty"`
|
||||
CachedTokens int `json:"cached_tokens,omitempty"`
|
||||
CacheWriteTokens int `json:"cache_write_tokens,omitempty"`
|
||||
}
|
||||
|
||||
type LLMInputMaterial struct {
|
||||
Name string `json:"name"`
|
||||
MediaType string `json:"media_type,omitempty"`
|
||||
@@ -162,12 +46,11 @@ func (set LLMInputSet) Clone() LLMInputSet {
|
||||
}
|
||||
|
||||
type ParseRequest struct {
|
||||
SourceID string `json:"source_id,omitempty"`
|
||||
Path string `json:"path,omitempty"`
|
||||
Raw []byte `json:"-"`
|
||||
LLMProfile string `json:"llm_profile,omitempty"`
|
||||
StructuredOutputRepairAttempts *int `json:"structured_output_repair_attempts,omitempty"`
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
SourceID string `json:"source_id,omitempty"`
|
||||
Path string `json:"path,omitempty"`
|
||||
Raw []byte `json:"-"`
|
||||
LLMProfile string `json:"llm_profile,omitempty"`
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
type InputAdapter interface {
|
||||
|
||||
@@ -14,7 +14,6 @@ var _ Chunker = fakeChunker{}
|
||||
var _ Extractor[fakeArtifact] = fakeExtractor{}
|
||||
var _ Merger[fakeArtifact] = fakeMerger{}
|
||||
var _ Normalizer[fakeArtifact] = fakeNormalizer{}
|
||||
var _ StructuredLLMClient = fakeLLMClient{}
|
||||
var _ OutputEncoder = fakeOutputEncoder{}
|
||||
|
||||
func TestCloneReferenceSlotsEmptyInputReturnsNil(t *testing.T) {
|
||||
@@ -314,14 +313,6 @@ func (normalizer fakeNormalizer) Normalize(ctx context.Context, req TypedNormali
|
||||
return TypedNormalizeResult[fakeArtifact]{Value: req.MergeOutput.Value}, nil
|
||||
}
|
||||
|
||||
type fakeLLMClient struct{}
|
||||
|
||||
func (client fakeLLMClient) CompleteStructured(ctx context.Context, req StructuredCompletionRequest, out any) (StructuredCompletionResponse, error) {
|
||||
return StructuredCompletionResponse{
|
||||
Content: json.RawMessage(`{"value":"example"}`),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type fakeOutputEncoder struct {
|
||||
key string
|
||||
}
|
||||
|
||||
@@ -1,178 +0,0 @@
|
||||
package contracts
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
)
|
||||
|
||||
// CorrectionProtocol identifies how a producer can represent the model output
|
||||
// that directly controlled a candidate.
|
||||
type CorrectionProtocol string
|
||||
|
||||
const (
|
||||
CorrectionProtocolSingleResponseV1 CorrectionProtocol = "single_response_v1"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxValidationReasonCodeBytes = 128
|
||||
MaxValidationCorrectionGuidanceBytes = 4 * 1024
|
||||
MaxAssistantResponseBytes = 1 << 20
|
||||
MaxCorrectionGuidanceBytes = 64 * 1024
|
||||
MaxCorrectionContentBytes = MaxAssistantResponseBytes + MaxCorrectionGuidanceBytes
|
||||
)
|
||||
|
||||
func (protocol CorrectionProtocol) Validate() error {
|
||||
if protocol == CorrectionProtocolSingleResponseV1 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("unsupported correction protocol %q", protocol)
|
||||
}
|
||||
|
||||
// SemanticCorrection carries the latest model response and application-owned
|
||||
// guidance for one fresh corrected request. Its content is sensitive and is
|
||||
// deliberately excluded from ordinary JSON serialization.
|
||||
type SemanticCorrection struct {
|
||||
AssistantResponse []byte `json:"-"`
|
||||
UserGuidance string `json:"-"`
|
||||
}
|
||||
|
||||
func NewSemanticCorrection(assistantResponse []byte, userGuidance string) (*SemanticCorrection, error) {
|
||||
correction := &SemanticCorrection{
|
||||
AssistantResponse: append([]byte(nil), assistantResponse...),
|
||||
UserGuidance: userGuidance,
|
||||
}
|
||||
if err := correction.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return correction, nil
|
||||
}
|
||||
|
||||
func CloneSemanticCorrection(correction *SemanticCorrection) (*SemanticCorrection, error) {
|
||||
if correction == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return NewSemanticCorrection(correction.AssistantResponse, correction.UserGuidance)
|
||||
}
|
||||
|
||||
// CloneStructuredCompletionRequest returns a request whose mutable values are
|
||||
// owned by the caller. It is suitable for clients that retain requests after
|
||||
// CompleteStructured returns.
|
||||
func CloneStructuredCompletionRequest(request StructuredCompletionRequest) (StructuredCompletionRequest, error) {
|
||||
correction, err := CloneSemanticCorrection(request.Correction)
|
||||
if err != nil {
|
||||
return StructuredCompletionRequest{}, fmt.Errorf("clone correction: %w", err)
|
||||
}
|
||||
vars, err := source.CloneMetadata(request.Vars)
|
||||
if err != nil {
|
||||
return StructuredCompletionRequest{}, fmt.Errorf("clone variables: %w", err)
|
||||
}
|
||||
request.Inputs = request.Inputs.Clone()
|
||||
request.Vars = vars
|
||||
request.Correction = correction
|
||||
if request.StructuredOutputRepairAttempts != nil {
|
||||
attempts := *request.StructuredOutputRepairAttempts
|
||||
request.StructuredOutputRepairAttempts = &attempts
|
||||
}
|
||||
return request, nil
|
||||
}
|
||||
|
||||
func (correction SemanticCorrection) Validate() error {
|
||||
if err := validateAssistantResponse(correction.AssistantResponse); err != nil {
|
||||
return fmt.Errorf("semantic correction assistant response: %w", err)
|
||||
}
|
||||
if err := validateBoundedText(correction.UserGuidance, MaxCorrectionGuidanceBytes, "semantic correction user guidance", false); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(correction.AssistantResponse)+len(correction.UserGuidance) > MaxCorrectionContentBytes {
|
||||
return errors.New("semantic correction content exceeds maximum length")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ModelCandidate preserves the exact single model response that directly
|
||||
// controlled a producer result. It is attempt-local and never durable data.
|
||||
type ModelCandidate struct {
|
||||
Response []byte `json:"-"`
|
||||
Protocol CorrectionProtocol `json:"-"`
|
||||
}
|
||||
|
||||
func NewModelCandidate(response []byte, protocol CorrectionProtocol) (*ModelCandidate, error) {
|
||||
candidate := &ModelCandidate{Response: append([]byte(nil), response...), Protocol: protocol}
|
||||
if err := candidate.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return candidate, nil
|
||||
}
|
||||
|
||||
func CloneModelCandidate(candidate *ModelCandidate) (*ModelCandidate, error) {
|
||||
if candidate == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return NewModelCandidate(candidate.Response, candidate.Protocol)
|
||||
}
|
||||
|
||||
func (candidate ModelCandidate) Validate() error {
|
||||
if err := candidate.Protocol.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateAssistantResponse(candidate.Response); err != nil {
|
||||
return fmt.Errorf("model candidate response: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateValidationResult(result ValidationResult) error {
|
||||
if err := ValidateProducerDiagnostics(result.Diagnostics); err != nil {
|
||||
return fmt.Errorf("validation diagnostics: %w", err)
|
||||
}
|
||||
if !result.Approved && result.ReasonCode == "" {
|
||||
return errors.New("validation rejection reason code must not be empty")
|
||||
}
|
||||
if result.ReasonCode != "" {
|
||||
if err := validateBoundedText(result.ReasonCode, MaxValidationReasonCodeBytes, "validation reason code", false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !result.Approved && result.CorrectionGuidance == "" {
|
||||
return errors.New("validation rejection correction guidance must not be empty")
|
||||
}
|
||||
if result.CorrectionGuidance != "" {
|
||||
if err := validateBoundedText(result.CorrectionGuidance, MaxValidationCorrectionGuidanceBytes, "validation correction guidance", false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateAssistantResponse(response []byte) error {
|
||||
if len(response) > MaxAssistantResponseBytes {
|
||||
return errors.New("exceeds maximum length")
|
||||
}
|
||||
if !utf8.Valid(response) {
|
||||
return errors.New("must be valid UTF-8")
|
||||
}
|
||||
if len(strings.TrimSpace(string(response))) == 0 {
|
||||
return errors.New("must not be blank")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateBoundedText(value string, maximum int, name string, optional bool) error {
|
||||
if value == "" && optional {
|
||||
return nil
|
||||
}
|
||||
if !utf8.ValidString(value) {
|
||||
return fmt.Errorf("%s must be valid UTF-8", name)
|
||||
}
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fmt.Errorf("%s must not be blank", name)
|
||||
}
|
||||
if len(value) > maximum {
|
||||
return fmt.Errorf("%s exceeds maximum length", name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
package contracts
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSemanticCorrectionOwnsValidatedContent(t *testing.T) {
|
||||
assistant := []byte(`{"items":["original"]}`)
|
||||
correction, err := NewSemanticCorrection(assistant, "Return one corrected replacement.")
|
||||
if err != nil {
|
||||
t.Fatalf("NewSemanticCorrection() error = %v", err)
|
||||
}
|
||||
assistant[0] = '['
|
||||
if got := string(correction.AssistantResponse); got != `{"items":["original"]}` {
|
||||
t.Fatalf("assistant response = %q, want owned original content", got)
|
||||
}
|
||||
|
||||
clone, err := CloneSemanticCorrection(correction)
|
||||
if err != nil {
|
||||
t.Fatalf("CloneSemanticCorrection() error = %v", err)
|
||||
}
|
||||
clone.AssistantResponse[0] = '['
|
||||
if got := string(correction.AssistantResponse); got != `{"items":["original"]}` {
|
||||
t.Fatalf("source correction changed through clone = %q", got)
|
||||
}
|
||||
if nilClone, err := CloneSemanticCorrection(nil); err != nil || nilClone != nil {
|
||||
t.Fatalf("CloneSemanticCorrection(nil) = %#v, %v; want nil, nil", nilClone, err)
|
||||
}
|
||||
|
||||
encoded, err := json.Marshal(correction)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal correction: %v", err)
|
||||
}
|
||||
if string(encoded) != "{}" {
|
||||
t.Fatalf("correction JSON = %s, want no sensitive content", encoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCorrectionContractsRejectInvalidContent(t *testing.T) {
|
||||
tooLongAssistant := bytes.Repeat([]byte("a"), MaxAssistantResponseBytes+1)
|
||||
tooLongGuidance := strings.Repeat("a", MaxCorrectionGuidanceBytes+1)
|
||||
tooLongReason := strings.Repeat("a", MaxValidationReasonCodeBytes+1)
|
||||
tooLongValidationGuidance := strings.Repeat("a", MaxValidationCorrectionGuidanceBytes+1)
|
||||
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
call func() error
|
||||
}{
|
||||
{"blank assistant", func() error { _, err := NewSemanticCorrection([]byte(" \n"), "guidance"); return err }},
|
||||
{"invalid assistant utf8", func() error { _, err := NewSemanticCorrection([]byte{0xff}, "guidance"); return err }},
|
||||
{"oversized assistant", func() error { _, err := NewSemanticCorrection(tooLongAssistant, "guidance"); return err }},
|
||||
{"blank guidance", func() error { _, err := NewSemanticCorrection([]byte("response"), " \t"); return err }},
|
||||
{"invalid guidance utf8", func() error { _, err := NewSemanticCorrection([]byte("response"), string([]byte{0xff})); return err }},
|
||||
{"oversized guidance", func() error { _, err := NewSemanticCorrection([]byte("response"), tooLongGuidance); return err }},
|
||||
{"unsupported protocol", func() error { _, err := NewModelCandidate([]byte("response"), "multiple_responses"); return err }},
|
||||
{"missing candidate protocol", func() error { _, err := NewModelCandidate([]byte("response"), ""); return err }},
|
||||
{"blank candidate response", func() error { _, err := NewModelCandidate([]byte(" "), CorrectionProtocolSingleResponseV1); return err }},
|
||||
{"missing rejection reason code", func() error {
|
||||
return ValidateValidationResult(ValidationResult{CorrectionGuidance: "Correct the response."})
|
||||
}},
|
||||
{"missing rejection guidance", func() error { return ValidateValidationResult(ValidationResult{ReasonCode: "invalid"}) }},
|
||||
{"oversized reason code", func() error {
|
||||
return ValidateValidationResult(ValidationResult{ReasonCode: tooLongReason, CorrectionGuidance: "Correct the response."})
|
||||
}},
|
||||
{"blank reason code", func() error {
|
||||
return ValidateValidationResult(ValidationResult{ReasonCode: " \t", CorrectionGuidance: "Correct the response."})
|
||||
}},
|
||||
{"invalid correction guidance utf8", func() error {
|
||||
return ValidateValidationResult(ValidationResult{ReasonCode: "invalid", CorrectionGuidance: string([]byte{0xff})})
|
||||
}},
|
||||
{"oversized correction guidance", func() error {
|
||||
return ValidateValidationResult(ValidationResult{ReasonCode: "invalid", CorrectionGuidance: tooLongValidationGuidance})
|
||||
}},
|
||||
{"invalid diagnostics", func() error {
|
||||
return ValidateValidationResult(ValidationResult{Approved: true, Diagnostics: []ProducerDiagnostic{{}}})
|
||||
}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if err := test.call(); err == nil {
|
||||
t.Fatal("validation error = nil, want error")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelCandidateOwnsValidatedResponse(t *testing.T) {
|
||||
response := []byte(`{"items":["original"]}`)
|
||||
candidate, err := NewModelCandidate(response, CorrectionProtocolSingleResponseV1)
|
||||
if err != nil {
|
||||
t.Fatalf("NewModelCandidate() error = %v", err)
|
||||
}
|
||||
response[0] = '['
|
||||
if got := string(candidate.Response); got != `{"items":["original"]}` {
|
||||
t.Fatalf("candidate response = %q, want owned original content", got)
|
||||
}
|
||||
clone, err := CloneModelCandidate(candidate)
|
||||
if err != nil {
|
||||
t.Fatalf("CloneModelCandidate() error = %v", err)
|
||||
}
|
||||
clone.Response[0] = '['
|
||||
if got := string(candidate.Response); got != `{"items":["original"]}` {
|
||||
t.Fatalf("source candidate changed through clone = %q", got)
|
||||
}
|
||||
if nilClone, err := CloneModelCandidate(nil); err != nil || nilClone != nil {
|
||||
t.Fatalf("CloneModelCandidate(nil) = %#v, %v; want nil, nil", nilClone, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidationResultAllowsAbsentOptionalCorrectionFields(t *testing.T) {
|
||||
if err := ValidateValidationResult(ValidationResult{Approved: true}); err != nil {
|
||||
t.Fatalf("ValidateValidationResult() error = %v, want nil", err)
|
||||
}
|
||||
if err := ValidateValidationResult(ValidationResult{ReasonCode: "invalid-evidence", CorrectionGuidance: "Provide source-backed evidence."}); err != nil {
|
||||
t.Fatalf("ValidateValidationResult() error = %v, want nil", err)
|
||||
}
|
||||
if err := CorrectionProtocol("").Validate(); err == nil {
|
||||
t.Fatal("empty correction protocol validation error = nil, want error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloneStructuredCompletionRequestOwnsCorrection(t *testing.T) {
|
||||
correction, err := NewSemanticCorrection([]byte(`{"value":"original"}`), "Correct the value.")
|
||||
if err != nil {
|
||||
t.Fatalf("NewSemanticCorrection() error = %v", err)
|
||||
}
|
||||
attempts := 2
|
||||
request := StructuredCompletionRequest{
|
||||
Inputs: LLMInputSet{"source": NewLLMInputMaterial("source", "application/json", []byte(`{"source":true}`), "", "")},
|
||||
Vars: map[string]any{"labels": []string{"original"}},
|
||||
StructuredOutputRepairAttempts: &attempts,
|
||||
Correction: correction,
|
||||
}
|
||||
clone, err := CloneStructuredCompletionRequest(request)
|
||||
if err != nil {
|
||||
t.Fatalf("CloneStructuredCompletionRequest() error = %v", err)
|
||||
}
|
||||
correction.AssistantResponse[0] = '['
|
||||
request.Inputs["source"] = NewLLMInputMaterial("source", "application/json", []byte(`{"source":false}`), "", "")
|
||||
*request.StructuredOutputRepairAttempts = 7
|
||||
if got := string(clone.Correction.AssistantResponse); got != `{"value":"original"}` {
|
||||
t.Fatalf("cloned correction response = %q, want owned original content", got)
|
||||
}
|
||||
if got := string(clone.Inputs["source"].Content); got != `{"source":true}` {
|
||||
t.Fatalf("cloned input = %q, want owned original content", got)
|
||||
}
|
||||
if clone.StructuredOutputRepairAttempts == nil || *clone.StructuredOutputRepairAttempts != 2 {
|
||||
t.Fatalf("cloned repair attempts = %v, want 2", clone.StructuredOutputRepairAttempts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStructuredCompletionRequestDebugSummaryOmitsCorrectionContent(t *testing.T) {
|
||||
const assistantResponse = `{"secret":"assistant response"}`
|
||||
const userGuidance = "secret user guidance"
|
||||
correction, err := NewSemanticCorrection([]byte(assistantResponse), userGuidance)
|
||||
if err != nil {
|
||||
t.Fatalf("NewSemanticCorrection() error = %v", err)
|
||||
}
|
||||
request := StructuredCompletionRequest{
|
||||
Inputs: LLMInputSet{"source": NewLLMInputMaterial("source", "application/json", []byte(`{"source":true}`), "", "")},
|
||||
Vars: map[string]any{"custom": "value"},
|
||||
Correction: correction,
|
||||
}
|
||||
|
||||
summary := request.DebugSummary()
|
||||
if summary.InputCount != 1 || summary.VariableCount != 1 || summary.Correction == nil {
|
||||
t.Fatalf("debug summary = %#v, want input, variable, and correction metadata", summary)
|
||||
}
|
||||
if summary.Correction.AssistantResponseBytes != len(assistantResponse) || summary.Correction.UserGuidanceBytes != len(userGuidance) ||
|
||||
summary.Correction.AssistantResponseDigest == "" || summary.Correction.UserGuidanceDigest == "" {
|
||||
t.Fatalf("correction summary = %#v, want byte counts and digests", summary.Correction)
|
||||
}
|
||||
encoded, err := json.Marshal(summary)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal debug summary: %v", err)
|
||||
}
|
||||
for _, rendered := range []string{string(encoded), fmt.Sprintf("%+v", request), fmt.Sprintf("%#v", request)} {
|
||||
for _, secret := range []string{assistantResponse, userGuidance} {
|
||||
if strings.Contains(rendered, secret) {
|
||||
t.Fatalf("content-safe request rendering leaked %q: %s", secret, rendered)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
52
internal/framework/contracts/validation_result.go
Normal file
52
internal/framework/contracts/validation_result.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package contracts
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxValidationReasonCodeBytes = 128
|
||||
MaxValidationCorrectionGuidanceBytes = 4 * 1024
|
||||
)
|
||||
|
||||
func ValidateValidationResult(result ValidationResult) error {
|
||||
if err := ValidateProducerDiagnostics(result.Diagnostics); err != nil {
|
||||
return fmt.Errorf("validation diagnostics: %w", err)
|
||||
}
|
||||
if !result.Approved && result.ReasonCode == "" {
|
||||
return errors.New("validation rejection reason code must not be empty")
|
||||
}
|
||||
if result.ReasonCode != "" {
|
||||
if err := validateBoundedText(result.ReasonCode, MaxValidationReasonCodeBytes, "validation reason code", false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !result.Approved && result.CorrectionGuidance == "" {
|
||||
return errors.New("validation rejection correction guidance must not be empty")
|
||||
}
|
||||
if result.CorrectionGuidance != "" {
|
||||
if err := validateBoundedText(result.CorrectionGuidance, MaxValidationCorrectionGuidanceBytes, "validation correction guidance", false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateBoundedText(value string, maximum int, name string, optional bool) error {
|
||||
if value == "" && optional {
|
||||
return nil
|
||||
}
|
||||
if !utf8.ValidString(value) {
|
||||
return fmt.Errorf("%s must be valid UTF-8", name)
|
||||
}
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fmt.Errorf("%s must not be blank", name)
|
||||
}
|
||||
if len(value) > maximum {
|
||||
return fmt.Errorf("%s exceeds maximum length", name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
33
internal/framework/contracts/validation_result_test.go
Normal file
33
internal/framework/contracts/validation_result_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package contracts
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidationResultRejectsInvalidFindings(t *testing.T) {
|
||||
for _, result := range []ValidationResult{
|
||||
{CorrectionGuidance: "Correct the response."},
|
||||
{ReasonCode: "invalid"},
|
||||
{ReasonCode: strings.Repeat("a", MaxValidationReasonCodeBytes+1), CorrectionGuidance: "Correct the response."},
|
||||
{ReasonCode: " \t", CorrectionGuidance: "Correct the response."},
|
||||
{ReasonCode: "invalid", CorrectionGuidance: string([]byte{0xff})},
|
||||
{ReasonCode: "invalid", CorrectionGuidance: strings.Repeat("a", MaxValidationCorrectionGuidanceBytes+1)},
|
||||
{Approved: true, Diagnostics: []ProducerDiagnostic{{}}},
|
||||
} {
|
||||
if err := ValidateValidationResult(result); err == nil {
|
||||
t.Fatalf("invalid validation result accepted: %#v", result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidationResultAllowsAbsentOptionalCorrectionFields(t *testing.T) {
|
||||
for _, result := range []ValidationResult{
|
||||
{Approved: true},
|
||||
{ReasonCode: "invalid-evidence", CorrectionGuidance: "Provide source-backed evidence."},
|
||||
} {
|
||||
if err := ValidateValidationResult(result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,278 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/readonlyfs"
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
)
|
||||
|
||||
type AssetSource struct {
|
||||
FS fs.FS
|
||||
Root string
|
||||
}
|
||||
|
||||
type AssetRegistry struct {
|
||||
prompts []AssetSource
|
||||
schemas []AssetSource
|
||||
fallbackProfiles []AssetSource
|
||||
}
|
||||
|
||||
type AssetHashPart struct {
|
||||
FS fs.FS
|
||||
Path string
|
||||
}
|
||||
|
||||
func NewAssetRegistry() *AssetRegistry {
|
||||
return &AssetRegistry{}
|
||||
}
|
||||
|
||||
func (r *AssetRegistry) RegisterPromptFS(fsys fs.FS, root string) error {
|
||||
if r == nil {
|
||||
return fmt.Errorf("asset registry must not be nil")
|
||||
}
|
||||
source, err := newAssetSource(fsys, root)
|
||||
if err != nil {
|
||||
return fmt.Errorf("register prompt assets: %w", err)
|
||||
}
|
||||
r.prompts = append(r.prompts, source)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *AssetRegistry) RegisterSchemaFS(fsys fs.FS, root string) error {
|
||||
if r == nil {
|
||||
return fmt.Errorf("asset registry must not be nil")
|
||||
}
|
||||
source, err := newAssetSource(fsys, root)
|
||||
if err != nil {
|
||||
return fmt.Errorf("register schema assets: %w", err)
|
||||
}
|
||||
r.schemas = append(r.schemas, source)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterFallbackProfileFS registers lower-precedence application profiles.
|
||||
// Operator entries recursively overlay them, retaining omitted fields.
|
||||
func (r *AssetRegistry) RegisterFallbackProfileFS(fsys fs.FS, root string) error {
|
||||
if r == nil {
|
||||
return fmt.Errorf("asset registry must not be nil")
|
||||
}
|
||||
source, err := newAssetSource(fsys, root)
|
||||
if err != nil {
|
||||
return fmt.Errorf("register fallback profile assets: %w", err)
|
||||
}
|
||||
r.fallbackProfiles = append(r.fallbackProfiles, source)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *AssetRegistry) PromptFS() (fs.FS, error) {
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("asset registry must not be nil")
|
||||
}
|
||||
return flattenAssetSources(r.prompts)
|
||||
}
|
||||
|
||||
func (r *AssetRegistry) SchemaFS() (fs.FS, error) {
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("asset registry must not be nil")
|
||||
}
|
||||
return flattenAssetSources(r.schemas)
|
||||
}
|
||||
|
||||
func (r *AssetRegistry) FallbackProfileFS() (fs.FS, error) {
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("asset registry must not be nil")
|
||||
}
|
||||
return flattenAssetSources(r.fallbackProfiles)
|
||||
}
|
||||
|
||||
// FallbackProfileDigest returns a deterministic, non-secret identity for the
|
||||
// flattened fallback profile assets.
|
||||
func (r *AssetRegistry) FallbackProfileDigest() (string, error) {
|
||||
_, digest, _, err := r.fallbackProfileAssets()
|
||||
return digest, err
|
||||
}
|
||||
|
||||
// PromptKitConfigurationSources returns the registered prompt and fallback
|
||||
// profile assets in one strict native PromptKit underlay. Schema assets remain a
|
||||
// separate PromptKit option because they are runtime validation resources.
|
||||
func (r *AssetRegistry) PromptKitConfigurationSources() (promptkit.ConfigurationSources, []promptkit.Option, error) {
|
||||
if r == nil {
|
||||
return promptkit.ConfigurationSources{}, nil, fmt.Errorf("asset registry must not be nil")
|
||||
}
|
||||
inputs, err := promptKitFilesystemInputs(r.prompts, "notarius-prompts")
|
||||
if err != nil {
|
||||
return promptkit.ConfigurationSources{}, nil, fmt.Errorf("prepare prompt configuration sources: %w", err)
|
||||
}
|
||||
profiles, err := promptKitFilesystemInputs(r.fallbackProfiles, "notarius-fallback-profiles")
|
||||
if err != nil {
|
||||
return promptkit.ConfigurationSources{}, nil, fmt.Errorf("prepare fallback profile configuration sources: %w", err)
|
||||
}
|
||||
inputs = append(inputs, profiles...)
|
||||
var underlays []configkit.Underlay
|
||||
if len(inputs) != 0 {
|
||||
underlays = []configkit.Underlay{{Inputs: inputs}}
|
||||
}
|
||||
schemaFS, err := r.SchemaFS()
|
||||
if err != nil {
|
||||
return promptkit.ConfigurationSources{}, nil, fmt.Errorf("prepare schema assets: %w", err)
|
||||
}
|
||||
return promptkit.ConfigurationSources{Underlays: underlays}, []promptkit.Option{
|
||||
promptkit.WithSchemaFS(schemaFS, "."),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func promptKitFilesystemInputs(sources []AssetSource, label string) ([]configkit.Input, error) {
|
||||
var selected []configkit.Input
|
||||
for index, source := range sources {
|
||||
inputs, err := promptkit.FilesystemInputs(fmt.Sprintf("%s-%d", label, index), source.FS, source.Root)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("select filesystem inputs: %w", err)
|
||||
}
|
||||
selected = append(selected, inputs...)
|
||||
}
|
||||
return selected, nil
|
||||
}
|
||||
|
||||
func (r *AssetRegistry) fallbackProfileAssets() (fs.FS, string, bool, error) {
|
||||
if r == nil {
|
||||
return nil, "", false, fmt.Errorf("asset registry must not be nil")
|
||||
}
|
||||
if len(r.fallbackProfiles) == 0 {
|
||||
empty := sha256.Sum256([]byte("notarius:fallback-profile-assets:empty"))
|
||||
return nil, "sha256:" + hex.EncodeToString(empty[:]), false, nil
|
||||
}
|
||||
fallbackFS, err := r.FallbackProfileFS()
|
||||
if err != nil {
|
||||
return nil, "", false, fmt.Errorf("prepare fallback profile assets: %w", err)
|
||||
}
|
||||
digest, err := hashAssetFilesystem(fallbackFS)
|
||||
if err != nil {
|
||||
return nil, "", false, err
|
||||
}
|
||||
return fallbackFS, digest, true, nil
|
||||
}
|
||||
|
||||
func HashAssets(parts []AssetHashPart) (string, error) {
|
||||
if len(parts) == 0 {
|
||||
return "", fmt.Errorf("asset hash requires at least one part")
|
||||
}
|
||||
hash := sha256.New()
|
||||
for _, part := range parts {
|
||||
cleanPath, err := cleanAssetPath(part.Path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("hash asset %q: %w", part.Path, err)
|
||||
}
|
||||
data, err := fs.ReadFile(part.FS, cleanPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read hash asset %s: %w", cleanPath, err)
|
||||
}
|
||||
if _, err := io.WriteString(hash, cleanPath); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := hash.Write([]byte{0}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := hash.Write(data); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := hash.Write([]byte{0}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return "sha256:" + hex.EncodeToString(hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
func hashAssetFilesystem(fsys fs.FS) (string, error) {
|
||||
var parts []AssetHashPart
|
||||
err := fs.WalkDir(fsys, ".", func(name string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
if entry.IsDir() {
|
||||
return nil
|
||||
}
|
||||
parts = append(parts, AssetHashPart{FS: fsys, Path: name})
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("walk assets for digest: %w", err)
|
||||
}
|
||||
if len(parts) > 0 {
|
||||
return HashAssets(parts)
|
||||
}
|
||||
empty := sha256.Sum256([]byte("notarius:fallback-profile-assets:empty"))
|
||||
return "sha256:" + hex.EncodeToString(empty[:]), nil
|
||||
}
|
||||
|
||||
func newAssetSource(fsys fs.FS, root string) (AssetSource, error) {
|
||||
if fsys == nil {
|
||||
return AssetSource{}, fmt.Errorf("filesystem must not be nil")
|
||||
}
|
||||
cleanRoot, err := cleanAssetRoot(root)
|
||||
if err != nil {
|
||||
return AssetSource{}, err
|
||||
}
|
||||
return AssetSource{FS: fsys, Root: cleanRoot}, nil
|
||||
}
|
||||
|
||||
func flattenAssetSources(sources []AssetSource) (fs.FS, error) {
|
||||
out := make(map[string][]byte)
|
||||
for _, source := range sources {
|
||||
if err := fs.WalkDir(source.FS, source.Root, func(name string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
if entry.IsDir() {
|
||||
return nil
|
||||
}
|
||||
rel := name
|
||||
if source.Root != "." {
|
||||
rel = strings.TrimPrefix(name, source.Root+"/")
|
||||
}
|
||||
rel, err := cleanAssetPath(rel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, exists := out[rel]; exists {
|
||||
return fmt.Errorf("duplicate asset path %q", rel)
|
||||
}
|
||||
data, err := fs.ReadFile(source.FS, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out[rel] = data
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("walk asset root %s: %w", source.Root, err)
|
||||
}
|
||||
}
|
||||
return readonlyfs.New(out)
|
||||
}
|
||||
|
||||
func cleanAssetRoot(root string) (string, error) {
|
||||
trimmed := strings.TrimSpace(root)
|
||||
if trimmed == "" || trimmed == "." {
|
||||
return ".", nil
|
||||
}
|
||||
return cleanAssetPath(trimmed)
|
||||
}
|
||||
|
||||
func cleanAssetPath(name string) (string, error) {
|
||||
trimmed := strings.TrimSpace(name)
|
||||
if trimmed == "" {
|
||||
return "", fmt.Errorf("path must not be empty")
|
||||
}
|
||||
cleaned := path.Clean(strings.TrimPrefix(trimmed, "/"))
|
||||
if cleaned == "." || !fs.ValidPath(cleaned) {
|
||||
return "", fmt.Errorf("invalid path %q", name)
|
||||
}
|
||||
return cleaned, nil
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func TestApplicationAssetsComposeStrictlyBeforeOperatorOverlay(t *testing.T) {
|
||||
for _, conflict := range []bool{false, true} {
|
||||
t.Run(fmt.Sprint("conflict=", conflict), func(t *testing.T) {
|
||||
registry := NewAssetRegistry()
|
||||
for _, id := range []string{"first", "second"} {
|
||||
if conflict {
|
||||
id = "first"
|
||||
}
|
||||
files := fstest.MapFS{"profiles.yaml": {Data: []byte(fmt.Sprintf("profiles:\n %s:\n backend: openrouter\n model: openai/gpt-5.6-luna\n", id))}}
|
||||
if err := registry.RegisterFallbackProfileFS(files, "."); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
operator := filepath.Join(t.TempDir(), "operator.yml")
|
||||
if err := os.WriteFile(operator, []byte("profiles:\n first:\n api_key_required: false\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
inspector, err := NewPromptKitProfileInspector(PromptKitProfileInspectorConfig{Assets: registry, Source: PromptKitProfileSourceConfig{ProfileFile: operator}})
|
||||
if conflict {
|
||||
if err == nil {
|
||||
t.Fatal("conflicting application profiles were silently overlaid")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, id := range []string{"first", "second"} {
|
||||
profile, err := inspector.InspectProfile(context.Background(), id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if profile.Model != "openai/gpt-5.6-luna" || profile.BackendID != "openrouter" {
|
||||
t.Fatalf("profile = %#v", profile)
|
||||
}
|
||||
if id == "first" && profile.CredentialRequired {
|
||||
t.Fatal("operator overlay was not applied")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,314 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
"time"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
)
|
||||
|
||||
func TestAssetRegistryCombinesPromptAndSchemaSources(t *testing.T) {
|
||||
registry := NewAssetRegistry()
|
||||
mustRegisterPromptFS(t, registry, fstest.MapFS{
|
||||
"prompts/test.yaml": {Data: []byte(validPromptYAML("schemas/out.json"))},
|
||||
"prompts/messages/user.tmpl": {Data: []byte(`Input: {{ input "transcript" }}`)},
|
||||
"prompts/messages/task.tmpl": {Data: []byte("Return JSON.")},
|
||||
"schemas/ignored/schema.json": {Data: []byte(`{"type":"object"}`)},
|
||||
}, ".")
|
||||
mustRegisterSchemaFS(t, registry, fstest.MapFS{
|
||||
"root/schemas/out.json": {Data: []byte(`{"type":"object"}`)},
|
||||
}, "root")
|
||||
|
||||
engine := newPromptKitAssetTestEngine(t, registry)
|
||||
prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{
|
||||
PromptID: "asset.test",
|
||||
ProfileID: "asset-test-profile",
|
||||
Inputs: map[string]promptkit.ArtifactRef{
|
||||
"transcript": promptkit.Inline(`{"ok":true}`),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare() error = %v, want nil", err)
|
||||
}
|
||||
if got := len(prepared.Messages); got != 2 {
|
||||
t.Fatalf("message count = %d, want 2", got)
|
||||
}
|
||||
if prepared.OutputContract.SchemaPath != "schemas/out.json" {
|
||||
t.Fatalf("schema path = %q, want schemas/out.json", prepared.OutputContract.SchemaPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetRegistryConstructionFailsForMissingPromptAsset(t *testing.T) {
|
||||
registry := NewAssetRegistry()
|
||||
mustRegisterPromptFS(t, registry, fstest.MapFS{
|
||||
"test.yaml": {Data: []byte(validPromptYAML("out.json"))},
|
||||
}, ".")
|
||||
mustRegisterSchemaFS(t, registry, fstest.MapFS{
|
||||
"out.json": {Data: []byte(`{"type":"object"}`)},
|
||||
}, ".")
|
||||
|
||||
sources, options, err := registry.PromptKitConfigurationSources()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sources.Underlays = append(sources.Underlays, configkit.Underlay{Inputs: []configkit.Input{{
|
||||
Source: promptkit.ProfileSource("asset test profile", map[string]promptkit.ProfileConfig{
|
||||
"asset-test-profile": {
|
||||
Endpoint: promptKitTestString("http://127.0.0.1:1/v1"),
|
||||
Model: promptKitTestString("asset-test-model"),
|
||||
},
|
||||
}),
|
||||
}}})
|
||||
_, err = promptkit.NewEngine(promptkit.Config{Sources: sources}, options...)
|
||||
if err == nil || !errors.Is(err, promptkit.ErrInvalidConfig) || !strings.Contains(err.Error(), "messages/user.tmpl") {
|
||||
t.Fatalf("NewEngine() error = %v, want invalid configuration for missing prompt content", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetRegistryPrepareFailsForMissingSchemaAsset(t *testing.T) {
|
||||
registry := NewAssetRegistry()
|
||||
mustRegisterPromptFS(t, registry, fstest.MapFS{
|
||||
"test.yaml": {Data: []byte(validPromptYAML("missing.json"))},
|
||||
"messages/user.tmpl": {Data: []byte(`Input: {{ input "transcript" }}`)},
|
||||
"messages/task.tmpl": {Data: []byte("Return JSON.")},
|
||||
}, ".")
|
||||
mustRegisterSchemaFS(t, registry, fstest.MapFS{
|
||||
"present.json": {Data: []byte(`{"type":"object"}`)},
|
||||
}, ".")
|
||||
|
||||
sources, options, err := registry.PromptKitConfigurationSources()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sources.Underlays = append(sources.Underlays, configkit.Underlay{Inputs: []configkit.Input{{
|
||||
Source: promptkit.ProfileSource("asset test profile", map[string]promptkit.ProfileConfig{
|
||||
"asset-test-profile": {
|
||||
Endpoint: promptKitTestString("http://127.0.0.1:1/v1"),
|
||||
Model: promptKitTestString("asset-test-model"),
|
||||
},
|
||||
}),
|
||||
}}})
|
||||
engine, err := promptkit.NewEngine(promptkit.Config{Sources: sources}, options...)
|
||||
if err != nil {
|
||||
t.Fatalf("NewEngine() error = %v, want nil", err)
|
||||
}
|
||||
_, err = engine.Prepare(context.Background(), promptkit.RunRequest{
|
||||
PromptID: "asset.test",
|
||||
ProfileID: "asset-test-profile",
|
||||
Inputs: map[string]promptkit.ArtifactRef{
|
||||
"transcript": promptkit.Inline(`{"ok":true}`),
|
||||
},
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "missing.json") {
|
||||
t.Fatalf("Prepare() error = %v, want missing schema asset failure", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetRegistryRejectsDuplicateAssetPaths(t *testing.T) {
|
||||
registry := NewAssetRegistry()
|
||||
mustRegisterPromptFS(t, registry, fstest.MapFS{"one/prompt.yaml": {Data: []byte("prompts: {}")}}, "one")
|
||||
mustRegisterPromptFS(t, registry, fstest.MapFS{"two/prompt.yaml": {Data: []byte("prompts: {}")}}, "two")
|
||||
|
||||
_, err := registry.PromptFS()
|
||||
if err == nil || !strings.Contains(err.Error(), "duplicate asset path") {
|
||||
t.Fatalf("PromptFS() error = %v, want duplicate path error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetRegistryCombinesFallbackProfileSources(t *testing.T) {
|
||||
registry := NewAssetRegistry()
|
||||
if err := registry.RegisterFallbackProfileFS(fstest.MapFS{
|
||||
"first/profiles/one.yaml": {Data: []byte("profiles:\n one:\n model: first\n")},
|
||||
}, "first/profiles"); err != nil {
|
||||
t.Fatalf("RegisterFallbackProfileFS() error = %v, want nil", err)
|
||||
}
|
||||
if err := registry.RegisterFallbackProfileFS(fstest.MapFS{
|
||||
"second/two.yaml": {Data: []byte("profiles:\n two:\n model: second\n")},
|
||||
}, "second"); err != nil {
|
||||
t.Fatalf("RegisterFallbackProfileFS() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
fallbackFS, err := registry.FallbackProfileFS()
|
||||
if err != nil {
|
||||
t.Fatalf("FallbackProfileFS() error = %v, want nil", err)
|
||||
}
|
||||
for _, name := range []string{"one.yaml", "two.yaml"} {
|
||||
if _, err := fs.ReadFile(fallbackFS, name); err != nil {
|
||||
t.Fatalf("FallbackProfileFS().ReadFile(%q) error = %v, want nil", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetRegistryRejectsInvalidFallbackProfileRoot(t *testing.T) {
|
||||
registry := NewAssetRegistry()
|
||||
err := registry.RegisterFallbackProfileFS(fstest.MapFS{}, "../profiles")
|
||||
if err == nil || !strings.Contains(err.Error(), "invalid path") {
|
||||
t.Fatalf("RegisterFallbackProfileFS() error = %v, want invalid root error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetRegistryRejectsUnreadableFallbackProfileAssets(t *testing.T) {
|
||||
registry := NewAssetRegistry()
|
||||
if err := registry.RegisterFallbackProfileFS(unreadableAssetFS{}, "."); err != nil {
|
||||
t.Fatalf("RegisterFallbackProfileFS() error = %v, want nil", err)
|
||||
}
|
||||
_, err := registry.FallbackProfileFS()
|
||||
if err == nil || !strings.Contains(err.Error(), "permission denied") {
|
||||
t.Fatalf("FallbackProfileFS() error = %v, want unreadable asset error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetRegistryRejectsDuplicateFallbackProfilePaths(t *testing.T) {
|
||||
registry := NewAssetRegistry()
|
||||
if err := registry.RegisterFallbackProfileFS(fstest.MapFS{"first/profile.yaml": {Data: []byte("profiles:\n first: {}\n")}}, "first"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := registry.RegisterFallbackProfileFS(fstest.MapFS{"second/profile.yaml": {Data: []byte("profiles:\n second: {}\n")}}, "second"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := registry.FallbackProfileFS()
|
||||
if err == nil || !strings.Contains(err.Error(), "duplicate asset path") {
|
||||
t.Fatalf("FallbackProfileFS() error = %v, want duplicate path error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetRegistryFallbackProfileDigestTracksContentWithoutLeakingIt(t *testing.T) {
|
||||
digestFor := func(content string) string {
|
||||
t.Helper()
|
||||
registry := NewAssetRegistry()
|
||||
if err := registry.RegisterFallbackProfileFS(fstest.MapFS{
|
||||
"profiles/profile.yaml": {Data: []byte(content)},
|
||||
}, "profiles"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
digest, err := registry.FallbackProfileDigest()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return digest
|
||||
}
|
||||
|
||||
first := digestFor("profiles:\n fallback:\n model: model-one\n")
|
||||
second := digestFor("profiles:\n fallback:\n model: model-two\n")
|
||||
if first == second {
|
||||
t.Fatalf("fallback profile digests = %q and %q, want content change", first, second)
|
||||
}
|
||||
if !strings.HasPrefix(first, "sha256:") || strings.Contains(first, "model-one") || strings.Contains(first, "profile.yaml") {
|
||||
t.Fatalf("fallback profile digest leaked source details: %q", first)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetRegistryCombinesNamespacedPromptSources(t *testing.T) {
|
||||
registry := NewAssetRegistry()
|
||||
mustRegisterPromptFS(t, registry, fstest.MapFS{
|
||||
"dnd.spells/dnd.spells.yaml": {Data: []byte(validPromptYAML("schema.json"))},
|
||||
"dnd.spells/task.md": {Data: []byte("spell task")},
|
||||
"dnd.spells/instructions.md": {Data: []byte("spell instructions")},
|
||||
}, ".")
|
||||
mustRegisterPromptFS(t, registry, fstest.MapFS{
|
||||
"dnd.scenes/dnd.scenes.yaml": {Data: []byte(validPromptYAML("schema.json"))},
|
||||
"dnd.scenes/task.md": {Data: []byte("scene task")},
|
||||
"dnd.scenes/instructions.md": {Data: []byte("scene instructions")},
|
||||
}, ".")
|
||||
|
||||
fsys, err := registry.PromptFS()
|
||||
if err != nil {
|
||||
t.Fatalf("PromptFS() error = %v, want nil", err)
|
||||
}
|
||||
for _, name := range []string{
|
||||
"dnd.spells/dnd.spells.yaml",
|
||||
"dnd.spells/task.md",
|
||||
"dnd.spells/instructions.md",
|
||||
"dnd.scenes/dnd.scenes.yaml",
|
||||
"dnd.scenes/task.md",
|
||||
"dnd.scenes/instructions.md",
|
||||
} {
|
||||
if _, err := fsys.Open(name); err != nil {
|
||||
t.Fatalf("PromptFS().Open(%q) error = %v, want nil", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashAssetsOmitsRawAssetContent(t *testing.T) {
|
||||
hash, err := HashAssets([]AssetHashPart{{
|
||||
FS: fstest.MapFS{"prompt.md": {Data: []byte("secret prompt text")}},
|
||||
Path: "prompt.md",
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatalf("HashAssets() error = %v, want nil", err)
|
||||
}
|
||||
if !strings.HasPrefix(hash, "sha256:") {
|
||||
t.Fatalf("hash = %q, want sha256-prefixed value", hash)
|
||||
}
|
||||
if strings.Contains(hash, "secret prompt text") {
|
||||
t.Fatalf("hash leaked asset content")
|
||||
}
|
||||
}
|
||||
|
||||
func newPromptKitAssetTestEngine(t *testing.T, registry *AssetRegistry) *promptkit.Engine {
|
||||
t.Helper()
|
||||
sources, options, err := registry.PromptKitConfigurationSources()
|
||||
if err != nil {
|
||||
t.Fatalf("PromptKitConfigurationSources() error = %v, want nil", err)
|
||||
}
|
||||
sources.Underlays = append(sources.Underlays, configkit.Underlay{Inputs: []configkit.Input{{
|
||||
Source: promptkit.ProfileSource("asset test profile", map[string]promptkit.ProfileConfig{
|
||||
"asset-test-profile": {
|
||||
Endpoint: promptKitTestString("http://127.0.0.1:1/v1"),
|
||||
Model: promptKitTestString("asset-test-model"),
|
||||
},
|
||||
}),
|
||||
}}})
|
||||
engine, err := promptkit.NewEngine(promptkit.Config{Sources: sources, Timeout: time.Second}, options...)
|
||||
if err != nil {
|
||||
t.Fatalf("NewEngine() error = %v, want nil", err)
|
||||
}
|
||||
return engine
|
||||
}
|
||||
|
||||
func mustRegisterPromptFS(t *testing.T, registry *AssetRegistry, fsys fstest.MapFS, root string) {
|
||||
t.Helper()
|
||||
if err := registry.RegisterPromptFS(fsys, root); err != nil {
|
||||
t.Fatalf("RegisterPromptFS() error = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustRegisterSchemaFS(t *testing.T, registry *AssetRegistry, fsys fstest.MapFS, root string) {
|
||||
t.Helper()
|
||||
if err := registry.RegisterSchemaFS(fsys, root); err != nil {
|
||||
t.Fatalf("RegisterSchemaFS() error = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func validPromptYAML(schemaPath string) string {
|
||||
return `prompts:
|
||||
asset.test:
|
||||
v1:
|
||||
inputs:
|
||||
- name: transcript
|
||||
required: true
|
||||
content_type: application/json
|
||||
messages:
|
||||
- role: user
|
||||
content_file: ./messages/user.tmpl
|
||||
- role: user
|
||||
content_file: ./messages/task.tmpl
|
||||
output:
|
||||
format: json
|
||||
validation_mode: json_schema
|
||||
schema_path: ` + schemaPath + `
|
||||
repair_attempts: 0
|
||||
`
|
||||
}
|
||||
|
||||
type unreadableAssetFS struct{}
|
||||
|
||||
func (unreadableAssetFS) Open(name string) (fs.File, error) {
|
||||
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrPermission}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package llm
|
||||
|
||||
// CheckpointFingerprint is a stable, non-secret semantic identity contributed
|
||||
// by the LLM runtime before pipeline execution.
|
||||
type CheckpointFingerprint struct {
|
||||
Name string
|
||||
Value string
|
||||
}
|
||||
|
||||
// CheckpointFingerprintProvider exposes LLM-runtime identities that must
|
||||
// participate in checkpoint composition.
|
||||
type CheckpointFingerprintProvider interface {
|
||||
LLMCheckpointFingerprints() ([]CheckpointFingerprint, error)
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type retryableError struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (e retryableError) Error() string {
|
||||
if e.err == nil {
|
||||
return ""
|
||||
}
|
||||
return e.err.Error()
|
||||
}
|
||||
|
||||
func (e retryableError) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
func validateOutputTarget(out any) error {
|
||||
if out == nil {
|
||||
return fmt.Errorf("structured completion output target must be a non-nil pointer")
|
||||
}
|
||||
value := reflect.ValueOf(out)
|
||||
if value.Kind() != reflect.Pointer || value.IsNil() {
|
||||
return fmt.Errorf("structured completion output target must be a non-nil pointer")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func canRetry(ctx context.Context, attempt int, maxRetries int, err error) bool {
|
||||
if attempt >= maxRetries {
|
||||
return false
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return false
|
||||
}
|
||||
var retryable retryableError
|
||||
return errors.As(err, &retryable)
|
||||
}
|
||||
|
||||
func firstNonEmpty(values ...string) string {
|
||||
for _, value := range values {
|
||||
if trimmed := strings.TrimSpace(value); trimmed != "" {
|
||||
return trimmed
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -1,569 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/artifacts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
)
|
||||
|
||||
const promptKitProviderName = "promptkit"
|
||||
|
||||
type PromptKitLocalBackendConfig struct {
|
||||
Endpoint string
|
||||
ConcurrencyLimit int
|
||||
}
|
||||
|
||||
type PromptKitClientConfig struct {
|
||||
ProfileDir string
|
||||
ProfileFile string
|
||||
Sources promptkit.ConfigurationSources
|
||||
TransientRetries *int
|
||||
LocalBackend *PromptKitLocalBackendConfig
|
||||
Assets *AssetRegistry
|
||||
Timeout time.Duration
|
||||
HTTPClient *http.Client
|
||||
EngineOptions []promptkit.Option
|
||||
Recorder *LLMProfileRecorder
|
||||
ReasoningEffort *string
|
||||
}
|
||||
|
||||
type PromptKitClient struct {
|
||||
engine *promptkit.Engine
|
||||
recorder *LLMProfileRecorder
|
||||
checkpointFingerprints []CheckpointFingerprint
|
||||
reasoningEffort *string
|
||||
}
|
||||
|
||||
type LLMProfileRecorder struct {
|
||||
mu sync.Mutex
|
||||
profiles map[string]artifacts.LLMProfileManifest
|
||||
}
|
||||
|
||||
type promptKitClientConfigurationError struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *promptKitClientConfigurationError) Error() string {
|
||||
return "PromptKit client configuration is invalid or unreadable"
|
||||
}
|
||||
|
||||
func (e *promptKitClientConfigurationError) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
var _ contracts.StructuredLLMClient = (*PromptKitClient)(nil)
|
||||
var _ contracts.LLMProfileManifestProvider = (*PromptKitClient)(nil)
|
||||
|
||||
func NewPromptKitClient(cfg PromptKitClientConfig) (*PromptKitClient, error) {
|
||||
if cfg.Assets == nil {
|
||||
return nil, fmt.Errorf("PromptKit client assets must not be nil")
|
||||
}
|
||||
_, sources, options, err := promptKitEngineConfiguration(PromptKitProfileSourceConfig{
|
||||
ProfileDir: cfg.ProfileDir,
|
||||
ProfileFile: cfg.ProfileFile,
|
||||
LocalBackend: cfg.LocalBackend,
|
||||
}, cfg.Assets, cfg.Sources)
|
||||
if err != nil {
|
||||
return nil, &promptKitClientConfigurationError{err: err}
|
||||
}
|
||||
options = append(options, cfg.EngineOptions...)
|
||||
|
||||
engine, err := promptkit.NewEngine(promptkit.Config{
|
||||
Sources: sources,
|
||||
Timeout: cfg.Timeout,
|
||||
HTTPClient: cfg.HTTPClient,
|
||||
TransientRetries: cfg.TransientRetries,
|
||||
}, options...)
|
||||
if err != nil {
|
||||
return nil, &promptKitClientConfigurationError{err: err}
|
||||
}
|
||||
fingerprint, err := promptKitConfigurationFingerprint(engine)
|
||||
if err != nil {
|
||||
return nil, &promptKitClientConfigurationError{err: err}
|
||||
}
|
||||
recorder := cfg.Recorder
|
||||
if recorder == nil {
|
||||
recorder = NewLLMProfileRecorder()
|
||||
}
|
||||
var reasoningEffort *string
|
||||
if cfg.ReasoningEffort != nil {
|
||||
value := *cfg.ReasoningEffort
|
||||
reasoningEffort = &value
|
||||
}
|
||||
return &PromptKitClient{
|
||||
engine: engine,
|
||||
recorder: recorder,
|
||||
checkpointFingerprints: []CheckpointFingerprint{fingerprint},
|
||||
reasoningEffort: reasoningEffort,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *PromptKitClient) CompleteStructured(ctx context.Context, req contracts.StructuredCompletionRequest, out any) (contracts.StructuredCompletionResponse, error) {
|
||||
if c == nil {
|
||||
return contracts.StructuredCompletionResponse{}, fmt.Errorf("PromptKit client must not be nil")
|
||||
}
|
||||
if c.engine == nil {
|
||||
return contracts.StructuredCompletionResponse{}, fmt.Errorf("PromptKit client engine must not be nil")
|
||||
}
|
||||
if err := validateOutputTarget(out); err != nil {
|
||||
return contracts.StructuredCompletionResponse{}, err
|
||||
}
|
||||
if req.StructuredOutputRepairAttempts != nil && (*req.StructuredOutputRepairAttempts < 0 || *req.StructuredOutputRepairAttempts > 3) {
|
||||
return contracts.StructuredCompletionResponse{}, fmt.Errorf("structured output repair attempts must be between zero and three")
|
||||
}
|
||||
appendedMessages, err := promptKitCorrectionMessages(req.Correction)
|
||||
if err != nil {
|
||||
return contracts.StructuredCompletionResponse{}, fmt.Errorf("structured completion correction: %w", err)
|
||||
}
|
||||
promptID := strings.TrimSpace(req.PromptID)
|
||||
if promptID == "" {
|
||||
return contracts.StructuredCompletionResponse{}, fmt.Errorf("structured completion prompt_id must not be empty")
|
||||
}
|
||||
sessionID := strings.TrimSpace(req.SessionID)
|
||||
var targetOverride *promptkit.ExecutionTargetOverride
|
||||
if c.reasoningEffort != nil {
|
||||
reasoningEffort := *c.reasoningEffort
|
||||
targetOverride = &promptkit.ExecutionTargetOverride{
|
||||
ReasoningEffort: &reasoningEffort,
|
||||
}
|
||||
}
|
||||
|
||||
runReq := promptkit.RunRequest{
|
||||
PromptID: promptID,
|
||||
PromptVersion: strings.TrimSpace(req.PromptVersion),
|
||||
ProfileID: strings.TrimSpace(req.ProfileID),
|
||||
SessionID: sessionID,
|
||||
Inputs: promptKitInputs(req.Inputs),
|
||||
Vars: promptKitVars(req, sessionID),
|
||||
Execution: targetOverride,
|
||||
AppendedMessages: appendedMessages,
|
||||
}
|
||||
var executionSummary *contracts.LLMExecutionSummary
|
||||
runReq.Observer = func(event promptkit.ExecutionEvent) {
|
||||
if event.Kind == promptkit.ExecutionFinished {
|
||||
executionSummary = promptKitExecutionSummary(event)
|
||||
}
|
||||
}
|
||||
if req.StructuredOutputRepairAttempts != nil {
|
||||
inspection, err := c.engine.InspectPrompt(ctx, promptID, strings.TrimSpace(req.PromptVersion))
|
||||
if err != nil {
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
return contracts.StructuredCompletionResponse{}, ctxErr
|
||||
}
|
||||
return contracts.StructuredCompletionResponse{}, fmt.Errorf("inspect PromptKit prompt %q: %v", promptID, redactPromptKitError(err))
|
||||
}
|
||||
contract := inspection.OutputContract
|
||||
contract.RepairAttempts = *req.StructuredOutputRepairAttempts
|
||||
runReq.Validation = &contract
|
||||
}
|
||||
prepared, err := c.engine.PrepareExecution(ctx, runReq)
|
||||
if err != nil {
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
return contracts.StructuredCompletionResponse{}, ctxErr
|
||||
}
|
||||
return contracts.StructuredCompletionResponse{}, fmt.Errorf("prepare PromptKit prompt %q: %v", promptID, redactPromptKitError(err))
|
||||
}
|
||||
defer prepared.Discard()
|
||||
preparedDetails := prepared.Details()
|
||||
result, err := c.engine.RunPrepared(ctx, prepared)
|
||||
if err != nil {
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
return promptKitResponseWithExecution(contracts.StructuredCompletionResponse{}, executionSummary), ctxErr
|
||||
}
|
||||
if errors.Is(err, promptkit.ErrCapacityExceeded) {
|
||||
var capacityErr *promptkit.CapacityError
|
||||
if errors.As(err, &capacityErr) && strings.TrimSpace(capacityErr.BackendID) != "" {
|
||||
return promptKitResponseWithExecution(contracts.StructuredCompletionResponse{}, executionSummary), fmt.Errorf(
|
||||
"run PromptKit prompt %q on backend %q: %w: %v",
|
||||
promptID,
|
||||
strings.TrimSpace(capacityErr.BackendID),
|
||||
contracts.ErrLLMCapacityExceeded,
|
||||
redactPromptKitError(err),
|
||||
)
|
||||
}
|
||||
return promptKitResponseWithExecution(contracts.StructuredCompletionResponse{}, executionSummary), fmt.Errorf(
|
||||
"run PromptKit prompt %q: %w: %v",
|
||||
promptID,
|
||||
contracts.ErrLLMCapacityExceeded,
|
||||
redactPromptKitError(err),
|
||||
)
|
||||
}
|
||||
if errors.Is(err, promptkit.ErrLLMGenerate) {
|
||||
var generationErr *promptkit.GenerationError
|
||||
var exhaustedErr *promptkit.RetryExhaustedError
|
||||
status := 0
|
||||
metadata := contracts.LLMGenerationMetadata{FailureKind: promptKitGenerationFailureKind(err)}
|
||||
if errors.As(err, &exhaustedErr) {
|
||||
metadata.RetriesExhausted = true
|
||||
metadata.Attempts = exhaustedErr.Attempts()
|
||||
}
|
||||
response := contracts.StructuredCompletionResponse{Debug: &contracts.LLMDebugMaterial{Prompt: promptKitDebugPrompt(&preparedDetails)}}
|
||||
if errors.As(err, &generationErr) {
|
||||
status = generationErr.StatusCode()
|
||||
response.Debug.Response = promptKitDebugGenerationError(&preparedDetails, generationErr)
|
||||
}
|
||||
return promptKitResponseWithExecution(response, executionSummary), contracts.NewLLMGenerationErrorWithMetadata(status, fmt.Sprintf("run PromptKit prompt %q: %v", promptID, redactPromptKitError(err)), metadata)
|
||||
}
|
||||
return promptKitResponseWithExecution(contracts.StructuredCompletionResponse{}, executionSummary), fmt.Errorf("run PromptKit prompt %q: %v", promptID, redactPromptKitError(err))
|
||||
}
|
||||
if result == nil {
|
||||
return promptKitResponseWithExecution(contracts.StructuredCompletionResponse{}, executionSummary), fmt.Errorf("run PromptKit prompt %q: %w: empty result", promptID, contracts.ErrInvalidStructuredOutput)
|
||||
}
|
||||
response := promptKitResponseWithExecution(c.responseFromResult(result, &preparedDetails), executionSummary)
|
||||
if result.Validation.Status == promptkit.ValidationFailed || !result.Validation.IsValid {
|
||||
return response, fmt.Errorf("run PromptKit prompt %q: %w: validation failed: %s", promptID, contracts.ErrInvalidStructuredOutput, strings.Join(result.Validation.Errors, "; "))
|
||||
}
|
||||
if len(strings.TrimSpace(string(response.Content))) == 0 {
|
||||
return response, fmt.Errorf("run PromptKit prompt %q: %w: empty structured output", promptID, contracts.ErrInvalidStructuredOutput)
|
||||
}
|
||||
if err := json.Unmarshal(response.Content, out); err != nil {
|
||||
return response, fmt.Errorf("decode PromptKit structured output for prompt %q: %w: %w", promptID, contracts.ErrInvalidStructuredOutput, err)
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func promptKitResponseWithExecution(response contracts.StructuredCompletionResponse, execution *contracts.LLMExecutionSummary) contracts.StructuredCompletionResponse {
|
||||
response.Execution = execution
|
||||
return response
|
||||
}
|
||||
|
||||
func promptKitExecutionSummary(event promptkit.ExecutionEvent) *contracts.LLMExecutionSummary {
|
||||
count := func(value promptkit.ObservedTokenCount) contracts.LLMObservedTokenCount {
|
||||
return contracts.LLMObservedTokenCount{Value: value.Value, Known: value.Known, Complete: value.Complete}
|
||||
}
|
||||
return &contracts.LLMExecutionSummary{
|
||||
Attempts: event.Attempts, TransientRetries: event.TransientRetries, RepairCalls: event.RepairCalls,
|
||||
CapacityWaitMS: event.CapacityWait.Milliseconds(), GenerationMS: event.GenerationDuration.Milliseconds(), RetryDelayMS: event.RetryDelay.Milliseconds(), DurationMS: event.Duration.Milliseconds(),
|
||||
Outcome: promptKitExecutionOutcome(event.Outcome), FailureKind: promptKitExecutionFailure(event.Failure), StatusCode: event.StatusCode, RetriesExhausted: event.RetriesExhausted,
|
||||
Usage: contracts.LLMObservedTokenUsage{PromptTokens: count(event.Usage.PromptTokens), CompletionTokens: count(event.Usage.CompletionTokens), TotalTokens: count(event.Usage.TotalTokens), CachedTokens: count(event.Usage.CachedTokens), CacheWriteTokens: count(event.Usage.CacheWriteTokens)},
|
||||
}
|
||||
}
|
||||
|
||||
func promptKitExecutionOutcome(value promptkit.ExecutionOutcome) string {
|
||||
switch value {
|
||||
case promptkit.ExecutionOutcomeSuccess:
|
||||
return "success"
|
||||
case promptkit.ExecutionOutcomeInvalidOutput:
|
||||
return "invalid_output"
|
||||
case promptkit.ExecutionOutcomeError:
|
||||
return "error"
|
||||
default:
|
||||
return "other"
|
||||
}
|
||||
}
|
||||
|
||||
func promptKitExecutionFailure(value promptkit.ExecutionFailureKind) string {
|
||||
switch value {
|
||||
case promptkit.ExecutionFailureNone:
|
||||
return ""
|
||||
case promptkit.ExecutionFailureCanceled, promptkit.ExecutionFailureDeadline, promptkit.ExecutionFailureTransport, promptkit.ExecutionFailureProviderStatus, promptkit.ExecutionFailureMalformedResponse, promptkit.ExecutionFailureCapacity, promptkit.ExecutionFailureInvalidConfig, promptkit.ExecutionFailureInvalidRequest, promptkit.ExecutionFailurePromptNotFound, promptkit.ExecutionFailureProfileNotFound, promptkit.ExecutionFailurePromptLoad, promptkit.ExecutionFailureProfileLoad, promptkit.ExecutionFailureArtifactLoad, promptkit.ExecutionFailurePromptRender, promptkit.ExecutionFailureValidation, promptkit.ExecutionFailureGeneration:
|
||||
return string(value)
|
||||
default:
|
||||
return "other"
|
||||
}
|
||||
}
|
||||
|
||||
func promptKitGenerationFailureKind(err error) contracts.LLMGenerationFailureKind {
|
||||
switch {
|
||||
case errors.Is(err, promptkit.ErrTransport):
|
||||
return contracts.LLMGenerationFailureTransport
|
||||
case errors.Is(err, promptkit.ErrMalformedResponse):
|
||||
return contracts.LLMGenerationFailureMalformedResponse
|
||||
}
|
||||
var generationErr *promptkit.GenerationError
|
||||
if errors.As(err, &generationErr) && generationErr.StatusCode() != 0 {
|
||||
return contracts.LLMGenerationFailureProviderStatus
|
||||
}
|
||||
return contracts.LLMGenerationFailureGeneration
|
||||
}
|
||||
|
||||
func promptKitCorrectionMessages(correction *contracts.SemanticCorrection) ([]promptkit.RenderedMessage, error) {
|
||||
owned, err := contracts.CloneSemanticCorrection(correction)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if owned == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return []promptkit.RenderedMessage{
|
||||
{Role: promptkit.RoleAssistant, Content: string(owned.AssistantResponse)},
|
||||
{Role: promptkit.RoleUser, Content: owned.UserGuidance},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func promptKitDebugGenerationError(prepared *promptkit.PreparedRun, generationErr *promptkit.GenerationError) *contracts.LLMDebugResponse {
|
||||
if generationErr == nil {
|
||||
return nil
|
||||
}
|
||||
var secrets []string
|
||||
if prepared != nil && strings.TrimSpace(prepared.EffectiveModelParams.APIKeyEnv) != "" {
|
||||
if value, ok := os.LookupEnv(prepared.EffectiveModelParams.APIKeyEnv); ok {
|
||||
secrets = append(secrets, value)
|
||||
}
|
||||
}
|
||||
redact := func(value string) string {
|
||||
return bearerTokenPattern.ReplaceAllString(RedactSecrets(value, secrets), "Bearer "+secretReplacement)
|
||||
}
|
||||
return &contracts.LLMDebugResponse{ProviderError: &contracts.LLMDebugProviderError{
|
||||
StatusCode: generationErr.StatusCode(),
|
||||
Code: redact(generationErr.ProviderCode()),
|
||||
Type: redact(generationErr.ProviderType()),
|
||||
Message: redact(generationErr.ProviderMessage()),
|
||||
}}
|
||||
}
|
||||
|
||||
func (c *PromptKitClient) responseFromResult(result *promptkit.RunResult, prepared *promptkit.PreparedRun) contracts.StructuredCompletionResponse {
|
||||
content := result.Artifact.Body
|
||||
if len(content) == 0 {
|
||||
content = []byte(result.RawOutput)
|
||||
}
|
||||
profile := artifacts.LLMProfileManifest{
|
||||
ID: strings.TrimSpace(result.SelectedProfileID),
|
||||
Provider: promptKitProviderName,
|
||||
Model: firstNonEmpty(result.ModelName, result.EffectiveModelParams.Model),
|
||||
BackendID: strings.TrimSpace(result.SelectedBackendID),
|
||||
ReasoningEffort: strings.TrimSpace(result.EffectiveModelParams.ReasoningEffort),
|
||||
}
|
||||
if c.recorder != nil {
|
||||
c.recorder.Record(profile)
|
||||
}
|
||||
return contracts.StructuredCompletionResponse{
|
||||
Content: append(json.RawMessage(nil), content...),
|
||||
Provider: profile.Provider,
|
||||
Model: profile.Model,
|
||||
ProfileID: profile.ID,
|
||||
PromptTokens: result.Usage.PromptTokens,
|
||||
CompletionTokens: result.Usage.CompletionTokens,
|
||||
TotalTokens: result.Usage.TotalTokens,
|
||||
RepairAttempts: result.Validation.RepairAttempts,
|
||||
Debug: promptKitDebugMaterial(prepared, result),
|
||||
}
|
||||
}
|
||||
|
||||
func promptKitDebugMaterial(prepared *promptkit.PreparedRun, result *promptkit.RunResult) *contracts.LLMDebugMaterial {
|
||||
material := &contracts.LLMDebugMaterial{}
|
||||
if prepared != nil {
|
||||
material.Prompt = promptKitDebugPrompt(prepared)
|
||||
}
|
||||
if result != nil {
|
||||
material.Response = promptKitDebugResponse(result)
|
||||
}
|
||||
if material.Prompt == nil && material.Response == nil {
|
||||
return nil
|
||||
}
|
||||
return material
|
||||
}
|
||||
|
||||
func promptKitDebugPrompt(prepared *promptkit.PreparedRun) *contracts.LLMDebugPrompt {
|
||||
if prepared == nil {
|
||||
return nil
|
||||
}
|
||||
messages := make([]contracts.LLMDebugMessage, 0, len(prepared.Messages))
|
||||
for _, message := range prepared.Messages {
|
||||
messages = append(messages, contracts.LLMDebugMessage{
|
||||
Role: message.Role,
|
||||
Content: message.Content,
|
||||
CacheControl: jsonObject(message.CacheControl),
|
||||
})
|
||||
}
|
||||
return &contracts.LLMDebugPrompt{
|
||||
PromptID: prepared.PromptID,
|
||||
PromptVersion: prepared.PromptVersion,
|
||||
PromptHash: prepared.PromptHash,
|
||||
SelectedProfileID: prepared.SelectedProfileID,
|
||||
SelectedBackendID: prepared.SelectedBackendID,
|
||||
SessionID: prepared.SessionID,
|
||||
RenderedPromptHash: prepared.RenderedPromptHash,
|
||||
Messages: messages,
|
||||
EffectiveModelParams: jsonObject(prepared.EffectiveModelParams),
|
||||
OutputContract: jsonObject(prepared.OutputContract),
|
||||
StructuredOutput: jsonObject(prepared.StructuredOutput),
|
||||
InputHashes: cloneStringMap(prepared.InputHashes),
|
||||
}
|
||||
}
|
||||
|
||||
func promptKitDebugResponse(result *promptkit.RunResult) *contracts.LLMDebugResponse {
|
||||
if result == nil {
|
||||
return nil
|
||||
}
|
||||
content := result.RawOutput
|
||||
if content == "" {
|
||||
content = string(result.Artifact.Body)
|
||||
}
|
||||
return &contracts.LLMDebugResponse{
|
||||
Content: content,
|
||||
RunID: result.RunID,
|
||||
PromptID: result.PromptID,
|
||||
PromptVersion: result.PromptVersion,
|
||||
PromptHash: result.PromptHash,
|
||||
RenderedPromptHash: result.RenderedPromptHash,
|
||||
SelectedProfileID: result.SelectedProfileID,
|
||||
ModelName: result.ModelName,
|
||||
Endpoint: result.Endpoint,
|
||||
EffectiveModelParams: jsonObject(result.EffectiveModelParams),
|
||||
InputHashes: cloneStringMap(result.InputHashes),
|
||||
Validation: jsonObject(result.Validation),
|
||||
Usage: contracts.LLMDebugUsage{
|
||||
PromptTokens: result.Usage.PromptTokens,
|
||||
CompletionTokens: result.Usage.CompletionTokens,
|
||||
TotalTokens: result.Usage.TotalTokens,
|
||||
CachedTokens: result.Usage.CachedTokens,
|
||||
CacheWriteTokens: result.Usage.CacheWriteTokens,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func jsonObject(value any) map[string]any {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil || string(data) == "null" {
|
||||
return nil
|
||||
}
|
||||
var out map[string]any
|
||||
if err := json.Unmarshal(data, &out); err != nil {
|
||||
return nil
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneStringMap(values map[string]string) map[string]string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]string, len(values))
|
||||
for key, value := range values {
|
||||
out[key] = value
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (c *PromptKitClient) LLMProfileManifests() []artifacts.LLMProfileManifest {
|
||||
if c == nil || c.recorder == nil {
|
||||
return nil
|
||||
}
|
||||
return c.recorder.Manifests()
|
||||
}
|
||||
|
||||
func (c *PromptKitClient) LLMCheckpointFingerprints() ([]CheckpointFingerprint, error) {
|
||||
if c == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return append([]CheckpointFingerprint(nil), c.checkpointFingerprints...), nil
|
||||
}
|
||||
|
||||
func NewLLMProfileRecorder() *LLMProfileRecorder {
|
||||
return &LLMProfileRecorder{profiles: map[string]artifacts.LLMProfileManifest{}}
|
||||
}
|
||||
|
||||
func (r *LLMProfileRecorder) Record(profile artifacts.LLMProfileManifest) {
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
profile = profile.Normalized()
|
||||
key := profile.IdentityKey()
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.profiles == nil {
|
||||
r.profiles = map[string]artifacts.LLMProfileManifest{}
|
||||
}
|
||||
r.profiles[key] = profile
|
||||
}
|
||||
|
||||
func (r *LLMProfileRecorder) Manifests() []artifacts.LLMProfileManifest {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if len(r.profiles) == 0 {
|
||||
return nil
|
||||
}
|
||||
keys := make([]string, 0, len(r.profiles))
|
||||
for key := range r.profiles {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
out := make([]artifacts.LLMProfileManifest, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
out = append(out, r.profiles[key])
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func promptKitInputs(inputs contracts.LLMInputSet) map[string]promptkit.ArtifactRef {
|
||||
if len(inputs) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]promptkit.ArtifactRef, len(inputs))
|
||||
for key, material := range inputs {
|
||||
name := strings.TrimSpace(key)
|
||||
if name == "" {
|
||||
name = strings.TrimSpace(material.Name)
|
||||
}
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
body := string(material.Content)
|
||||
if body == "" {
|
||||
body = " "
|
||||
}
|
||||
if origin := strings.TrimSpace(material.OriginURI); origin != "" {
|
||||
out[name] = promptkit.InlineWithURI(origin, body)
|
||||
} else {
|
||||
out[name] = promptkit.Inline(body)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func promptKitVars(req contracts.StructuredCompletionRequest, sessionID string) map[string]string {
|
||||
vars := make(map[string]string, len(req.Vars)+1)
|
||||
for key, value := range req.Vars {
|
||||
name := strings.TrimSpace(key)
|
||||
if name == "" || value == nil {
|
||||
continue
|
||||
}
|
||||
vars[name] = fmt.Sprint(value)
|
||||
}
|
||||
if sessionID != "" {
|
||||
vars["session_id"] = sessionID
|
||||
}
|
||||
if len(vars) == 0 {
|
||||
return nil
|
||||
}
|
||||
return vars
|
||||
}
|
||||
|
||||
var bearerTokenPattern = regexp.MustCompile(`(?i)Bearer\s+[A-Za-z0-9._~+/=-]+`)
|
||||
|
||||
func redactPromptKitError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return sanitizedProviderDiagnostic{message: bearerTokenPattern.ReplaceAllString(err.Error(), "Bearer "+secretReplacement)}
|
||||
}
|
||||
|
||||
type sanitizedProviderDiagnostic struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (e sanitizedProviderDiagnostic) Error() string {
|
||||
return e.message
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,129 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
"go.yaml.in/yaml/v4"
|
||||
)
|
||||
|
||||
const promptKitConfigurationFingerprintName = "promptkit_configuration"
|
||||
|
||||
// This version describes the public definition graph captured by the engine.
|
||||
// Include the release and catalog revisions because they contribute built-in
|
||||
// profiles and other effective definitions without appearing in app sources.
|
||||
const promptKitConfigurationSnapshotMarker = "promptkit:v0.12.0:openrouter:v3.20260930.0:rakestrawhome:v3.20260930.0:configuration-snapshot:v1"
|
||||
|
||||
type promptKitConfigurationFingerprintPayload struct {
|
||||
SnapshotMarker string `json:"snapshot_marker"`
|
||||
Configuration map[string]any `json:"configuration"`
|
||||
Profiles []promptKitProfileFingerprintEntry `json:"profiles"`
|
||||
Prompts []promptKitPromptFingerprintEntry `json:"prompts"`
|
||||
}
|
||||
|
||||
type promptKitProfileFingerprintEntry struct {
|
||||
ID string `json:"id"`
|
||||
Target promptkit.ExecutionTarget `json:"target"`
|
||||
APIKeyRequired bool `json:"api_key_required"`
|
||||
}
|
||||
|
||||
type promptKitPromptFingerprintEntry struct {
|
||||
ID string `json:"id"`
|
||||
Version string `json:"version"`
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
|
||||
func promptKitConfigurationFingerprint(engine *promptkit.Engine) (CheckpointFingerprint, error) {
|
||||
if engine == nil {
|
||||
return CheckpointFingerprint{}, fmt.Errorf("PromptKit engine is unavailable for checkpoint identity")
|
||||
}
|
||||
|
||||
inspection, err := engine.InspectConfiguration()
|
||||
if err != nil {
|
||||
return CheckpointFingerprint{}, fmt.Errorf("inspect captured PromptKit configuration for checkpoint identity")
|
||||
}
|
||||
var report bytes.Buffer
|
||||
if err := inspection.WriteEffective(&report); err != nil {
|
||||
return CheckpointFingerprint{}, fmt.Errorf("write captured PromptKit configuration for checkpoint identity")
|
||||
}
|
||||
var configuration map[string]any
|
||||
if err := yaml.Unmarshal(report.Bytes(), &configuration); err != nil {
|
||||
return CheckpointFingerprint{}, fmt.Errorf("decode captured PromptKit configuration for checkpoint identity")
|
||||
}
|
||||
if configuration == nil {
|
||||
configuration = map[string]any{}
|
||||
}
|
||||
removeLocalSchedulingFields(configuration)
|
||||
|
||||
payload := promptKitConfigurationFingerprintPayload{
|
||||
SnapshotMarker: promptKitConfigurationSnapshotMarker,
|
||||
Configuration: configuration,
|
||||
}
|
||||
profileIDs := sortedMapKeys(configuration["profiles"])
|
||||
payload.Profiles = make([]promptKitProfileFingerprintEntry, 0, len(profileIDs))
|
||||
for _, id := range profileIDs {
|
||||
profile, err := engine.InspectProfile(context.Background(), id)
|
||||
if err != nil {
|
||||
return CheckpointFingerprint{}, fmt.Errorf("inspect captured PromptKit profile for checkpoint identity")
|
||||
}
|
||||
payload.Profiles = append(payload.Profiles, promptKitProfileFingerprintEntry{
|
||||
ID: profile.ProfileID,
|
||||
Target: profile.EffectiveModelParams,
|
||||
APIKeyRequired: profile.APIKeyRequired,
|
||||
})
|
||||
}
|
||||
|
||||
promptDefinitions, _ := configuration["prompts"].(map[string]any)
|
||||
var promptIDs []string
|
||||
for id := range promptDefinitions {
|
||||
promptIDs = append(promptIDs, id)
|
||||
}
|
||||
sort.Strings(promptIDs)
|
||||
for _, id := range promptIDs {
|
||||
versions := sortedMapKeys(promptDefinitions[id])
|
||||
for _, version := range versions {
|
||||
prompt, err := engine.InspectPrompt(context.Background(), id, version)
|
||||
if err != nil {
|
||||
return CheckpointFingerprint{}, fmt.Errorf("inspect captured PromptKit prompt for checkpoint identity")
|
||||
}
|
||||
payload.Prompts = append(payload.Prompts, promptKitPromptFingerprintEntry{
|
||||
ID: prompt.PromptID,
|
||||
Version: prompt.PromptVersion,
|
||||
Hash: prompt.PromptHash,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
encoded, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return CheckpointFingerprint{}, fmt.Errorf("encode captured PromptKit checkpoint identity")
|
||||
}
|
||||
digest := sha256.Sum256(encoded)
|
||||
return CheckpointFingerprint{
|
||||
Name: promptKitConfigurationFingerprintName,
|
||||
Value: "sha256:" + hex.EncodeToString(digest[:]),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func sortedMapKeys(value any) []string {
|
||||
values, _ := value.(map[string]any)
|
||||
keys := make([]string, 0, len(values))
|
||||
for key := range values {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
|
||||
func removeLocalSchedulingFields(configuration map[string]any) {
|
||||
backends, _ := configuration["backends"].(map[string]any)
|
||||
local, _ := backends["local"].(map[string]any)
|
||||
delete(local, "concurrency_limit")
|
||||
delete(local, "queue_capacity")
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
)
|
||||
|
||||
var ErrInvalidPromptKitConfigurationPointer = errors.New("invalid PromptKit configuration pointer")
|
||||
|
||||
// PromptKitEffectiveModelParams is the provider-neutral JSON projection of an
|
||||
// inspected profile target. Credential-shaped extra-parameter values are
|
||||
// redacted in this reporting copy.
|
||||
type PromptKitEffectiveModelParams struct {
|
||||
BackendID string `json:"backend_id"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Model string `json:"model"`
|
||||
Temperature float64 `json:"temperature"`
|
||||
MaxTokens int `json:"max_tokens"`
|
||||
TopP float64 `json:"top_p"`
|
||||
TimeoutSeconds int `json:"timeout_seconds"`
|
||||
ServiceTier string `json:"service_tier"`
|
||||
ReasoningEffort string `json:"reasoning_effort"`
|
||||
APIKeyEnv string `json:"api_key_env"`
|
||||
ExtraParams map[string]any `json:"extra_params"`
|
||||
}
|
||||
|
||||
type invalidPromptKitConfigurationPointerError struct {
|
||||
cause error
|
||||
}
|
||||
|
||||
func (e *invalidPromptKitConfigurationPointerError) Error() string {
|
||||
return ErrInvalidPromptKitConfigurationPointer.Error()
|
||||
}
|
||||
|
||||
func (e *invalidPromptKitConfigurationPointerError) Unwrap() error {
|
||||
return e.cause
|
||||
}
|
||||
|
||||
func (e *invalidPromptKitConfigurationPointerError) Is(target error) bool {
|
||||
return target == ErrInvalidPromptKitConfigurationPointer
|
||||
}
|
||||
|
||||
func (i *PromptKitProfileInspector) EffectiveConfiguration() (string, error) {
|
||||
var output bytes.Buffer
|
||||
if err := i.WriteEffectiveConfiguration(&output); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return output.String(), nil
|
||||
}
|
||||
|
||||
func (i *PromptKitProfileInspector) WriteEffectiveConfiguration(output io.Writer) error {
|
||||
if i == nil || i.engine == nil {
|
||||
return fmt.Errorf("PromptKit inspector must not be nil")
|
||||
}
|
||||
inspection, err := i.engine.InspectConfiguration()
|
||||
if err != nil {
|
||||
return fmt.Errorf("inspect PromptKit configuration: %w", err)
|
||||
}
|
||||
return inspection.WriteEffective(output)
|
||||
}
|
||||
|
||||
func (i *PromptKitProfileInspector) ExplainConfiguration(pointer string) (string, bool, error) {
|
||||
var output bytes.Buffer
|
||||
found, err := i.WriteConfigurationExplanation(&output, pointer)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
return output.String(), found, nil
|
||||
}
|
||||
|
||||
func (i *PromptKitProfileInspector) WriteConfigurationExplanation(output io.Writer, pointer string) (bool, error) {
|
||||
if i == nil || i.engine == nil {
|
||||
return false, fmt.Errorf("PromptKit inspector must not be nil")
|
||||
}
|
||||
path, err := configkit.ParsePath(pointer)
|
||||
if err != nil || path.String() != pointer {
|
||||
if err == nil {
|
||||
err = fmt.Errorf("pointer spelling is not canonical")
|
||||
}
|
||||
return false, &invalidPromptKitConfigurationPointerError{cause: err}
|
||||
}
|
||||
inspection, err := i.engine.InspectConfiguration()
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("inspect PromptKit configuration: %w", err)
|
||||
}
|
||||
if _, found := inspection.Explain(path); !found {
|
||||
return false, nil
|
||||
}
|
||||
if err := inspection.WriteExplanation(output, path); err != nil {
|
||||
return true, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func promptKitEffectiveModelParams(target promptkit.ExecutionTarget) PromptKitEffectiveModelParams {
|
||||
return PromptKitEffectiveModelParams{
|
||||
BackendID: strings.TrimSpace(target.BackendID),
|
||||
Endpoint: strings.TrimSpace(target.Endpoint),
|
||||
Model: strings.TrimSpace(target.Model),
|
||||
Temperature: target.Temperature,
|
||||
MaxTokens: target.MaxTokens,
|
||||
TopP: target.TopP,
|
||||
TimeoutSeconds: target.TimeoutSeconds,
|
||||
ServiceTier: target.ServiceTier,
|
||||
ReasoningEffort: target.ReasoningEffort,
|
||||
APIKeyEnv: strings.TrimSpace(target.APIKeyEnv),
|
||||
ExtraParams: redactCredentialParameters(target.ExtraParams),
|
||||
}
|
||||
}
|
||||
|
||||
func redactCredentialParameters(values map[string]any) map[string]any {
|
||||
if values == nil {
|
||||
return nil
|
||||
}
|
||||
redacted, _ := redactCredentialParameterValue(values).(map[string]any)
|
||||
return redacted
|
||||
}
|
||||
|
||||
func redactCredentialParameterValue(value any) any {
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
copy := make(map[string]any, len(typed))
|
||||
for key, nested := range typed {
|
||||
if isCredentialParameterName(key) {
|
||||
copy[key] = "[REDACTED]"
|
||||
continue
|
||||
}
|
||||
copy[key] = redactCredentialParameterValue(nested)
|
||||
}
|
||||
return copy
|
||||
case []any:
|
||||
copy := make([]any, len(typed))
|
||||
for index, nested := range typed {
|
||||
copy[index] = redactCredentialParameterValue(nested)
|
||||
}
|
||||
return copy
|
||||
default:
|
||||
return typed
|
||||
}
|
||||
}
|
||||
|
||||
func isCredentialParameterName(name string) bool {
|
||||
switch strings.ToLower(name) {
|
||||
case "api_key", "apikey", "authorization", "credential", "credentials", "password", "secret", "token", "access_token":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -1,143 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
)
|
||||
|
||||
type promptKitInspectionErrorWriter struct{ err error }
|
||||
|
||||
func (w promptKitInspectionErrorWriter) Write([]byte) (int, error) { return 0, w.err }
|
||||
|
||||
func TestPromptKitProfileInspectionCopiesAndRedactsTarget(t *testing.T) {
|
||||
const (
|
||||
profileID = "inspection/profile~case"
|
||||
keyEnv = "NOT_SET_SYNTHETIC_PROFILE_KEY"
|
||||
)
|
||||
endpoint, initialModel, finalModel := "https://example.invalid/v1", "model-before", "model-after"
|
||||
params := map[string]any{
|
||||
"Authorization": "Bearer authorization-secret",
|
||||
"api_key": "api-key-secret",
|
||||
"ApiKey": "api-key-camel-secret",
|
||||
"apikey": "apikey-secret",
|
||||
"credential": "credential-secret",
|
||||
"credentials": "credentials-secret",
|
||||
"password": "password-secret",
|
||||
"secret": "secret-secret",
|
||||
"token": "token-secret",
|
||||
"access_token": "access-token-secret",
|
||||
"normal": "visible-value",
|
||||
"nested": map[string]any{
|
||||
"Access_Token": "nested-token-secret",
|
||||
"items": []any{map[string]any{"PASSWORD": "list-secret", "label": "ordinary"}},
|
||||
},
|
||||
}
|
||||
inspector, err := NewPromptKitProfileInspector(PromptKitProfileInspectorConfig{Sources: promptkit.ConfigurationSources{
|
||||
Underlays: []configkit.Underlay{
|
||||
{Inputs: []configkit.Input{{Source: promptkit.ProfileSource("lower profile", map[string]promptkit.ProfileConfig{
|
||||
profileID: {Endpoint: &endpoint, Model: &initialModel, APIKeyEnv: promptKitTestString(keyEnv), ExtraParams: params},
|
||||
})}}},
|
||||
{Inputs: []configkit.Input{{Source: promptkit.ProfileSource("profile overlay", map[string]promptkit.ProfileConfig{
|
||||
profileID: {Model: &finalModel},
|
||||
})}}},
|
||||
},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatalf("NewPromptKitProfileInspector() error = %v", err)
|
||||
}
|
||||
profile, err := inspector.InspectProfile(t.Context(), profileID)
|
||||
if err != nil {
|
||||
t.Fatalf("InspectProfile() error = %v; an unset synthetic credential name must not be resolved", err)
|
||||
}
|
||||
if profile.ProfileID != profileID || profile.BackendID != "" || profile.Model != finalModel || profile.CredentialEnvironment != keyEnv || profile.CredentialRequired {
|
||||
t.Fatalf("profile inspection = %#v", profile)
|
||||
}
|
||||
paramsCopy := profile.EffectiveModelParams.ExtraParams
|
||||
for _, key := range []string{"Authorization", "api_key", "ApiKey", "apikey", "credential", "credentials", "password", "secret", "token", "access_token"} {
|
||||
if paramsCopy[key] != "[REDACTED]" {
|
||||
t.Errorf("effective provider parameter %q = %#v, want redacted", key, paramsCopy[key])
|
||||
}
|
||||
}
|
||||
if paramsCopy["normal"] != "visible-value" {
|
||||
t.Fatalf("top-level effective provider parameters = %#v", paramsCopy)
|
||||
}
|
||||
nested := paramsCopy["nested"].(map[string]any)
|
||||
if nested["Access_Token"] != "[REDACTED]" {
|
||||
t.Fatalf("nested effective provider parameters = %#v", nested)
|
||||
}
|
||||
items := nested["items"].([]any)
|
||||
if items[0].(map[string]any)["PASSWORD"] != "[REDACTED]" || items[0].(map[string]any)["label"] != "ordinary" {
|
||||
t.Fatalf("list provider parameters = %#v", items)
|
||||
}
|
||||
encoded, err := json.Marshal(profile.EffectiveModelParams)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, secret := range []string{"authorization-secret", "api-key-secret", "api-key-camel-secret", "apikey-secret", "credential-secret", "credentials-secret", "password-secret", "secret-secret", "token-secret", "access-token-secret", "nested-token-secret", "list-secret"} {
|
||||
if strings.Contains(string(encoded), secret) {
|
||||
t.Fatalf("profile reporting projection contains %q", secret)
|
||||
}
|
||||
}
|
||||
nested["ordinary"] = "caller mutation"
|
||||
paramsCopy["normal"] = "caller mutation"
|
||||
second, err := inspector.InspectProfile(t.Context(), profileID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if second.EffectiveModelParams.ExtraParams["normal"] != "visible-value" || second.EffectiveModelParams.ExtraParams["nested"].(map[string]any)["ordinary"] != nil {
|
||||
t.Fatalf("profile inspection retained caller mutation: %#v", second.EffectiveModelParams.ExtraParams)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptKitConfigurationInspectionDistinguishesInvalidAndUnknownPointers(t *testing.T) {
|
||||
const profileID = "inspection/profile~case"
|
||||
endpoint, initialModel, finalModel := "https://example.invalid/v1", "model-before", "model-after"
|
||||
inspector, err := NewPromptKitProfileInspector(PromptKitProfileInspectorConfig{Sources: promptkit.ConfigurationSources{
|
||||
Underlays: []configkit.Underlay{
|
||||
{Inputs: []configkit.Input{{Source: promptkit.ProfileSource("lower profile", map[string]promptkit.ProfileConfig{
|
||||
profileID: {Endpoint: &endpoint, Model: &initialModel},
|
||||
})}}},
|
||||
{Inputs: []configkit.Input{{Source: promptkit.ProfileSource("profile overlay", map[string]promptkit.ProfileConfig{
|
||||
profileID: {Model: &finalModel},
|
||||
})}}},
|
||||
},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatalf("NewPromptKitProfileInspector() error = %v", err)
|
||||
}
|
||||
|
||||
report, err := inspector.EffectiveConfiguration()
|
||||
if err != nil {
|
||||
t.Fatalf("EffectiveConfiguration() error = %v", err)
|
||||
}
|
||||
if !strings.Contains(report, "model-after") || strings.Contains(report, "model-before") {
|
||||
t.Fatalf("effective report = %q, want only the effective model", report)
|
||||
}
|
||||
const escapedPointer = "/profiles/inspection~1profile~0case/model"
|
||||
explanation, found, err := inspector.ExplainConfiguration(escapedPointer)
|
||||
if err != nil || !found || !strings.Contains(explanation, "model-before") || !strings.Contains(explanation, "model-after") {
|
||||
t.Fatalf("ExplainConfiguration(%q) = (%q, %t, %v), want effective and historical values", escapedPointer, explanation, found, err)
|
||||
}
|
||||
if _, found, err := inspector.ExplainConfiguration("/profiles/not-configured"); err != nil || found {
|
||||
t.Fatalf("unknown configuration pointer = (found %t, error %v), want (false, nil)", found, err)
|
||||
}
|
||||
if _, _, err := inspector.ExplainConfiguration("/profiles/~2/model"); !errors.Is(err, ErrInvalidPromptKitConfigurationPointer) {
|
||||
t.Fatalf("invalid configuration pointer error = %v", err)
|
||||
}
|
||||
if _, found, err := inspector.ExplainConfiguration("/profiles/inspection~01profile~00case/model"); err != nil || found {
|
||||
t.Fatalf("configuration pointer with literal escape sequences = (found %t, error %v), want a valid unknown pointer", found, err)
|
||||
}
|
||||
|
||||
writerFailure := errors.New("writer failure")
|
||||
if err := inspector.WriteEffectiveConfiguration(promptKitInspectionErrorWriter{err: writerFailure}); !errors.Is(err, writerFailure) {
|
||||
t.Fatalf("WriteEffectiveConfiguration() error = %v, want writer cause", err)
|
||||
}
|
||||
if _, err := inspector.WriteConfigurationExplanation(promptKitInspectionErrorWriter{err: writerFailure}, escapedPointer); !errors.Is(err, writerFailure) {
|
||||
t.Fatalf("WriteConfigurationExplanation() error = %v, want writer cause", err)
|
||||
}
|
||||
}
|
||||
@@ -1,245 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
)
|
||||
|
||||
func newObservedHTTPClient(t *testing.T, handler http.HandlerFunc, retries int, transport http.RoundTripper) *PromptKitClient {
|
||||
t.Helper()
|
||||
server := httptest.NewServer(handler)
|
||||
t.Cleanup(server.Close)
|
||||
client, err := NewPromptKitClient(PromptKitClientConfig{
|
||||
Assets: newTestPromptKitAssets(t), TransientRetries: &retries,
|
||||
HTTPClient: &http.Client{Transport: transport},
|
||||
Sources: promptKitTestSources(nil, map[string]promptkit.ProfileConfig{"default-profile": {
|
||||
Endpoint: promptKitTestString(server.URL + "/v1"), Model: promptKitTestString("test-model"),
|
||||
}}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
func observedRequest() contracts.StructuredCompletionRequest {
|
||||
return contracts.StructuredCompletionRequest{
|
||||
PromptID: "adapter.test", SessionID: "observation-test",
|
||||
Inputs: contracts.LLMInputSet{"transcript": contracts.NewLLMInputMaterial("transcript", "application/json", []byte(`{}`), "", "")},
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptKitObservationsComposeTransientRetriesAndRepair(t *testing.T) {
|
||||
var calls atomic.Int32
|
||||
client := newObservedHTTPClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch calls.Add(1) {
|
||||
case 1, 3:
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
_, _ = io.WriteString(w, `{"error":{"message":"try again"}}`)
|
||||
case 2:
|
||||
_, _ = io.WriteString(w, `{"choices":[{"message":{"content":"{\"bad\":true}"}}],"usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5,"prompt_tokens_details":{"cached_tokens":0}}}`)
|
||||
case 4:
|
||||
_, _ = io.WriteString(w, `{"choices":[{"message":{"content":"{\"ok\":true}"}}],"usage":{"prompt_tokens":7,"completion_tokens":0,"total_tokens":7}}`)
|
||||
default:
|
||||
t.Error("unexpected extra HTTP attempt")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
}, 1, nil)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
req := observedRequest()
|
||||
repairs := 1
|
||||
req.StructuredOutputRepairAttempts = &repairs
|
||||
var out struct{ OK bool }
|
||||
response, err := client.CompleteStructured(ctx, req, &out)
|
||||
if err != nil || !out.OK {
|
||||
t.Fatalf("completion = (%+v, %v), output = %+v", response, err, out)
|
||||
}
|
||||
summary := response.Execution
|
||||
if calls.Load() != 4 || summary == nil || summary.Attempts != 4 || summary.TransientRetries != 2 || summary.RepairCalls != 1 || summary.Outcome != "success" || summary.RetriesExhausted {
|
||||
t.Fatalf("calls = %d, execution = %+v", calls.Load(), summary)
|
||||
}
|
||||
wantUsage := contracts.LLMObservedTokenUsage{
|
||||
PromptTokens: contracts.LLMObservedTokenCount{Value: 10, Known: true},
|
||||
CompletionTokens: contracts.LLMObservedTokenCount{Value: 2, Known: true},
|
||||
TotalTokens: contracts.LLMObservedTokenCount{Value: 12, Known: true},
|
||||
CachedTokens: contracts.LLMObservedTokenCount{Known: true},
|
||||
}
|
||||
// Failed HTTP attempts contribute unknown usage, not known zero. Neither
|
||||
// terminal totals nor legacy result totals should be added a second time.
|
||||
if summary.Usage != wantUsage {
|
||||
t.Fatalf("observed usage = %+v, want %+v", summary.Usage, wantUsage)
|
||||
}
|
||||
if response.PromptTokens != 10 || response.CompletionTokens != 2 || response.TotalTokens != 12 || response.RepairAttempts != 1 {
|
||||
t.Fatalf("legacy result accounting changed: %+v", response)
|
||||
}
|
||||
}
|
||||
|
||||
type closeSignalingBody struct {
|
||||
io.ReadCloser
|
||||
closed chan<- struct{}
|
||||
}
|
||||
|
||||
func (b closeSignalingBody) Close() error {
|
||||
err := b.ReadCloser.Close()
|
||||
b.closed <- struct{}{}
|
||||
return err
|
||||
}
|
||||
|
||||
func TestPromptKitObservationSurvivesCanceledRetryWait(t *testing.T) {
|
||||
consumed := make(chan struct{}, 2)
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
t.Cleanup(transport.CloseIdleConnections)
|
||||
var calls atomic.Int32
|
||||
client := newObservedHTTPClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
calls.Add(1)
|
||||
w.Header().Set("Retry-After", "3600")
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
_, _ = io.WriteString(w, `{"error":{"message":"busy"}}`)
|
||||
}, 1, roundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
response, err := transport.RoundTrip(req)
|
||||
if err == nil {
|
||||
response.Body = closeSignalingBody{ReadCloser: response.Body, closed: consumed}
|
||||
}
|
||||
return response, err
|
||||
}))
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
type result struct {
|
||||
response contracts.StructuredCompletionResponse
|
||||
err error
|
||||
}
|
||||
done := make(chan result, 1)
|
||||
go func() {
|
||||
var out map[string]any
|
||||
response, err := client.CompleteStructured(ctx, observedRequest(), &out)
|
||||
done <- result{response, err}
|
||||
}()
|
||||
// The failure has been consumed, so cancellation must interrupt the pending
|
||||
// retry wait rather than depend on another provider response or its timeout.
|
||||
select {
|
||||
case <-consumed:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("provider response was not consumed")
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case got := <-done:
|
||||
if !errors.Is(got.err, context.Canceled) || got.response.Execution == nil {
|
||||
t.Fatalf("canceled completion = (%+v, %v)", got.response, got.err)
|
||||
}
|
||||
summary := got.response.Execution
|
||||
if summary.Outcome != "error" || summary.FailureKind != "canceled" || summary.Attempts != 1 || summary.RetriesExhausted || calls.Load() != 1 {
|
||||
t.Fatalf("canceled execution = %+v, HTTP calls = %d", summary, calls.Load())
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("cancellation did not interrupt retry waiting")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptKitObservationsSeparatePreparationAndExecutionFailure(t *testing.T) {
|
||||
var calls atomic.Int32
|
||||
client := newObservedHTTPClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
calls.Add(1)
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
_, _ = io.WriteString(w, `{"error":{"message":"busy"}}`)
|
||||
}, 1, nil)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
req := observedRequest()
|
||||
req.PromptID = "missing-prompt"
|
||||
var out map[string]any
|
||||
response, err := client.CompleteStructured(ctx, req, &out)
|
||||
if err == nil || response.Execution != nil || calls.Load() != 0 {
|
||||
t.Fatalf("preparation failure = (%+v, %v), calls = %d", response, err, calls.Load())
|
||||
}
|
||||
response, err = client.CompleteStructured(ctx, observedRequest(), &out)
|
||||
var generation *contracts.LLMGenerationError
|
||||
if !errors.As(err, &generation) || response.Execution == nil {
|
||||
t.Fatalf("execution failure = (%+v, %v)", response, err)
|
||||
}
|
||||
summary := response.Execution
|
||||
if summary.Outcome != "error" || summary.FailureKind != "provider_status" || summary.StatusCode != http.StatusServiceUnavailable || !summary.RetriesExhausted || summary.Attempts != 2 || summary.TransientRetries != 1 || calls.Load() != 2 {
|
||||
t.Fatalf("failed execution = %+v, calls = %d", summary, calls.Load())
|
||||
}
|
||||
if summary.Usage != (contracts.LLMObservedTokenUsage{}) {
|
||||
t.Fatalf("failed attempts should have unknown usage: %+v", summary.Usage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptKitObservationsAreIsolatedAcrossConcurrentCalls(t *testing.T) {
|
||||
arrived := make(chan struct{}, 2)
|
||||
release := make(chan struct{})
|
||||
client := newObservedHTTPClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
tokens := 11
|
||||
if strings.Contains(string(body), "second-call") {
|
||||
tokens = 22
|
||||
}
|
||||
arrived <- struct{}{}
|
||||
select {
|
||||
case <-release:
|
||||
case <-r.Context().Done():
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"choices": []any{map[string]any{"message": map[string]string{"content": `{"ok":true}`}}},
|
||||
"usage": map[string]int{"prompt_tokens": tokens, "completion_tokens": 0, "total_tokens": tokens},
|
||||
})
|
||||
}, 0, nil)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
type result struct {
|
||||
want int
|
||||
response contracts.StructuredCompletionResponse
|
||||
err error
|
||||
}
|
||||
done := make(chan result, 2)
|
||||
for name, tokens := range map[string]int{"first-call": 11, "second-call": 22} {
|
||||
go func() {
|
||||
req := observedRequest()
|
||||
req.Vars = map[string]any{"custom": name}
|
||||
var out map[string]any
|
||||
response, err := client.CompleteStructured(ctx, req, &out)
|
||||
done <- result{tokens, response, err}
|
||||
}()
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
select {
|
||||
case <-arrived:
|
||||
case <-ctx.Done():
|
||||
t.Fatal("requests did not overlap")
|
||||
}
|
||||
}
|
||||
close(release)
|
||||
for i := 0; i < 2; i++ {
|
||||
select {
|
||||
case got := <-done:
|
||||
summary := got.response.Execution
|
||||
if got.err != nil || summary == nil {
|
||||
t.Fatalf("concurrent completion = (%+v, %v)", got.response, got.err)
|
||||
}
|
||||
if summary.Attempts != 1 || summary.Usage.PromptTokens != (contracts.LLMObservedTokenCount{Value: got.want, Known: true, Complete: true}) || summary.Usage.CompletionTokens != (contracts.LLMObservedTokenCount{Known: true, Complete: true}) || summary.Usage.CachedTokens != (contracts.LLMObservedTokenCount{}) {
|
||||
t.Fatalf("execution for %d tokens = %+v", got.want, summary)
|
||||
}
|
||||
case <-ctx.Done():
|
||||
t.Fatal("concurrent completions did not return")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,179 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
)
|
||||
|
||||
type PromptKitProfileSourceConfig struct {
|
||||
ProfileDir string
|
||||
ProfileFile string
|
||||
LocalBackend *PromptKitLocalBackendConfig
|
||||
}
|
||||
|
||||
type PromptKitProfileInspectorConfig struct {
|
||||
Source PromptKitProfileSourceConfig
|
||||
Assets *AssetRegistry
|
||||
Sources promptkit.ConfigurationSources
|
||||
}
|
||||
|
||||
func (c PromptKitProfileSourceConfig) localEndpoint() string {
|
||||
if c.LocalBackend == nil {
|
||||
return ""
|
||||
}
|
||||
return c.LocalBackend.Endpoint
|
||||
}
|
||||
|
||||
type PromptKitProfileInspector struct {
|
||||
engine *promptkit.Engine
|
||||
}
|
||||
|
||||
type PromptKitProfileInspection struct {
|
||||
ProfileID string
|
||||
BackendID string
|
||||
Model string
|
||||
CredentialEnvironment string
|
||||
CredentialRequired bool
|
||||
EffectiveModelParams PromptKitEffectiveModelParams `json:"effective_model_params"`
|
||||
}
|
||||
|
||||
type PromptKitProfileInspectionError struct {
|
||||
ProfileID string
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *PromptKitProfileInspectionError) Error() string {
|
||||
switch {
|
||||
case errors.Is(e.err, promptkit.ErrProfileNotFound):
|
||||
return fmt.Sprintf("PromptKit profile %q is not configured", e.ProfileID)
|
||||
case errors.Is(e.err, promptkit.ErrInvalidRequest):
|
||||
return fmt.Sprintf("PromptKit profile ID %q is invalid", e.ProfileID)
|
||||
case errors.Is(e.err, promptkit.ErrProfileLoad):
|
||||
return fmt.Sprintf("PromptKit profile %q is invalid or unreadable", e.ProfileID)
|
||||
default:
|
||||
return fmt.Sprintf("PromptKit profile %q could not be inspected", e.ProfileID)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *PromptKitProfileInspectionError) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
type promptKitProfileConfigurationError struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *promptKitProfileConfigurationError) Error() string {
|
||||
return "PromptKit profile configuration is invalid or unreadable"
|
||||
}
|
||||
|
||||
func (e *promptKitProfileConfigurationError) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
func NewPromptKitProfileInspector(cfg PromptKitProfileInspectorConfig) (*PromptKitProfileInspector, error) {
|
||||
_, sources, options, err := promptKitEngineConfiguration(cfg.Source, cfg.Assets, cfg.Sources)
|
||||
if err != nil {
|
||||
return nil, &promptKitProfileConfigurationError{err: err}
|
||||
}
|
||||
engine, err := promptkit.NewEngine(promptkit.Config{
|
||||
Sources: sources,
|
||||
}, options...)
|
||||
if err != nil {
|
||||
return nil, &promptKitProfileConfigurationError{err: err}
|
||||
}
|
||||
return &PromptKitProfileInspector{engine: engine}, nil
|
||||
}
|
||||
|
||||
func (i *PromptKitProfileInspector) InspectProfile(ctx context.Context, profileID string) (PromptKitProfileInspection, error) {
|
||||
if i == nil || i.engine == nil {
|
||||
return PromptKitProfileInspection{}, fmt.Errorf("PromptKit profile inspector must not be nil")
|
||||
}
|
||||
inspection, err := i.engine.InspectProfile(ctx, profileID)
|
||||
if err != nil {
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
return PromptKitProfileInspection{}, ctxErr
|
||||
}
|
||||
return PromptKitProfileInspection{}, &PromptKitProfileInspectionError{
|
||||
ProfileID: profileID,
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
return PromptKitProfileInspection{
|
||||
ProfileID: inspection.ProfileID,
|
||||
BackendID: strings.TrimSpace(inspection.EffectiveModelParams.BackendID),
|
||||
Model: strings.TrimSpace(inspection.EffectiveModelParams.Model),
|
||||
CredentialEnvironment: strings.TrimSpace(inspection.EffectiveModelParams.APIKeyEnv),
|
||||
CredentialRequired: inspection.APIKeyRequired,
|
||||
EffectiveModelParams: promptKitEffectiveModelParams(inspection.EffectiveModelParams),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func promptKitEngineConfiguration(cfg PromptKitProfileSourceConfig, assets *AssetRegistry, configuredSources promptkit.ConfigurationSources) (PromptKitProfileSourceConfig, promptkit.ConfigurationSources, []promptkit.Option, error) {
|
||||
profileSource, profileSources, err := promptKitProfileConfigurationSources(cfg)
|
||||
if err != nil {
|
||||
return PromptKitProfileSourceConfig{}, promptkit.ConfigurationSources{}, nil, err
|
||||
}
|
||||
sources := promptkit.ConfigurationSources{}
|
||||
var options []promptkit.Option
|
||||
if assets != nil {
|
||||
maintainedSources, maintainedOptions, err := assets.PromptKitConfigurationSources()
|
||||
if err != nil {
|
||||
return PromptKitProfileSourceConfig{}, promptkit.ConfigurationSources{}, nil, err
|
||||
}
|
||||
sources = maintainedSources
|
||||
options = append(options, maintainedOptions...)
|
||||
}
|
||||
sources.Underlays = append(sources.Underlays, configuredSources.Underlays...)
|
||||
sources.Underlays = append(sources.Underlays, profileSources.Underlays...)
|
||||
sources.Main = configuredSources.Main
|
||||
return profileSource, sources, options, nil
|
||||
}
|
||||
|
||||
func promptKitProfileConfigurationSources(cfg PromptKitProfileSourceConfig) (PromptKitProfileSourceConfig, promptkit.ConfigurationSources, error) {
|
||||
source := PromptKitProfileSourceConfig{
|
||||
ProfileDir: strings.TrimSpace(cfg.ProfileDir),
|
||||
ProfileFile: strings.TrimSpace(cfg.ProfileFile),
|
||||
}
|
||||
if source.ProfileDir != "" && source.ProfileFile != "" {
|
||||
return PromptKitProfileSourceConfig{}, promptkit.ConfigurationSources{}, fmt.Errorf("PromptKit profile_dir and profile_file are mutually exclusive")
|
||||
}
|
||||
if cfg.LocalBackend != nil {
|
||||
localBackend := *cfg.LocalBackend
|
||||
localBackend.Endpoint = strings.TrimSpace(localBackend.Endpoint)
|
||||
source.LocalBackend = &localBackend
|
||||
}
|
||||
|
||||
var underlays []configkit.Underlay
|
||||
if source.ProfileFile != "" {
|
||||
underlays = append(underlays, configkit.Underlay{Inputs: []configkit.Input{{
|
||||
Source: configkit.File(source.ProfileFile),
|
||||
Imports: true,
|
||||
}}})
|
||||
} else if source.ProfileDir != "" {
|
||||
inputs, err := promptkit.DirectoryInputs(source.ProfileDir)
|
||||
if err != nil {
|
||||
return PromptKitProfileSourceConfig{}, promptkit.ConfigurationSources{}, fmt.Errorf("select PromptKit profile directory: %w", err)
|
||||
}
|
||||
for index := range inputs {
|
||||
inputs[index].Imports = true
|
||||
}
|
||||
if len(inputs) != 0 {
|
||||
underlays = append(underlays, configkit.Underlay{Inputs: inputs})
|
||||
}
|
||||
}
|
||||
if source.LocalBackend != nil {
|
||||
backend := promptkit.LocalBackend(source.LocalBackend.Endpoint, source.LocalBackend.ConcurrencyLimit)
|
||||
underlays = append(underlays, configkit.Underlay{Inputs: []configkit.Input{{
|
||||
Source: promptkit.BackendSource("notarius-local-backend", map[string]promptkit.BackendConfig{
|
||||
promptkit.BackendLocal: backend,
|
||||
}),
|
||||
}}})
|
||||
}
|
||||
return source, promptkit.ConfigurationSources{Underlays: underlays}, nil
|
||||
}
|
||||
@@ -1,462 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
)
|
||||
|
||||
func TestPromptKitOperatorDocumentComposesForInspectionAndExecution(t *testing.T) {
|
||||
for _, name := range []string{
|
||||
"NOTARIUS_TEST_BACKEND_KEY",
|
||||
"NOTARIUS_TEST_PROFILE_KEY",
|
||||
"NOTARIUS_TEST_CATALOG_KEY",
|
||||
} {
|
||||
t.Setenv(name, "")
|
||||
}
|
||||
operatorDir := t.TempDir()
|
||||
profilePath := filepath.Join(operatorDir, "operator.yaml")
|
||||
if err := os.WriteFile(filepath.Join(operatorDir, "inherited.yaml"), []byte(`profiles:
|
||||
inherited-base:
|
||||
backend: custom-backend
|
||||
model: imported-model
|
||||
temperature: 0.7
|
||||
service_tier: flex
|
||||
extra_params:
|
||||
inherited: retained
|
||||
nested:
|
||||
inherited: true
|
||||
`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(profilePath, []byte(`configkit:
|
||||
imports:
|
||||
- path: inherited.yaml
|
||||
backends:
|
||||
custom-backend:
|
||||
endpoint: http://127.0.0.1:1/v1
|
||||
api_key_env: NOTARIUS_TEST_BACKEND_KEY
|
||||
concurrency_limit: 1
|
||||
queue_capacity: 0
|
||||
openrouter:
|
||||
api_key_env: NOTARIUS_TEST_CATALOG_KEY
|
||||
profiles:
|
||||
custom-profile:
|
||||
extends: inherited-base
|
||||
model: operator-model
|
||||
temperature: 0
|
||||
api_key_required: false
|
||||
service_tier: null
|
||||
extra_params: null
|
||||
empty-map-profile:
|
||||
extends: inherited-base
|
||||
extra_params: {}
|
||||
api_key_required: false
|
||||
profile-key-profile:
|
||||
backend: custom-backend
|
||||
model: profile-key-model
|
||||
api_key_env: NOTARIUS_TEST_PROFILE_KEY
|
||||
api_key_required: false
|
||||
required-profile:
|
||||
extends: inherited-base
|
||||
api_key_env: NOTARIUS_TEST_PROFILE_KEY
|
||||
api_key_required: true
|
||||
catalog-profile:
|
||||
backend: openrouter
|
||||
model: openai/gpt-5.6-luna
|
||||
api_key_required: false
|
||||
prompts:
|
||||
adapter.test:
|
||||
v1:
|
||||
output:
|
||||
repair_attempts: 1
|
||||
`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assets := newTestPromptKitAssets(t)
|
||||
fake := &fakePromptKitLLM{}
|
||||
client, err := NewPromptKitClient(PromptKitClientConfig{
|
||||
Assets: assets,
|
||||
ProfileFile: profilePath,
|
||||
EngineOptions: []promptkit.Option{
|
||||
promptkit.WithLLMClient(fake),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewPromptKitClient() error = %v", err)
|
||||
}
|
||||
inspector, err := NewPromptKitProfileInspector(PromptKitProfileInspectorConfig{
|
||||
Source: PromptKitProfileSourceConfig{ProfileFile: profilePath},
|
||||
Assets: assets,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewPromptKitProfileInspector() error = %v", err)
|
||||
}
|
||||
|
||||
for _, test := range []struct {
|
||||
id string
|
||||
backend string
|
||||
model string
|
||||
keyEnv string
|
||||
required bool
|
||||
}{
|
||||
{id: "custom-profile", backend: "custom-backend", model: "operator-model", keyEnv: "NOTARIUS_TEST_BACKEND_KEY"},
|
||||
{id: "inherited-base", backend: "custom-backend", model: "imported-model", keyEnv: "NOTARIUS_TEST_BACKEND_KEY"},
|
||||
{id: "empty-map-profile", backend: "custom-backend", model: "imported-model", keyEnv: "NOTARIUS_TEST_BACKEND_KEY"},
|
||||
{id: "profile-key-profile", backend: "custom-backend", model: "profile-key-model", keyEnv: "NOTARIUS_TEST_PROFILE_KEY"},
|
||||
{id: "required-profile", backend: "custom-backend", model: "imported-model", keyEnv: "NOTARIUS_TEST_PROFILE_KEY", required: true},
|
||||
{id: "catalog-profile", backend: promptkit.BackendOpenRouter, model: "openai/gpt-5.6-luna", keyEnv: "NOTARIUS_TEST_CATALOG_KEY"},
|
||||
} {
|
||||
inspection, err := inspector.InspectProfile(context.Background(), test.id)
|
||||
if err != nil {
|
||||
t.Fatalf("InspectProfile(%q) error = %v", test.id, err)
|
||||
}
|
||||
if inspection.ProfileID != test.id || inspection.BackendID != test.backend || inspection.Model != test.model ||
|
||||
inspection.CredentialEnvironment != test.keyEnv || inspection.CredentialRequired != test.required {
|
||||
t.Fatalf("InspectProfile(%q) = %#v", test.id, inspection)
|
||||
}
|
||||
}
|
||||
|
||||
var out map[string]any
|
||||
response, err := client.CompleteStructured(context.Background(), contracts.StructuredCompletionRequest{
|
||||
PromptID: "adapter.test",
|
||||
ProfileID: "custom-profile",
|
||||
SessionID: "source-composition-test",
|
||||
Inputs: contracts.LLMInputSet{
|
||||
"transcript": contracts.NewLLMInputMaterial("transcript", "application/json", []byte(`{"source":true}`), "", ""),
|
||||
},
|
||||
}, &out)
|
||||
if err != nil {
|
||||
t.Fatalf("CompleteStructured() with an unset optional credential error = %v", err)
|
||||
}
|
||||
if response.Debug == nil || response.Debug.Prompt == nil {
|
||||
t.Fatalf("completion debug = %#v, want captured prompt", response.Debug)
|
||||
}
|
||||
debug := response.Debug.Prompt
|
||||
if debug.SelectedBackendID != "custom-backend" || debug.SelectedProfileID != "custom-profile" {
|
||||
t.Fatalf("runtime selection = backend %q, profile %q", debug.SelectedBackendID, debug.SelectedProfileID)
|
||||
}
|
||||
if got := debug.EffectiveModelParams["model"]; got != "operator-model" {
|
||||
t.Fatalf("runtime model = %#v, want operator-model", got)
|
||||
}
|
||||
if got, ok := debug.EffectiveModelParams["temperature"].(float64); !ok || got != 0 {
|
||||
t.Fatalf("runtime temperature = %#v, want explicit zero", debug.EffectiveModelParams["temperature"])
|
||||
}
|
||||
if got := debug.EffectiveModelParams["service_tier"]; got == "flex" {
|
||||
t.Fatalf("explicit null retained inherited service tier: %#v", got)
|
||||
}
|
||||
if got, ok := debug.EffectiveModelParams["extra_params"]; ok && got != nil {
|
||||
t.Fatalf("explicit null retained inherited provider map: %#v", got)
|
||||
}
|
||||
if got := debug.OutputContract["repair_attempts"]; got != float64(1) {
|
||||
t.Fatalf("overlaid output repair attempts = %#v, want 1", got)
|
||||
}
|
||||
if len(debug.Messages) == 0 || !strings.Contains(debug.Messages[0].Content, "Transcript:") {
|
||||
t.Fatalf("rendered prompt messages = %#v", debug.Messages)
|
||||
}
|
||||
if got := atomic.LoadInt32(&fake.calls); got != 1 {
|
||||
t.Fatalf("provider calls after optional-credential execution = %d, want 1", got)
|
||||
}
|
||||
|
||||
_, err = client.CompleteStructured(context.Background(), contracts.StructuredCompletionRequest{
|
||||
PromptID: "adapter.test",
|
||||
ProfileID: "required-profile",
|
||||
SessionID: "source-composition-test",
|
||||
Inputs: contracts.LLMInputSet{
|
||||
"transcript": contracts.NewLLMInputMaterial("transcript", "application/json", []byte(`{"source":true}`), "", ""),
|
||||
},
|
||||
}, &out)
|
||||
if err == nil || !strings.Contains(err.Error(), "api_key_env points to an unset environment variable") {
|
||||
t.Fatalf("CompleteStructured() with required unset credential error = %v, want native missing-environment failure", err)
|
||||
}
|
||||
if got := atomic.LoadInt32(&fake.calls); got != 1 {
|
||||
t.Fatalf("provider calls after required-credential failure = %d, want 1", got)
|
||||
}
|
||||
_, err = client.CompleteStructured(context.Background(), contracts.StructuredCompletionRequest{
|
||||
PromptID: "adapter.test",
|
||||
ProfileID: "empty-map-profile",
|
||||
SessionID: "source-composition-test",
|
||||
Inputs: contracts.LLMInputSet{
|
||||
"transcript": contracts.NewLLMInputMaterial("transcript", "application/json", []byte(`{"source":true}`), "", ""),
|
||||
},
|
||||
}, &out)
|
||||
if err != nil {
|
||||
t.Fatalf("CompleteStructured() with empty overlay map error = %v", err)
|
||||
}
|
||||
if got := fake.lastRequest().Target.ExtraParams["inherited"]; got != "retained" {
|
||||
t.Fatalf("empty provider-map overlay result = %#v, want inherited entry", fake.lastRequest().Target.ExtraParams)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptKitPromptContentUsesDeclaringAndReplacementSourceContexts(t *testing.T) {
|
||||
rootDirectory := t.TempDir()
|
||||
declaringDirectory := filepath.Join(rootDirectory, "child")
|
||||
replacementDirectory := t.TempDir()
|
||||
writeFile := func(path, content string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
rootConfig := filepath.Join(rootDirectory, "operator.yaml")
|
||||
declaringConfig := filepath.Join(declaringDirectory, "imported.yaml")
|
||||
replacementConfig := filepath.Join(replacementDirectory, "replacement.yaml")
|
||||
writeFile(rootConfig, `configkit:
|
||||
imports:
|
||||
- path: child/imported.yaml
|
||||
`)
|
||||
writeFile(filepath.Join(rootDirectory, "messages", "shared.tmpl"), "root decoy")
|
||||
writeFile(declaringConfig, `prompts:
|
||||
imported.capture:
|
||||
v1:
|
||||
messages:
|
||||
- role: user
|
||||
content_file: messages/shared.tmpl
|
||||
output:
|
||||
format: text
|
||||
validation_mode: none
|
||||
`)
|
||||
writeFile(filepath.Join(declaringDirectory, "messages", "shared.tmpl"), "declared import")
|
||||
writeFile(replacementConfig, `prompts:
|
||||
adapter.test:
|
||||
v1:
|
||||
messages:
|
||||
- role: user
|
||||
content_file: messages/shared.tmpl
|
||||
`)
|
||||
writeFile(filepath.Join(replacementDirectory, "messages", "shared.tmpl"), "replacement original")
|
||||
|
||||
endpoint, model := "https://example.invalid/v1", "content-context-model"
|
||||
newClient := func() *PromptKitClient {
|
||||
t.Helper()
|
||||
client, err := NewPromptKitClient(PromptKitClientConfig{
|
||||
Assets: newTestPromptKitAssets(t),
|
||||
Sources: promptkit.ConfigurationSources{Underlays: []configkit.Underlay{
|
||||
{Inputs: []configkit.Input{{Source: promptkit.ProfileSource("content context profile", map[string]promptkit.ProfileConfig{
|
||||
"content-context-profile": {Endpoint: &endpoint, Model: &model},
|
||||
})}}},
|
||||
{Inputs: []configkit.Input{{Source: configkit.File(rootConfig), Imports: true}}},
|
||||
{Inputs: []configkit.Input{{Source: configkit.File(replacementConfig), Imports: true}}},
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewPromptKitClient() error = %v", err)
|
||||
}
|
||||
return client
|
||||
}
|
||||
prepareMessage := func(client *PromptKitClient, promptID string) string {
|
||||
t.Helper()
|
||||
request := promptkit.RunRequest{PromptID: promptID, ProfileID: "content-context-profile", SessionID: "content-context-session"}
|
||||
if promptID == "adapter.test" {
|
||||
request.Inputs = map[string]promptkit.ArtifactRef{
|
||||
"transcript": promptkit.Inline(`{"source":true}`),
|
||||
}
|
||||
}
|
||||
prepared, err := client.engine.Prepare(context.Background(), request)
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare(%q) error = %v", promptID, err)
|
||||
}
|
||||
if len(prepared.Messages) != 1 {
|
||||
t.Fatalf("Prepare(%q) messages = %#v, want one", promptID, prepared.Messages)
|
||||
}
|
||||
return prepared.Messages[0].Content
|
||||
}
|
||||
|
||||
first := newClient()
|
||||
if got := prepareMessage(first, "imported.capture"); got != "declared import" {
|
||||
t.Fatalf("imported prompt content = %q, want declaring-source content", got)
|
||||
}
|
||||
if got := prepareMessage(first, "adapter.test"); got != "replacement original" {
|
||||
t.Fatalf("partially overlaid application prompt content = %q, want replacement-source content", got)
|
||||
}
|
||||
writeFile(filepath.Join(declaringDirectory, "messages", "shared.tmpl"), "declared import updated")
|
||||
writeFile(filepath.Join(replacementDirectory, "messages", "shared.tmpl"), "replacement updated")
|
||||
if got := prepareMessage(first, "imported.capture"); got != "declared import" {
|
||||
t.Fatalf("existing engine imported prompt content after edit = %q", got)
|
||||
}
|
||||
if got := prepareMessage(first, "adapter.test"); got != "replacement original" {
|
||||
t.Fatalf("existing engine replacement content after edit = %q", got)
|
||||
}
|
||||
second := newClient()
|
||||
if got := prepareMessage(second, "imported.capture"); got != "declared import updated" {
|
||||
t.Fatalf("new engine imported prompt content = %q", got)
|
||||
}
|
||||
if got := prepareMessage(second, "adapter.test"); got != "replacement updated" {
|
||||
t.Fatalf("new engine replacement content = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptKitStaticMessageFailuresOccurAtConstruction(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
prompt string
|
||||
files fstest.MapFS
|
||||
}{
|
||||
{
|
||||
name: "missing message file",
|
||||
prompt: `prompts:
|
||||
construction.failure:
|
||||
v1:
|
||||
messages:
|
||||
- role: user
|
||||
content_file: messages/missing.tmpl
|
||||
output:
|
||||
format: text
|
||||
validation_mode: none
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "invalid static template",
|
||||
prompt: `prompts:
|
||||
construction.failure:
|
||||
v1:
|
||||
messages:
|
||||
- role: user
|
||||
content: '{{ .request_value'
|
||||
output:
|
||||
format: text
|
||||
validation_mode: none
|
||||
`,
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
assets := NewAssetRegistry()
|
||||
files := test.files
|
||||
if files == nil {
|
||||
files = fstest.MapFS{}
|
||||
}
|
||||
files["prompt.yaml"] = &fstest.MapFile{Data: []byte(test.prompt)}
|
||||
if err := assets.RegisterPromptFS(files, "."); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := NewPromptKitClient(PromptKitClientConfig{
|
||||
Assets: assets,
|
||||
Sources: promptKitTestSources(nil, map[string]promptkit.ProfileConfig{"construction-profile": {
|
||||
Endpoint: promptKitTestString("https://example.invalid/v1"),
|
||||
Model: promptKitTestString("construction-model"),
|
||||
}}),
|
||||
})
|
||||
if err == nil || !errors.Is(err, promptkit.ErrInvalidConfig) {
|
||||
t.Fatalf("NewPromptKitClient() error = %v, want static construction failure", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptKitMissingRequestVariableFailsDuringPreparation(t *testing.T) {
|
||||
assets := NewAssetRegistry()
|
||||
if err := assets.RegisterPromptFS(fstest.MapFS{
|
||||
"prompt.yaml": {Data: []byte(`prompts:
|
||||
request.variable:
|
||||
v1:
|
||||
messages:
|
||||
- role: user
|
||||
content: 'Value: {{ .request_value }}'
|
||||
output:
|
||||
format: text
|
||||
validation_mode: none
|
||||
`)},
|
||||
}, "."); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
client, err := NewPromptKitClient(PromptKitClientConfig{
|
||||
Assets: assets,
|
||||
Sources: promptKitTestSources(nil, map[string]promptkit.ProfileConfig{"request-profile": {
|
||||
Endpoint: promptKitTestString("https://example.invalid/v1"),
|
||||
Model: promptKitTestString("request-model"),
|
||||
}}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewPromptKitClient() error = %v, want valid static template", err)
|
||||
}
|
||||
if _, err := client.engine.Prepare(context.Background(), promptkit.RunRequest{
|
||||
PromptID: "request.variable", ProfileID: "request-profile",
|
||||
}); !errors.Is(err, promptkit.ErrPromptRender) {
|
||||
t.Fatalf("Prepare() error = %v, want missing request variable render failure", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptKitProfileSourceGroupsPreserveNativeLayerRules(t *testing.T) {
|
||||
t.Run("directory group conflicts", func(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
for name, model := range map[string]string{"first.yaml": "first-model", "second.yaml": "second-model"} {
|
||||
content := "profiles:\n duplicate:\n endpoint: http://127.0.0.1:1/v1\n model: " + model + "\n"
|
||||
if err := os.WriteFile(filepath.Join(directory, name), []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
_, err := NewPromptKitProfileInspector(PromptKitProfileInspectorConfig{
|
||||
Source: PromptKitProfileSourceConfig{ProfileDir: directory},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("NewPromptKitProfileInspector() error = nil, want same-group conflict")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("later group repairs an invalid lower field and overlays supplied values", func(t *testing.T) {
|
||||
badEndpoint := "not an endpoint"
|
||||
goodEndpoint := "http://127.0.0.1:1/v1"
|
||||
lowerModel := "lower-model"
|
||||
sources := promptkit.ConfigurationSources{Underlays: []configkit.Underlay{
|
||||
{Inputs: []configkit.Input{{Source: promptkit.ProfileSource("lower source", map[string]promptkit.ProfileConfig{
|
||||
"layered-profile": {Endpoint: &badEndpoint, Model: &lowerModel},
|
||||
})}}},
|
||||
{Inputs: []configkit.Input{{Source: promptkit.ProfileSource("upper source", map[string]promptkit.ProfileConfig{
|
||||
"layered-profile": {Endpoint: &goodEndpoint},
|
||||
})}}},
|
||||
}}
|
||||
inspector, err := NewPromptKitProfileInspector(PromptKitProfileInspectorConfig{Sources: sources})
|
||||
if err != nil {
|
||||
t.Fatalf("NewPromptKitProfileInspector() error = %v, want repaired final layer", err)
|
||||
}
|
||||
inspection, err := inspector.InspectProfile(context.Background(), "layered-profile")
|
||||
if err != nil || inspection.Model != lowerModel {
|
||||
t.Fatalf("InspectProfile() = (%#v, %v), want lower model retained", inspection, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty and missing selected directories", func(t *testing.T) {
|
||||
if _, err := NewPromptKitProfileInspector(PromptKitProfileInspectorConfig{
|
||||
Source: PromptKitProfileSourceConfig{ProfileDir: filepath.Join(t.TempDir(), "missing")},
|
||||
}); err == nil {
|
||||
t.Fatal("missing selected directory was accepted")
|
||||
}
|
||||
empty := t.TempDir()
|
||||
inspector, err := NewPromptKitProfileInspector(PromptKitProfileInspectorConfig{
|
||||
Source: PromptKitProfileSourceConfig{ProfileDir: empty},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("empty selected directory error = %v, want no contribution", err)
|
||||
}
|
||||
if _, err := inspector.InspectProfile(context.Background(), "not-present"); err == nil {
|
||||
t.Fatal("InspectProfile() in empty directory error = nil, want unknown profile")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unused invalid final profile fails construction", func(t *testing.T) {
|
||||
badEndpoint := "not an endpoint"
|
||||
model := "unused-model"
|
||||
sources := promptkit.ConfigurationSources{Underlays: []configkit.Underlay{{Inputs: []configkit.Input{{
|
||||
Source: promptkit.ProfileSource("unused invalid profile", map[string]promptkit.ProfileConfig{
|
||||
"unused-profile": {Endpoint: &badEndpoint, Model: &model},
|
||||
}),
|
||||
}}}}}
|
||||
_, err := NewPromptKitProfileInspector(PromptKitProfileInspectorConfig{Sources: sources})
|
||||
if err == nil || !errors.Is(err, promptkit.ErrInvalidConfig) {
|
||||
t.Fatalf("NewPromptKitProfileInspector() error = %v, want invalid final configuration", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
)
|
||||
|
||||
func promptKitTestString(value string) *string { return &value }
|
||||
|
||||
func promptKitTestInt(value int) *int { return &value }
|
||||
|
||||
func promptKitTestFloat(value float64) *float64 { return &value }
|
||||
|
||||
func promptKitTestInputs(t *testing.T, label string, files fstest.MapFS, root string) []configkit.Input {
|
||||
t.Helper()
|
||||
inputs, err := promptkit.FilesystemInputs(label, files, root)
|
||||
if err != nil {
|
||||
t.Fatalf("FilesystemInputs() error = %v", err)
|
||||
}
|
||||
return inputs
|
||||
}
|
||||
|
||||
func promptKitTestSources(
|
||||
backends map[string]promptkit.BackendConfig,
|
||||
profiles map[string]promptkit.ProfileConfig,
|
||||
otherInputs ...configkit.Input,
|
||||
) promptkit.ConfigurationSources {
|
||||
inputs := make([]configkit.Input, 0, len(otherInputs)+2)
|
||||
if len(backends) != 0 {
|
||||
inputs = append(inputs, configkit.Input{Source: promptkit.BackendSource("test backends", backends)})
|
||||
}
|
||||
if len(profiles) != 0 {
|
||||
inputs = append(inputs, configkit.Input{Source: promptkit.ProfileSource("test profiles", profiles)})
|
||||
}
|
||||
inputs = append(inputs, otherInputs...)
|
||||
if len(inputs) == 0 {
|
||||
return promptkit.ConfigurationSources{}
|
||||
}
|
||||
return promptkit.ConfigurationSources{Underlays: []configkit.Underlay{{Inputs: inputs}}}
|
||||
}
|
||||
@@ -1,171 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
rootassets "gitea.maximumdirect.net/eric/notarius/assets"
|
||||
)
|
||||
|
||||
var schemaAssets = mustSubFS(rootassets.FS(), "generic")
|
||||
|
||||
// ResponseSchemaKey identifies one structured response schema.
|
||||
type ResponseSchemaKey string
|
||||
|
||||
const (
|
||||
TestArtifactSchemaKey ResponseSchemaKey = "test_artifact"
|
||||
TestValidatorDecisionSchemaKey ResponseSchemaKey = "test_validator_decision"
|
||||
|
||||
schemaVersionV1 = "v1"
|
||||
)
|
||||
|
||||
// ResponseSchemaDefinition identifies a caller-owned structured response schema asset.
|
||||
type ResponseSchemaDefinition struct {
|
||||
Key ResponseSchemaKey
|
||||
ID string
|
||||
Version string
|
||||
Name string
|
||||
AssetPath string
|
||||
}
|
||||
|
||||
// ResponseSchema describes one registered structured response schema.
|
||||
type ResponseSchema struct {
|
||||
Key ResponseSchemaKey `json:"key"`
|
||||
ID string `json:"id"`
|
||||
Version string `json:"version"`
|
||||
Name string `json:"name"`
|
||||
JSONSchema json.RawMessage `json:"json_schema"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
var responseSchemaRegistry = map[ResponseSchemaKey]ResponseSchema{
|
||||
TestArtifactSchemaKey: mustLoadResponseSchema(schemaAssets, ResponseSchemaDefinition{
|
||||
Key: TestArtifactSchemaKey,
|
||||
ID: "notarius.test_artifact",
|
||||
Version: schemaVersionV1,
|
||||
Name: "notarius_test_artifact_v1",
|
||||
AssetPath: "schemas/test_artifact.v1.json",
|
||||
}),
|
||||
TestValidatorDecisionSchemaKey: mustLoadResponseSchema(schemaAssets, ResponseSchemaDefinition{
|
||||
Key: TestValidatorDecisionSchemaKey,
|
||||
ID: "notarius.test_validator_decision",
|
||||
Version: schemaVersionV1,
|
||||
Name: "notarius_test_validator_decision_v1",
|
||||
AssetPath: "schemas/test_validator_decision.v1.json",
|
||||
}),
|
||||
}
|
||||
|
||||
func mustSubFS(fsys fs.FS, dir string) fs.FS {
|
||||
sub, err := fs.Sub(fsys, dir)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("scope embedded assets to %s: %w", dir, err))
|
||||
}
|
||||
return sub
|
||||
}
|
||||
|
||||
// RegisteredResponseSchemas returns all registered response schemas sorted by key.
|
||||
func RegisteredResponseSchemas() []ResponseSchema {
|
||||
keys := make([]string, 0, len(responseSchemaRegistry))
|
||||
for key := range responseSchemaRegistry {
|
||||
keys = append(keys, string(key))
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
out := make([]ResponseSchema, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
out = append(out, cloneResponseSchema(responseSchemaRegistry[ResponseSchemaKey(key)]))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// LookupResponseSchema returns a copy of the schema for key.
|
||||
func LookupResponseSchema(key ResponseSchemaKey) (ResponseSchema, bool) {
|
||||
schema, ok := responseSchemaRegistry[key]
|
||||
if !ok {
|
||||
return ResponseSchema{}, false
|
||||
}
|
||||
return cloneResponseSchema(schema), true
|
||||
}
|
||||
|
||||
// MustLookupResponseSchema returns a copy of the schema for key and panics when missing.
|
||||
func MustLookupResponseSchema(key ResponseSchemaKey) ResponseSchema {
|
||||
schema, ok := LookupResponseSchema(key)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("unknown structured response schema key %q", key))
|
||||
}
|
||||
return schema
|
||||
}
|
||||
|
||||
// DiagnosticsMap returns schema metadata without raw schema content.
|
||||
func (s ResponseSchema) DiagnosticsMap() map[string]any {
|
||||
return map[string]any{
|
||||
"key": s.Key,
|
||||
"id": s.ID,
|
||||
"version": s.Version,
|
||||
"name": s.Name,
|
||||
"sha256": s.SHA256,
|
||||
}
|
||||
}
|
||||
|
||||
// LoadResponseSchema loads a structured response schema from a caller-owned filesystem.
|
||||
func LoadResponseSchema(fsys fs.FS, def ResponseSchemaDefinition) (ResponseSchema, error) {
|
||||
key := ResponseSchemaKey(strings.TrimSpace(string(def.Key)))
|
||||
id := strings.TrimSpace(def.ID)
|
||||
version := strings.TrimSpace(def.Version)
|
||||
name := strings.TrimSpace(def.Name)
|
||||
path := strings.TrimSpace(def.AssetPath)
|
||||
if key == "" {
|
||||
return ResponseSchema{}, fmt.Errorf("response schema key must not be empty")
|
||||
}
|
||||
if id == "" {
|
||||
return ResponseSchema{}, fmt.Errorf("response schema id must not be empty")
|
||||
}
|
||||
if version == "" {
|
||||
return ResponseSchema{}, fmt.Errorf("response schema version must not be empty")
|
||||
}
|
||||
if name == "" {
|
||||
return ResponseSchema{}, fmt.Errorf("response schema name must not be empty")
|
||||
}
|
||||
if path == "" {
|
||||
return ResponseSchema{}, fmt.Errorf("response schema asset path must not be empty")
|
||||
}
|
||||
|
||||
rawSchema, err := fs.ReadFile(fsys, path)
|
||||
if err != nil {
|
||||
return ResponseSchema{}, fmt.Errorf("read response schema %s: %w", path, err)
|
||||
}
|
||||
if !json.Valid(rawSchema) {
|
||||
return ResponseSchema{}, fmt.Errorf("response schema %s is not valid JSON", path)
|
||||
}
|
||||
|
||||
hash := sha256.Sum256(rawSchema)
|
||||
return ResponseSchema{
|
||||
Key: key,
|
||||
ID: id,
|
||||
Version: version,
|
||||
Name: name,
|
||||
JSONSchema: append(json.RawMessage(nil), rawSchema...),
|
||||
SHA256: "sha256:" + hex.EncodeToString(hash[:]),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func mustLoadResponseSchema(fsys fs.FS, def ResponseSchemaDefinition) ResponseSchema {
|
||||
schema, err := LoadResponseSchema(fsys, def)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return schema
|
||||
}
|
||||
|
||||
func cloneResponseSchema(in ResponseSchema) ResponseSchema {
|
||||
out := in
|
||||
if in.JSONSchema != nil {
|
||||
out.JSONSchema = append(json.RawMessage(nil), in.JSONSchema...)
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLookupResponseSchemaSucceedsForRegisteredSchemas(t *testing.T) {
|
||||
tests := []ResponseSchemaKey{
|
||||
TestArtifactSchemaKey,
|
||||
TestValidatorDecisionSchemaKey,
|
||||
}
|
||||
|
||||
for _, key := range tests {
|
||||
t.Run(string(key), func(t *testing.T) {
|
||||
schema, ok := LookupResponseSchema(key)
|
||||
if !ok {
|
||||
t.Fatalf("expected schema for key %q", key)
|
||||
}
|
||||
if schema.Key != key {
|
||||
t.Fatalf("unexpected key: got %q want %q", schema.Key, key)
|
||||
}
|
||||
if schema.ID == "" || schema.Version == "" || schema.Name == "" {
|
||||
t.Fatalf("expected schema metadata, got %+v", schema)
|
||||
}
|
||||
if !strings.HasPrefix(schema.SHA256, "sha256:") {
|
||||
t.Fatalf("expected prefixed hash, got %q", schema.SHA256)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLookupResponseSchemaUnknownReturnsFalse(t *testing.T) {
|
||||
if schema, ok := LookupResponseSchema("unknown"); ok {
|
||||
t.Fatalf("expected unknown schema lookup to fail, got %+v", schema)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLookupResponseSchemaDNDSpellsLLMIsNotFrameworkRegistered(t *testing.T) {
|
||||
if schema, ok := LookupResponseSchema("dnd_spells_llm"); ok {
|
||||
t.Fatalf("expected D&D spells schema lookup to fail in framework registry, got %+v", schema)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMustLookupResponseSchemaPanicsForUnknownKey(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatalf("expected panic")
|
||||
}
|
||||
}()
|
||||
|
||||
_ = MustLookupResponseSchema("unknown")
|
||||
}
|
||||
|
||||
func TestRegisteredResponseSchemasSortedByKey(t *testing.T) {
|
||||
schemas := RegisteredResponseSchemas()
|
||||
keys := make([]string, len(schemas))
|
||||
seen := make(map[ResponseSchemaKey]bool, len(schemas))
|
||||
for i, schema := range schemas {
|
||||
keys[i] = string(schema.Key)
|
||||
seen[schema.Key] = true
|
||||
}
|
||||
if !sort.StringsAreSorted(keys) {
|
||||
t.Fatalf("expected sorted keys, got %v", keys)
|
||||
}
|
||||
if !seen[TestArtifactSchemaKey] || !seen[TestValidatorDecisionSchemaKey] {
|
||||
t.Fatalf("registered schemas = %v, want test schemas", keys)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponseSchemaContentIsValidJSON(t *testing.T) {
|
||||
for _, schema := range RegisteredResponseSchemas() {
|
||||
if !json.Valid(schema.JSONSchema) {
|
||||
t.Fatalf("schema %q has invalid JSON: %s", schema.Key, schema.JSONSchema)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponseSchemaJSONIsMutationSafe(t *testing.T) {
|
||||
for _, key := range []ResponseSchemaKey{TestArtifactSchemaKey, TestValidatorDecisionSchemaKey} {
|
||||
t.Run(string(key), func(t *testing.T) {
|
||||
first := MustLookupResponseSchema(key)
|
||||
first.JSONSchema[0] = '['
|
||||
|
||||
second := MustLookupResponseSchema(key)
|
||||
if !json.Valid(second.JSONSchema) {
|
||||
t.Fatalf("schema JSON was mutated: %s", second.JSONSchema)
|
||||
}
|
||||
if len(second.JSONSchema) > 0 && second.JSONSchema[0] == '[' {
|
||||
t.Fatalf("schema JSON did not use defensive copy")
|
||||
}
|
||||
|
||||
registered := RegisteredResponseSchemas()
|
||||
for i := range registered {
|
||||
if registered[i].Key == key {
|
||||
registered[i].JSONSchema[0] = '['
|
||||
}
|
||||
}
|
||||
again := MustLookupResponseSchema(key)
|
||||
if !json.Valid(again.JSONSchema) || again.JSONSchema[0] == '[' {
|
||||
t.Fatalf("registered schema JSON did not use defensive copy")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponseSchemaDiagnosticsMapOmitsRawSchemaContent(t *testing.T) {
|
||||
schema := MustLookupResponseSchema(TestValidatorDecisionSchemaKey)
|
||||
diagnostics := schema.DiagnosticsMap()
|
||||
|
||||
for _, key := range []string{"id", "version", "name", "sha256"} {
|
||||
if diagnostics[key] == "" {
|
||||
t.Fatalf("expected diagnostics key %q, got %#v", key, diagnostics)
|
||||
}
|
||||
}
|
||||
if _, ok := diagnostics["json_schema"]; ok {
|
||||
t.Fatalf("diagnostics should omit raw schema content: %#v", diagnostics)
|
||||
}
|
||||
if _, ok := diagnostics["JSONSchema"]; ok {
|
||||
t.Fatalf("diagnostics should omit raw schema content: %#v", diagnostics)
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const secretReplacement = "[REDACTED]"
|
||||
|
||||
// RedactSecrets replaces configured secret values in diagnostics.
|
||||
func RedactSecrets(message string, secrets []string) string {
|
||||
if message == "" || len(secrets) == 0 {
|
||||
return message
|
||||
}
|
||||
|
||||
normalized := normalizeSecrets(secrets)
|
||||
for _, secret := range normalized {
|
||||
message = strings.ReplaceAll(message, secret, secretReplacement)
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
// ErrorWithSecretsRedacted returns an error with known secret values removed
|
||||
// from its message.
|
||||
func ErrorWithSecretsRedacted(err error, secrets []string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return errors.New(RedactSecrets(err.Error(), secrets))
|
||||
}
|
||||
|
||||
func normalizeSecrets(secrets []string) []string {
|
||||
seen := make(map[string]struct{}, len(secrets))
|
||||
result := make([]string, 0, len(secrets))
|
||||
for _, secret := range secrets {
|
||||
secret = strings.TrimSpace(secret)
|
||||
if secret == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[secret]; ok {
|
||||
continue
|
||||
}
|
||||
seen[secret] = struct{}{}
|
||||
result = append(result, secret)
|
||||
}
|
||||
|
||||
sort.Slice(result, func(i, j int) bool {
|
||||
return len(result[i]) > len(result[j])
|
||||
})
|
||||
return result
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRedactSecrets(t *testing.T) {
|
||||
got := RedactSecrets("api key secret-token and Bearer secret-token failed", []string{"", "secret-token", "secret-token"})
|
||||
|
||||
if strings.Contains(got, "secret-token") {
|
||||
t.Fatalf("expected secret to be redacted, got %q", got)
|
||||
}
|
||||
if strings.Count(got, secretReplacement) != 2 {
|
||||
t.Fatalf("expected two redactions, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedactSecretsPrefersLongerSecrets(t *testing.T) {
|
||||
got := RedactSecrets("token token-extra", []string{"token", "token-extra"})
|
||||
|
||||
if strings.Contains(got, "token") {
|
||||
t.Fatalf("expected overlapping secrets to be redacted, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorWithSecretsRedacted(t *testing.T) {
|
||||
err := ErrorWithSecretsRedacted(errors.New("secret-value failed"), []string{"secret-value"})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("expected redacted error")
|
||||
}
|
||||
if strings.Contains(err.Error(), "secret-value") {
|
||||
t.Fatalf("expected secret to be redacted, got %q", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorWithSecretsRedactedNil(t *testing.T) {
|
||||
if err := ErrorWithSecretsRedacted(nil, []string{"secret"}); err != nil {
|
||||
t.Fatalf("expected nil error, got %v", err)
|
||||
}
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
package promptfs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/readonlyfs"
|
||||
)
|
||||
|
||||
// ModulePromptFile maps a module-owned embedded prompt file into the registered
|
||||
// module prompt directory.
|
||||
type ModulePromptFile struct {
|
||||
Name string
|
||||
Path string
|
||||
}
|
||||
|
||||
// SharedPromptFile maps a caller-owned shared prompt file into a module's
|
||||
// registered sharedassets prompt subdirectory.
|
||||
type SharedPromptFile struct {
|
||||
Name string
|
||||
FS fs.FS
|
||||
Path string
|
||||
}
|
||||
|
||||
// ModulePromptFS builds a prompt filesystem for a module directory from
|
||||
// module-owned prompt files plus caller-provided shared prompt files under the
|
||||
// module's sharedassets subdirectory.
|
||||
func ModulePromptFS(moduleDir string, moduleFS fs.FS, files []ModulePromptFile, sharedFiles ...SharedPromptFile) (fs.FS, error) {
|
||||
cleanModuleDir, err := cleanPromptPath(moduleDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("module prompt directory: %w", err)
|
||||
}
|
||||
if moduleFS == nil {
|
||||
return nil, fmt.Errorf("module prompt filesystem must not be nil")
|
||||
}
|
||||
assets := make(map[string][]byte, len(files)+len(sharedFiles))
|
||||
moduleDestinations := make(map[string]struct{}, len(files))
|
||||
for _, file := range files {
|
||||
name, err := cleanPromptPath(file.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("module prompt file name %q: %w", file.Name, err)
|
||||
}
|
||||
if strings.Contains(name, "/") {
|
||||
return nil, fmt.Errorf("module prompt file name %q must not contain path separators", file.Name)
|
||||
}
|
||||
destination := "assets/prompts/" + cleanModuleDir + "/" + name
|
||||
if _, exists := moduleDestinations[destination]; exists {
|
||||
return nil, fmt.Errorf("duplicate module prompt destination %q", destination)
|
||||
}
|
||||
moduleDestinations[destination] = struct{}{}
|
||||
filePath, err := cleanPromptPath(file.Path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("module prompt file path %q: %w", file.Path, err)
|
||||
}
|
||||
data, err := fs.ReadFile(moduleFS, filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read module prompt asset %s: %w", filePath, err)
|
||||
}
|
||||
assets[destination] = data
|
||||
}
|
||||
|
||||
sharedDestinations := make(map[string]struct{}, len(sharedFiles))
|
||||
for _, file := range sharedFiles {
|
||||
name, err := cleanPromptPath(file.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("shared prompt file name %q: %w", file.Name, err)
|
||||
}
|
||||
if strings.Contains(name, "/") {
|
||||
return nil, fmt.Errorf("shared prompt file name %q must not contain path separators", file.Name)
|
||||
}
|
||||
destination := "assets/prompts/" + cleanModuleDir + "/sharedassets/" + name
|
||||
if _, exists := sharedDestinations[destination]; exists {
|
||||
return nil, fmt.Errorf("duplicate sharedassets prompt destination %q", destination)
|
||||
}
|
||||
sharedDestinations[destination] = struct{}{}
|
||||
if file.FS == nil {
|
||||
return nil, fmt.Errorf("shared prompt file %q filesystem must not be nil", file.Name)
|
||||
}
|
||||
filePath, err := cleanPromptPath(file.Path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("shared prompt file path %q: %w", file.Path, err)
|
||||
}
|
||||
data, err := fs.ReadFile(file.FS, filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read shared prompt asset %s: %w", filePath, err)
|
||||
}
|
||||
assets[destination] = data
|
||||
}
|
||||
fsys, err := readonlyfs.New(assets)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("construct prompt filesystem: %w", err)
|
||||
}
|
||||
return fsys, nil
|
||||
}
|
||||
|
||||
func cleanPromptPath(name string) (string, error) {
|
||||
trimmed := strings.TrimSpace(name)
|
||||
if trimmed == "" {
|
||||
return "", fmt.Errorf("path must not be empty")
|
||||
}
|
||||
cleaned := path.Clean(strings.TrimPrefix(trimmed, "/"))
|
||||
if cleaned == "." || !fs.ValidPath(cleaned) {
|
||||
return "", fmt.Errorf("invalid path %q", name)
|
||||
}
|
||||
return cleaned, nil
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
package promptfs
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func TestModulePromptFSCombinesModuleAndSharedPrompts(t *testing.T) {
|
||||
sharedFS := fstest.MapFS{
|
||||
"shared/system.md": {Data: []byte("system")},
|
||||
"shared/reference.md": {Data: []byte("reference")},
|
||||
"nested/transcript.md": {Data: []byte("transcript")},
|
||||
}
|
||||
fsys, err := ModulePromptFS("module.test", fstest.MapFS{
|
||||
"assets/prompts/module.test.yaml": {Data: []byte("id: module.test")},
|
||||
"assets/prompts/task.md": {Data: []byte("task")},
|
||||
}, []ModulePromptFile{
|
||||
{Name: "module.test.yaml", Path: "assets/prompts/module.test.yaml"},
|
||||
{Name: "task.md", Path: "assets/prompts/task.md"},
|
||||
},
|
||||
SharedPromptFile{Name: "system.md", FS: sharedFS, Path: "shared/system.md"},
|
||||
SharedPromptFile{Name: "reference.md", FS: sharedFS, Path: "shared/reference.md"},
|
||||
SharedPromptFile{Name: "transcript.md", FS: sharedFS, Path: "nested/transcript.md"},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("ModulePromptFS() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
tests := map[string]string{
|
||||
"assets/prompts/module.test/module.test.yaml": "id: module.test",
|
||||
"assets/prompts/module.test/task.md": "task",
|
||||
"assets/prompts/module.test/sharedassets/system.md": "system",
|
||||
"assets/prompts/module.test/sharedassets/reference.md": "reference",
|
||||
"assets/prompts/module.test/sharedassets/transcript.md": "transcript",
|
||||
}
|
||||
for path, want := range tests {
|
||||
data, err := fs.ReadFile(fsys, path)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile(%q) error = %v, want nil", path, err)
|
||||
}
|
||||
if string(data) != want {
|
||||
t.Fatalf("ReadFile(%q) = %q, want %q", path, data, want)
|
||||
}
|
||||
}
|
||||
|
||||
entries, err := fs.ReadDir(fsys, ".")
|
||||
if err != nil {
|
||||
t.Fatalf("ReadDir(.) error = %v, want nil", err)
|
||||
}
|
||||
if len(entries) != 1 || entries[0].Name() != "assets" || !entries[0].IsDir() {
|
||||
t.Fatalf("ReadDir(.) = %#v, want assets directory", entries)
|
||||
}
|
||||
entries, err = fs.ReadDir(fsys, "assets/prompts/module.test/sharedassets")
|
||||
if err != nil {
|
||||
t.Fatalf("ReadDir(sharedassets) error = %v, want nil", err)
|
||||
}
|
||||
if len(entries) != 3 || entries[0].Name() != "reference.md" || entries[1].Name() != "system.md" || entries[2].Name() != "transcript.md" {
|
||||
t.Fatalf("ReadDir(sharedassets) = %#v, want sorted shared prompt entries", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModulePromptFSRejectsMissingModuleFile(t *testing.T) {
|
||||
_, err := ModulePromptFS("module.test", fstest.MapFS{}, []ModulePromptFile{
|
||||
{Name: "task.md", Path: "assets/prompts/task.md"},
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "read module prompt asset assets/prompts/task.md") {
|
||||
t.Fatalf("ModulePromptFS() error = %v, want missing module asset context", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModulePromptFSRejectsMissingSharedFile(t *testing.T) {
|
||||
_, err := ModulePromptFS("module.test", fstest.MapFS{
|
||||
"assets/prompts/task.md": {Data: []byte("task")},
|
||||
}, []ModulePromptFile{
|
||||
{Name: "task.md", Path: "assets/prompts/task.md"},
|
||||
}, SharedPromptFile{Name: "system.md", FS: fstest.MapFS{}, Path: "shared/system.md"})
|
||||
if err == nil || !strings.Contains(err.Error(), "read shared prompt asset shared/system.md") {
|
||||
t.Fatalf("ModulePromptFS() error = %v, want missing shared asset context", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModulePromptFSRejectsNestedVirtualFileName(t *testing.T) {
|
||||
_, err := ModulePromptFS("module.test", fstest.MapFS{
|
||||
"assets/prompts/task.md": {Data: []byte("task")},
|
||||
}, []ModulePromptFile{
|
||||
{Name: "nested/task.md", Path: "assets/prompts/task.md"},
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "must not contain path separators") {
|
||||
t.Fatalf("ModulePromptFS() error = %v, want nested file name error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModulePromptFSRejectsNestedSharedFileName(t *testing.T) {
|
||||
_, err := ModulePromptFS("module.test", fstest.MapFS{
|
||||
"assets/prompts/task.md": {Data: []byte("task")},
|
||||
}, []ModulePromptFile{
|
||||
{Name: "task.md", Path: "assets/prompts/task.md"},
|
||||
}, SharedPromptFile{Name: "nested/system.md", FS: fstest.MapFS{}, Path: "shared/system.md"})
|
||||
if err == nil || !strings.Contains(err.Error(), "shared prompt file name") || !strings.Contains(err.Error(), "must not contain path separators") {
|
||||
t.Fatalf("ModulePromptFS() error = %v, want nested shared file name error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModulePromptFSRejectsDuplicateNormalizedDestinations(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
call func() error
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "module",
|
||||
call: func() error {
|
||||
_, err := ModulePromptFS("module.test", fstest.MapFS{
|
||||
"assets/prompts/first.md": {Data: []byte("first")},
|
||||
"assets/prompts/second.md": {Data: []byte("second")},
|
||||
}, []ModulePromptFile{
|
||||
{Name: "task.md", Path: "assets/prompts/first.md"},
|
||||
{Name: " task.md ", Path: "assets/prompts/second.md"},
|
||||
})
|
||||
return err
|
||||
},
|
||||
want: `duplicate module prompt destination "assets/prompts/module.test/task.md"`,
|
||||
},
|
||||
{
|
||||
name: "sharedassets",
|
||||
call: func() error {
|
||||
shared := fstest.MapFS{
|
||||
"shared/first.md": {Data: []byte("first")},
|
||||
"shared/second.md": {Data: []byte("second")},
|
||||
}
|
||||
_, err := ModulePromptFS("module.test", fstest.MapFS{}, nil,
|
||||
SharedPromptFile{Name: "task.md", FS: shared, Path: "shared/first.md"},
|
||||
SharedPromptFile{Name: " task.md ", FS: shared, Path: "shared/second.md"},
|
||||
)
|
||||
return err
|
||||
},
|
||||
want: `duplicate sharedassets prompt destination "assets/prompts/module.test/sharedassets/task.md"`,
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if err := test.call(); err == nil || err.Error() != test.want {
|
||||
t.Fatalf("ModulePromptFS() error = %v, want %q", err, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestModulePromptFSAllowsMatchingModuleAndSharedBasenames(t *testing.T) {
|
||||
fsys, err := ModulePromptFS("module.test", fstest.MapFS{
|
||||
"assets/prompts/system.md": {Data: []byte("module")},
|
||||
}, []ModulePromptFile{{Name: "system.md", Path: "assets/prompts/system.md"}},
|
||||
SharedPromptFile{Name: "system.md", FS: fstest.MapFS{"shared/system.md": {Data: []byte("shared")}}, Path: "shared/system.md"},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, want := range map[string]string{
|
||||
"assets/prompts/module.test/system.md": "module",
|
||||
"assets/prompts/module.test/sharedassets/system.md": "shared",
|
||||
} {
|
||||
data, err := fs.ReadFile(fsys, name)
|
||||
if err != nil || string(data) != want {
|
||||
t.Fatalf("ReadFile(%q) = %q, %v; want %q", name, data, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestModulePromptFSRejectsInvalidModuleDir(t *testing.T) {
|
||||
_, err := ModulePromptFS(".", fstest.MapFS{}, nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "module prompt directory") {
|
||||
t.Fatalf("ModulePromptFS() error = %v, want module directory context", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModulePromptFSRejectsNilFilesystems(t *testing.T) {
|
||||
_, err := ModulePromptFS("module.test", nil, nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "module prompt filesystem must not be nil") {
|
||||
t.Fatalf("ModulePromptFS() error = %v, want nil module filesystem error", err)
|
||||
}
|
||||
|
||||
_, err = ModulePromptFS("module.test", fstest.MapFS{}, nil, SharedPromptFile{Name: "system.md", Path: "shared/system.md"})
|
||||
if err == nil || !strings.Contains(err.Error(), "shared prompt file \"system.md\" filesystem must not be nil") {
|
||||
t.Fatalf("ModulePromptFS() error = %v, want nil shared filesystem error", err)
|
||||
}
|
||||
}
|
||||
@@ -1,193 +0,0 @@
|
||||
// Package readonlyfs provides an immutable filesystem backed by owned bytes.
|
||||
package readonlyfs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"path"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
// FS is an immutable filesystem constructed from a map of file contents.
|
||||
type FS struct {
|
||||
files map[string][]byte
|
||||
dirs map[string][]fs.DirEntry
|
||||
}
|
||||
|
||||
var _ fs.FS = (*FS)(nil)
|
||||
var _ fs.ReadFileFS = (*FS)(nil)
|
||||
var _ fs.ReadDirFS = (*FS)(nil)
|
||||
|
||||
// New constructs an immutable filesystem. File names must be valid io/fs
|
||||
// paths and the root path may not be used as a file.
|
||||
func New(files map[string][]byte) (*FS, error) {
|
||||
owned := make(map[string][]byte, len(files))
|
||||
directories := map[string]struct{}{".": {}}
|
||||
for name, data := range files {
|
||||
if name == "." || !fs.ValidPath(name) {
|
||||
return nil, fmt.Errorf("invalid file path %q", name)
|
||||
}
|
||||
owned[name] = append([]byte(nil), data...)
|
||||
for directory := path.Dir(name); directory != "."; directory = path.Dir(directory) {
|
||||
directories[directory] = struct{}{}
|
||||
}
|
||||
}
|
||||
for directory := range directories {
|
||||
if _, exists := owned[directory]; exists {
|
||||
return nil, fmt.Errorf("file path %q conflicts with an inferred directory", directory)
|
||||
}
|
||||
}
|
||||
|
||||
dirs := make(map[string][]fs.DirEntry, len(directories))
|
||||
for directory := range directories {
|
||||
dirs[directory] = make([]fs.DirEntry, 0)
|
||||
}
|
||||
for directory := range directories {
|
||||
if directory == "." {
|
||||
continue
|
||||
}
|
||||
parent := path.Dir(directory)
|
||||
dirs[parent] = append(dirs[parent], dirEntry{name: path.Base(directory), dir: true})
|
||||
}
|
||||
for name, data := range owned {
|
||||
parent := path.Dir(name)
|
||||
dirs[parent] = append(dirs[parent], dirEntry{name: path.Base(name), size: int64(len(data))})
|
||||
}
|
||||
for directory := range dirs {
|
||||
sort.Slice(dirs[directory], func(left, right int) bool {
|
||||
return dirs[directory][left].Name() < dirs[directory][right].Name()
|
||||
})
|
||||
}
|
||||
return &FS{files: owned, dirs: dirs}, nil
|
||||
}
|
||||
|
||||
func (f *FS) Open(name string) (fs.File, error) {
|
||||
if err := validOpenPath(name); err != nil {
|
||||
return nil, pathError("open", name, err)
|
||||
}
|
||||
if data, ok := f.files[name]; ok {
|
||||
return &file{reader: bytes.NewReader(data), info: fileInfo{name: path.Base(name), size: int64(len(data))}}, nil
|
||||
}
|
||||
if entries, ok := f.dirs[name]; ok {
|
||||
return &dir{name: path.Base(name), entries: entries}, nil
|
||||
}
|
||||
return nil, pathError("open", name, fs.ErrNotExist)
|
||||
}
|
||||
|
||||
func (f *FS) ReadFile(name string) ([]byte, error) {
|
||||
if err := validOpenPath(name); err != nil {
|
||||
return nil, pathError("readfile", name, err)
|
||||
}
|
||||
data, ok := f.files[name]
|
||||
if !ok {
|
||||
if _, isDirectory := f.dirs[name]; isDirectory {
|
||||
return nil, pathError("readfile", name, errIsDirectory)
|
||||
}
|
||||
return nil, pathError("readfile", name, fs.ErrNotExist)
|
||||
}
|
||||
return append([]byte(nil), data...), nil
|
||||
}
|
||||
|
||||
func (f *FS) ReadDir(name string) ([]fs.DirEntry, error) {
|
||||
if err := validOpenPath(name); err != nil {
|
||||
return nil, pathError("readdir", name, err)
|
||||
}
|
||||
entries, ok := f.dirs[name]
|
||||
if !ok {
|
||||
if _, isFile := f.files[name]; isFile {
|
||||
return nil, pathError("readdir", name, errNotDirectory)
|
||||
}
|
||||
return nil, pathError("readdir", name, fs.ErrNotExist)
|
||||
}
|
||||
return append([]fs.DirEntry(nil), entries...), nil
|
||||
}
|
||||
|
||||
func validOpenPath(name string) error {
|
||||
if name == "." || fs.ValidPath(name) {
|
||||
return nil
|
||||
}
|
||||
return fs.ErrInvalid
|
||||
}
|
||||
|
||||
func pathError(operation, name string, err error) error {
|
||||
return &fs.PathError{Op: operation, Path: name, Err: err}
|
||||
}
|
||||
|
||||
var (
|
||||
errIsDirectory = errors.New("is a directory")
|
||||
errNotDirectory = errors.New("not a directory")
|
||||
)
|
||||
|
||||
type file struct {
|
||||
reader *bytes.Reader
|
||||
info fileInfo
|
||||
}
|
||||
|
||||
func (f *file) Stat() (fs.FileInfo, error) { return f.info, nil }
|
||||
func (f *file) Read(data []byte) (int, error) {
|
||||
return f.reader.Read(data)
|
||||
}
|
||||
func (f *file) Close() error { return nil }
|
||||
|
||||
type dir struct {
|
||||
name string
|
||||
offset int
|
||||
entries []fs.DirEntry
|
||||
}
|
||||
|
||||
func (d *dir) Stat() (fs.FileInfo, error) { return fileInfo{name: d.name, dir: true}, nil }
|
||||
func (d *dir) Read([]byte) (int, error) { return 0, errIsDirectory }
|
||||
func (d *dir) Close() error { return nil }
|
||||
func (d *dir) ReadDir(count int) ([]fs.DirEntry, error) {
|
||||
if count <= 0 {
|
||||
entries := append([]fs.DirEntry(nil), d.entries[d.offset:]...)
|
||||
d.offset = len(d.entries)
|
||||
return entries, nil
|
||||
}
|
||||
if d.offset >= len(d.entries) {
|
||||
return nil, io.EOF
|
||||
}
|
||||
end := len(d.entries)
|
||||
if d.offset+count < end {
|
||||
end = d.offset + count
|
||||
}
|
||||
entries := append([]fs.DirEntry(nil), d.entries[d.offset:end]...)
|
||||
d.offset = end
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
type dirEntry struct {
|
||||
name string
|
||||
dir bool
|
||||
size int64
|
||||
}
|
||||
|
||||
func (e dirEntry) Name() string { return e.name }
|
||||
func (e dirEntry) IsDir() bool { return e.dir }
|
||||
func (e dirEntry) Type() fs.FileMode { return e.mode().Type() }
|
||||
func (e dirEntry) Info() (fs.FileInfo, error) {
|
||||
return fileInfo{name: e.name, dir: e.dir, size: e.size}, nil
|
||||
}
|
||||
func (e dirEntry) mode() fs.FileMode {
|
||||
if e.dir {
|
||||
return fs.ModeDir | 0o555
|
||||
}
|
||||
return 0o444
|
||||
}
|
||||
|
||||
type fileInfo struct {
|
||||
name string
|
||||
dir bool
|
||||
size int64
|
||||
}
|
||||
|
||||
func (i fileInfo) Name() string { return i.name }
|
||||
func (i fileInfo) Size() int64 { return i.size }
|
||||
func (i fileInfo) Mode() fs.FileMode { return dirEntry{dir: i.dir}.mode() }
|
||||
func (i fileInfo) ModTime() time.Time { return time.Time{} }
|
||||
func (i fileInfo) IsDir() bool { return i.dir }
|
||||
func (i fileInfo) Sys() any { return nil }
|
||||
@@ -1,153 +0,0 @@
|
||||
package readonlyfs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func TestNewPassesFSTest(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
files map[string][]byte
|
||||
paths []string
|
||||
}{
|
||||
{name: "empty", files: map[string][]byte{}, paths: nil},
|
||||
{name: "single file", files: map[string][]byte{"message.txt": []byte("hello")}, paths: []string{"message.txt"}},
|
||||
{name: "nested tree", files: map[string][]byte{"a/one.txt": []byte("one"), "a/b/two.txt": []byte("two")}, paths: []string{"a/one.txt", "a/b/two.txt"}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
fsys, err := New(test.files)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := fstest.TestFS(fsys, test.paths...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewOwnsConstructionAndReadBytes(t *testing.T) {
|
||||
input := []byte("value")
|
||||
fsys, err := New(map[string][]byte{"value.txt": input})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
input[0] = 'x'
|
||||
first, err := fsys.ReadFile("value.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first[0] = 'y'
|
||||
second, err := fsys.ReadFile("value.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(second) != "value" {
|
||||
t.Fatalf("ReadFile() = %q, want owned bytes", second)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesystemStandardBehavior(t *testing.T) {
|
||||
fsys, err := New(map[string][]byte{
|
||||
"z.txt": []byte("z"),
|
||||
"nested/a.md": []byte("abc"),
|
||||
"nested/b.md": []byte("b"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, err := fsys.ReadDir("nested")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 2 || entries[0].Name() != "a.md" || entries[1].Name() != "b.md" {
|
||||
t.Fatalf("ReadDir(nested) = %#v, want sorted entries", entries)
|
||||
}
|
||||
info, err := entries[0].Info()
|
||||
if err != nil || info.Mode().Perm() != 0o444 || info.Size() != 3 {
|
||||
t.Fatalf("file info = %#v, %v", info, err)
|
||||
}
|
||||
root, err := fsys.Open(".")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rootInfo, err := root.Stat()
|
||||
if err != nil || !rootInfo.IsDir() || rootInfo.Mode() != fs.ModeDir|0o555 {
|
||||
t.Fatalf("root info = %#v, %v", rootInfo, err)
|
||||
}
|
||||
directory := root.(fs.ReadDirFile)
|
||||
first, err := directory.ReadDir(1)
|
||||
if err != nil || len(first) != 1 || first[0].Name() != "nested" {
|
||||
t.Fatalf("first directory page = %#v, %v", first, err)
|
||||
}
|
||||
second, err := directory.ReadDir(1)
|
||||
if err != nil || len(second) != 1 || second[0].Name() != "z.txt" {
|
||||
t.Fatalf("second directory page = %#v, %v", second, err)
|
||||
}
|
||||
if _, err := directory.ReadDir(1); !errors.Is(err, io.EOF) {
|
||||
t.Fatalf("final directory page error = %v, want EOF", err)
|
||||
}
|
||||
secondRoot, err := fsys.Open(".")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secondDirectory := secondRoot.(fs.ReadDirFile)
|
||||
firstAgain, err := secondDirectory.ReadDir(1)
|
||||
if err != nil || len(firstAgain) != 1 || firstAgain[0].Name() != "nested" {
|
||||
t.Fatalf("independent directory handle page = %#v, %v", firstAgain, err)
|
||||
}
|
||||
|
||||
left, err := fsys.Open("nested/a.md")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
right, err := fsys.Open("nested/a.md")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
leftByte := make([]byte, 1)
|
||||
rightByte := make([]byte, 1)
|
||||
if _, err := left.Read(leftByte); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := right.Read(rightByte); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(leftByte) != "a" || string(rightByte) != "a" {
|
||||
t.Fatalf("independent file handles read %q and %q", leftByte, rightByte)
|
||||
}
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
op func() error
|
||||
want error
|
||||
}{
|
||||
{name: "invalid open path", op: func() error { _, err := fsys.Open("../value"); return err }, want: fs.ErrInvalid},
|
||||
{name: "missing file", op: func() error { _, err := fsys.Open("missing"); return err }, want: fs.ErrNotExist},
|
||||
{name: "read directory", op: func() error { _, err := fsys.ReadFile("nested"); return err }, want: errIsDirectory},
|
||||
{name: "read directory from file", op: func() error { _, err := fsys.ReadDir("z.txt"); return err }, want: errNotDirectory},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if err := test.op(); !errors.Is(err, test.want) {
|
||||
t.Fatalf("operation error = %v, want %v", err, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRejectsInvalidAndConflictingPaths(t *testing.T) {
|
||||
for _, files := range []map[string][]byte{
|
||||
{".": []byte("root")},
|
||||
{"": []byte("empty")},
|
||||
{"../outside": []byte("outside")},
|
||||
{"nested//file": []byte("invalid")},
|
||||
{"file": []byte("file"), "file/nested": []byte("nested")},
|
||||
} {
|
||||
if _, err := New(files); err == nil {
|
||||
t.Fatalf("New(%#v) error = nil, want rejection", files)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -373,8 +373,8 @@ func TestPlanFromResponseRejectsMalformedSceneRanges(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("Plan() error = nil, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "dnd scenes") || !strings.Contains(err.Error(), tt.want) {
|
||||
t.Fatalf("Plan() error = %q, want module context and %q", err.Error(), tt.want)
|
||||
if !strings.Contains(err.Error(), tt.want) {
|
||||
t.Fatalf("planFromResponse() error = %q, want %q", err.Error(), tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -17,7 +17,20 @@ import (
|
||||
|
||||
const missingActor = `{"combat_turns":[{"actor":"","turn_kind":"reaction","source_refs":[{"start_unit_id":10,"end_unit_id":10}]}]}`
|
||||
const invalidCitation = `{"combat_turns":[{"actor":"Aria","turn_kind":"reaction","source_refs":[{"start_unit_id":999,"end_unit_id":999}]}]}`
|
||||
const knownTurn = `{"combat_turns":[{"actor":"Aria","turn_kind":"reaction","source_refs":[{"start_unit_id":2,"end_unit_id":10}]}]}`
|
||||
const knownTurn = `{"combat_turns":[{"actor":"Aria","turn_kind":"reaction","source_refs":[{"start_unit_id":10,"end_unit_id":2}]}]}`
|
||||
|
||||
func nativeCombatRequest(t *testing.T) contracts.TypedExtractionRequest[dnd.CombatTurnList] {
|
||||
t.Helper()
|
||||
request := extractionRequest(t)
|
||||
// These end-to-end native cases must also satisfy durable numeric range
|
||||
// constraints. The interpretation-only fixtures separately cover arbitrary
|
||||
// document ordering; no durable codec rule is relaxed for them.
|
||||
request.Source.Units[0], request.Source.Units[1] = request.Source.Units[1], request.Source.Units[0]
|
||||
request.Chunk.Units = append(request.Chunk.Units[:0], request.Source.Units...)
|
||||
request.Chunk.Content = []byte(`{"units":[2,10]}`)
|
||||
request.SourceInput = contracts.NewLLMInputMaterial("source", request.Chunk.MediaType, request.Chunk.Content, "sha256:chunk", "file:///session-alpha.json")
|
||||
return request
|
||||
}
|
||||
|
||||
func bindCombatAssessment(request *contracts.TypedExtractionRequest[dnd.CombatTurnList]) *[]string {
|
||||
validators := []contracts.TypedValidator[dnd.CombatTurnList]{
|
||||
@@ -45,7 +58,7 @@ func bindCombatAssessment(request *contracts.TypedExtractionRequest[dnd.CombatTu
|
||||
|
||||
func TestCombatNativeCorrectionKeepsEligibilityAndSourceContextFixed(t *testing.T) {
|
||||
for _, first := range []string{missingActor, invalidCitation} {
|
||||
request := extractionRequest(t)
|
||||
request := nativeCombatRequest(t)
|
||||
request.References.Slots[NPCRegistryReferenceSlot] = contracts.ResolvedReferenceSlot{Items: []contracts.ReferenceItem{
|
||||
{SlotName: NPCRegistryReferenceSlot, MediaType: "application/json", Content: npcRegistryJSON(t)},
|
||||
}}
|
||||
@@ -70,7 +83,7 @@ func TestCombatNativeCorrectionKeepsEligibilityAndSourceContextFixed(t *testing.
|
||||
t.Fatalf("combat correction=%#v %v, validators=%#v native=%d/%d", result, err, *calls, len(client.requests), len(prepared))
|
||||
}
|
||||
turn := result.Value.CombatTurns[0]
|
||||
if turn.Actor != "Aria" || turn.SourceRefs[0].SourceID != request.Source.ID || turn.SourceRefs[0].StartUnitID != 10 || turn.SourceRefs[0].EndUnitID != 2 {
|
||||
if turn.Actor != "Aria" || turn.SourceRefs[0].SourceID != request.Source.ID || turn.SourceRefs[0].StartUnitID != 2 || turn.SourceRefs[0].EndUnitID != 10 {
|
||||
t.Fatalf("document-order endpoints=%#v", turn)
|
||||
}
|
||||
for i, call := range client.requests {
|
||||
@@ -96,13 +109,13 @@ func TestCombatNativeCorrectionKeepsEligibilityAndSourceContextFixed(t *testing.
|
||||
}
|
||||
|
||||
func TestResolvableReversedCombatEndpointsNeedNoSemanticCorrection(t *testing.T) {
|
||||
request := extractionRequest(t)
|
||||
request := nativeCombatRequest(t)
|
||||
request.Operation.Controls.MaxAdditionalTurns = 0
|
||||
calls := bindCombatAssessment(&request)
|
||||
client := &fakeCombatTurnsLLMClient{raw: knownTurn}
|
||||
result, err := newExtractor(t, client).Extract(context.Background(), request)
|
||||
if err != nil || result.Outcome.Disposition != contracts.ProducerAccepted || len(client.requests) != 1 || len(*calls) != 3 ||
|
||||
result.Value.CombatTurns[0].SourceRefs[0].StartUnitID != 10 || result.Value.CombatTurns[0].SourceRefs[0].EndUnitID != 2 {
|
||||
result.Value.CombatTurns[0].SourceRefs[0].StartUnitID != 2 || result.Value.CombatTurns[0].SourceRefs[0].EndUnitID != 10 {
|
||||
t.Fatalf("endpoint reliability=%#v %v, native=%d assessments=%#v", result, err, len(client.requests), *calls)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,17 +18,11 @@ import (
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/generic/chunk/units"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/generic/merge/appendorder"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/generic/normalize/noop"
|
||||
jsonoutput "gitea.maximumdirect.net/eric/notarius/internal/modules/generic/output/json"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/seriatim/input/transcript"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
type combatPipelineOutput struct{}
|
||||
|
||||
func (combatPipelineOutput) Key() string { return "test/combat-output" }
|
||||
func (combatPipelineOutput) Encode(context.Context, contracts.OutputRequest) (contracts.OutputResult, error) {
|
||||
return contracts.OutputResult{}, nil
|
||||
}
|
||||
|
||||
func TestNativeCombatPipelineHonorsSceneEligibilityWithoutSyntheticRequests(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
@@ -70,8 +64,7 @@ func TestNativeCombatPipelineHonorsSceneEligibilityWithoutSyntheticRequests(t *t
|
||||
return result, nil
|
||||
}))
|
||||
check(noop.RegisterTyped[dnd.CombatTurnList](registries.Normalizers, dnd.CombatTurnListKind))
|
||||
check(registries.Outputs.RegisterWithSpec(pipeline.ModuleSpec{Key: "test/combat-output", Stage: pipeline.StageOutput, ExecutionClass: contracts.ExecutionClassDeterministic},
|
||||
func() (contracts.OutputEncoder, error) { return combatPipelineOutput{}, nil }))
|
||||
check(jsonoutput.Register(registries.Outputs))
|
||||
check(shape.Register(registries.Validators))
|
||||
check(sourcerefs.Register(registries.Validators))
|
||||
check(relatedness.Register(registries.Validators))
|
||||
@@ -87,7 +80,7 @@ func TestNativeCombatPipelineHonorsSceneEligibilityWithoutSyntheticRequests(t *t
|
||||
path := filepath.Join(t.TempDir(), "scenes.json")
|
||||
check(os.WriteFile(path, content, 0600))
|
||||
profile := pipeline.PipelineProfile{
|
||||
ID: "native-combat", Input: pipeline.Binding(transcript.Key), Chunk: pipeline.Binding(units.Key), Output: pipeline.Binding("test/combat-output"),
|
||||
ID: "native-combat", Input: pipeline.Binding(transcript.Key), Chunk: pipeline.Binding(units.Key), Output: pipeline.Binding(jsonoutput.Key),
|
||||
LLMProfile: "profile-combat",
|
||||
Artifacts: map[string]pipeline.ArtifactLaneProfile{"combat": {
|
||||
Extract: pipeline.ModuleBinding{Module: Key, Validators: pipeline.ValidatorOverride{Set: true, Validators: []pipeline.ModuleBinding{
|
||||
@@ -110,14 +103,14 @@ func TestNativeCombatPipelineHonorsSceneEligibilityWithoutSyntheticRequests(t *t
|
||||
preparations := 0
|
||||
result, err := pipeline.New().Run(context.Background(), pipeline.RunInput{
|
||||
Prepared: prepared, SourceID: "combat-session", Workers: 1,
|
||||
RawInput: []byte(`{"segments":[{"id":1,"speaker":"Alice","start":0,"end":1,"text":"Aria strikes the ogre."}]}`),
|
||||
RawInput: []byte(`{"metadata":{},"segments":[{"id":1,"speaker":"Alice","start":0,"end":1,"text":"Aria strikes the ogre."}]}`),
|
||||
Execution: contracts.ExecutionOptions{OnPrepared: func(context.Context, promptkit.PreparedRun) { preparations++ }},
|
||||
})
|
||||
check(err)
|
||||
if len(client.requests) != test.wantCalls || preparations != test.wantCalls || result.FallbackCount != 0 || len(result.NormalizeOutputs) != 1 {
|
||||
if len(client.requests) != test.wantCalls || preparations != test.wantCalls || result.FallbackCount != 0 || len(result.OutputFiles) != 1 {
|
||||
t.Fatalf("pipeline=%#v native=%d/%d, want %d", result, len(client.requests), preparations, test.wantCalls)
|
||||
}
|
||||
value, err := combatcodec.New().Decode(result.NormalizeOutputs[0].Artifact.Content)
|
||||
value, err := combatcodec.New().Decode(result.OutputFiles[0].Bytes)
|
||||
check(err)
|
||||
if value.CombatTurns == nil || len(value.CombatTurns) != test.wantCalls {
|
||||
t.Fatalf("combat artifact=%#v", value)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package combatturns
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
@@ -100,7 +101,7 @@ func renderedInput(t *testing.T, request promptkit.GenerateRequest, name string)
|
||||
content := []byte(message.Content[start:])
|
||||
var fields map[string]json.RawMessage
|
||||
if json.Unmarshal(content, &fields) == nil && fields[key] != nil {
|
||||
return content
|
||||
return bytes.TrimSpace(content)
|
||||
}
|
||||
}
|
||||
t.Fatalf("missing rendered %s input", name)
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
eventcodec "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/codec/enemyevents"
|
||||
@@ -16,16 +15,10 @@ import (
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/generic/chunk/units"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/generic/merge/appendorder"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/generic/normalize/noop"
|
||||
jsonoutput "gitea.maximumdirect.net/eric/notarius/internal/modules/generic/output/json"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/seriatim/input/transcript"
|
||||
)
|
||||
|
||||
type enemyPipelineOutput struct{}
|
||||
|
||||
func (enemyPipelineOutput) Key() string { return "test/enemy-output" }
|
||||
func (enemyPipelineOutput) Encode(context.Context, contracts.OutputRequest) (contracts.OutputResult, error) {
|
||||
return contracts.OutputResult{}, nil
|
||||
}
|
||||
|
||||
func TestNativeEnemyPipelineRetainsIntrinsicCodecBoundaryWithEmptyValidators(t *testing.T) {
|
||||
for _, name := range []string{"Ashfang", " "} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
@@ -58,8 +51,7 @@ func TestNativeEnemyPipelineRetainsIntrinsicCodecBoundaryWithEmptyValidators(t *
|
||||
return result, nil
|
||||
}))
|
||||
check(noop.RegisterTyped[dnd.EnemyEventList](registries.Normalizers, dnd.EnemyEventListKind))
|
||||
check(registries.Outputs.RegisterWithSpec(pipeline.ModuleSpec{Key: "test/enemy-output", Stage: pipeline.StageOutput, ExecutionClass: contracts.ExecutionClassDeterministic},
|
||||
func() (contracts.OutputEncoder, error) { return enemyPipelineOutput{}, nil }))
|
||||
check(jsonoutput.Register(registries.Outputs))
|
||||
references := groundingReferences(t, "Ashfang", dnd.SceneKindCombat)
|
||||
content, err := scenecodec.New().Encode(dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{
|
||||
ID: "chunk-000001", SourceRef: source.SourceRef{SourceID: "combat-session", StartUnitID: 1, EndUnitID: 1},
|
||||
@@ -75,7 +67,7 @@ func TestNativeEnemyPipelineRetainsIntrinsicCodecBoundaryWithEmptyValidators(t *
|
||||
referenceSources[name] = pipeline.ExternalReference(path)
|
||||
}
|
||||
profile := pipeline.PipelineProfile{
|
||||
ID: "native-enemies", Input: pipeline.Binding(transcript.Key), Chunk: pipeline.Binding(units.Key), Output: pipeline.Binding("test/enemy-output"),
|
||||
ID: "native-enemies", Input: pipeline.Binding(transcript.Key), Chunk: pipeline.Binding(units.Key), Output: pipeline.Binding(jsonoutput.Key),
|
||||
LLMProfile: "enemy-profile",
|
||||
Artifacts: map[string]pipeline.ArtifactLaneProfile{"enemies": {
|
||||
Extract: pipeline.ModuleBinding{Module: Key, References: referenceSources, Validators: pipeline.ValidatorOverride{Set: true, Validators: []pipeline.ModuleBinding{}}},
|
||||
@@ -95,22 +87,22 @@ func TestNativeEnemyPipelineRetainsIntrinsicCodecBoundaryWithEmptyValidators(t *
|
||||
check(err)
|
||||
result, err := pipeline.New().Run(context.Background(), pipeline.RunInput{
|
||||
Prepared: prepared, SourceID: "combat-session", Workers: 1,
|
||||
RawInput: []byte(`{"segments":[{"id":1,"speaker":"GM","start":0,"end":1,"text":"Ashfang attacks the party."}]}`),
|
||||
RawInput: []byte(`{"metadata":{},"segments":[{"id":1,"speaker":"GM","start":0,"end":1,"text":"Ashfang attacks the party."}]}`),
|
||||
})
|
||||
if len(client.requests) != 1 {
|
||||
t.Fatalf("native calls=%d", len(client.requests))
|
||||
}
|
||||
if strings.TrimSpace(name) == "" {
|
||||
if err == nil || !strings.Contains(err.Error(), "serialize accepted extract output") || !strings.Contains(err.Error(), "name must not be empty") {
|
||||
if err == nil || !strings.Contains(err.Error(), "encode extract lane") || !strings.Contains(err.Error(), "name must not be empty") {
|
||||
t.Fatalf("intrinsic codec error=%v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
check(err)
|
||||
if result.FallbackCount != 0 || len(result.NormalizeOutputs) != 1 {
|
||||
if result.FallbackCount != 0 || len(result.OutputFiles) != 1 {
|
||||
t.Fatalf("assembled outcome=%#v", result)
|
||||
}
|
||||
value, err := eventcodec.New().Decode(result.NormalizeOutputs[0].Artifact.Content)
|
||||
value, err := eventcodec.New().Decode(result.OutputFiles[0].Bytes)
|
||||
check(err)
|
||||
if len(value.Events) != 1 || value.Events[0].Name != name || value.Events[0].SourceRefs[0].SourceID != "combat-session" {
|
||||
t.Fatalf("durable events=%#v", value)
|
||||
|
||||
@@ -117,7 +117,13 @@ func mustGroundingResolver(t *testing.T, references contracts.ReferenceSet) *gro
|
||||
func enemyExtractionRequest(t *testing.T) contracts.TypedExtractionRequest[dnd.EnemyEventList] {
|
||||
t.Helper()
|
||||
chunk := combatChunk()
|
||||
chunk.SourceID = chunk.Ref.SourceID
|
||||
chunk.Units = []source.SourceUnit{{ID: 1}, {ID: 2}, {ID: 3}, {ID: 4}}
|
||||
for index := range chunk.Units {
|
||||
unit := &chunk.Units[index]
|
||||
unit.Kind = "transcript_segment"
|
||||
unit.Ref = source.SourceRef{SourceID: chunk.SourceID, StartUnitID: unit.ID, EndUnitID: unit.ID}
|
||||
}
|
||||
chunk.MediaType = "application/json"
|
||||
chunk.Content = []byte(`{"id":"combat-scene","units":[1,2,3,4]}`)
|
||||
return contracts.TypedExtractionRequest[dnd.EnemyEventList]{
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
itemidentity "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/items/identity"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package itemoccurrences
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
@@ -97,7 +98,7 @@ func renderedInput(t *testing.T, request promptkit.GenerateRequest, name string)
|
||||
content := []byte(message.Content[start:])
|
||||
var fields map[string]json.RawMessage
|
||||
if json.Unmarshal(content, &fields) == nil && fields[key] != nil {
|
||||
return content
|
||||
return bytes.TrimSpace(content)
|
||||
}
|
||||
}
|
||||
t.Fatalf("missing rendered %s input", name)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package npcoccurrences
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
@@ -137,7 +138,7 @@ func renderedInput(t *testing.T, request promptkit.GenerateRequest, name string)
|
||||
content := []byte(message.Content[start:])
|
||||
var fields map[string]json.RawMessage
|
||||
if json.Unmarshal(content, &fields) == nil && fields[key] != nil {
|
||||
return content
|
||||
return bytes.TrimSpace(content)
|
||||
}
|
||||
}
|
||||
t.Fatalf("missing rendered %s input", name)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package scenedescriptions
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
@@ -104,7 +105,7 @@ func renderedInput(t *testing.T, request promptkit.GenerateRequest, name string)
|
||||
content := []byte(message.Content[start:])
|
||||
var fields map[string]json.RawMessage
|
||||
if json.Unmarshal(content, &fields) == nil && fields[key] != nil {
|
||||
return content
|
||||
return bytes.TrimSpace(content)
|
||||
}
|
||||
}
|
||||
t.Fatalf("missing rendered %s input", name)
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/generic/chunk/units"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/generic/merge/appendorder"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/generic/normalize/noop"
|
||||
jsonoutput "gitea.maximumdirect.net/eric/notarius/internal/modules/generic/output/json"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/seriatim/input/transcript"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
@@ -36,13 +37,6 @@ func (v observedSpellValidator) Validate(ctx context.Context, req contracts.Type
|
||||
return v.TypedValidator.Validate(ctx, req)
|
||||
}
|
||||
|
||||
type spellPipelineOutput struct{}
|
||||
|
||||
func (spellPipelineOutput) Key() string { return "test/spell-output" }
|
||||
func (spellPipelineOutput) Encode(context.Context, contracts.OutputRequest) (contracts.OutputResult, error) {
|
||||
return contracts.OutputResult{}, nil
|
||||
}
|
||||
|
||||
func TestNativeSpellPipelineAssessesEveryCandidateOnce(t *testing.T) {
|
||||
response := func(name string) string {
|
||||
return fmt.Sprintf(`{"spell_casts":[{"caster":"Aria","spell":%q,"source_refs":[{"start_unit_id":1,"end_unit_id":1}]}]}`, name)
|
||||
@@ -87,8 +81,7 @@ func TestNativeSpellPipelineAssessesEveryCandidateOnce(t *testing.T) {
|
||||
return result, nil
|
||||
}))
|
||||
check(noop.RegisterTyped[dnd.SpellList](registries.Normalizers, dnd.SpellListKind))
|
||||
check(registries.Outputs.RegisterWithSpec(pipeline.ModuleSpec{Key: "test/spell-output", Stage: pipeline.StageOutput, ExecutionClass: contracts.ExecutionClassDeterministic},
|
||||
func() (contracts.OutputEncoder, error) { return spellPipelineOutput{}, nil }))
|
||||
check(jsonoutput.Register(registries.Outputs))
|
||||
catalogValidator, err := spellcatalogvalidator.New(spellcatalogvalidator.Options{})
|
||||
check(err)
|
||||
validators := []contracts.TypedValidator[dnd.SpellList]{
|
||||
@@ -114,7 +107,7 @@ func TestNativeSpellPipelineAssessesEveryCandidateOnce(t *testing.T) {
|
||||
turns := 2
|
||||
profile := pipeline.PipelineProfile{
|
||||
ID: "native-spells", Input: pipeline.Binding(transcript.Key),
|
||||
Chunk: pipeline.Binding(units.Key), Output: pipeline.Binding("test/spell-output"),
|
||||
Chunk: pipeline.Binding(units.Key), Output: pipeline.Binding(jsonoutput.Key),
|
||||
LLMProfile: "profile-spells",
|
||||
Artifacts: map[string]pipeline.ArtifactLaneProfile{"spells": {
|
||||
Extract: pipeline.ModuleBinding{Module: Key, Mode: &test.mode, MaxAdditionalTurns: &turns,
|
||||
@@ -138,7 +131,7 @@ func TestNativeSpellPipelineAssessesEveryCandidateOnce(t *testing.T) {
|
||||
var preparations []promptkit.PreparedRun
|
||||
output, err := pipeline.New().Run(context.Background(), pipeline.RunInput{
|
||||
Prepared: prepared, SourceID: "spell-session", SessionID: "spell-session-override", Workers: 1,
|
||||
RawInput: []byte(`{"segments":[{"id":1,"speaker":"Alice","start":0,"end":1,"text":"I cast Cure Wounds."}]}`),
|
||||
RawInput: []byte(`{"metadata":{},"segments":[{"id":1,"speaker":"Alice","start":0,"end":1,"text":"I cast Cure Wounds."}]}`),
|
||||
Execution: contracts.ExecutionOptions{Execution: &promptkit.ExecutionTargetOverride{Model: model},
|
||||
OnPrepared: func(_ context.Context, value promptkit.PreparedRun) { preparations = append(preparations, value) }},
|
||||
})
|
||||
@@ -186,10 +179,10 @@ func TestNativeSpellPipelineAssessesEveryCandidateOnce(t *testing.T) {
|
||||
if test.wantError {
|
||||
return
|
||||
}
|
||||
if output.FallbackCount != test.fallbacks || len(output.NormalizeOutputs) != 1 {
|
||||
if output.FallbackCount != test.fallbacks || len(output.OutputFiles) != 1 {
|
||||
t.Fatalf("outcome = %#v", output)
|
||||
}
|
||||
value, err := spellcodec.New().Decode(output.NormalizeOutputs[0].Artifact.Content)
|
||||
value, err := spellcodec.New().Decode(output.OutputFiles[0].Bytes)
|
||||
check(err)
|
||||
if test.fallbacks > 0 {
|
||||
if value.SpellCasts == nil || len(value.SpellCasts) != 0 {
|
||||
|
||||
@@ -38,8 +38,6 @@ var requiredCapabilities = []string{"merged"}
|
||||
var providedCapabilities = []string{"normalized"}
|
||||
|
||||
var _ contracts.Normalizer[dnd.CombatTurnList] = (*Normalizer)(nil)
|
||||
var _ contracts.ManifestMetadataProvider = (*Normalizer)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Normalizer)(nil)
|
||||
|
||||
type Options struct{}
|
||||
|
||||
@@ -66,35 +64,6 @@ func (n *Normalizer) Key() string { return Key }
|
||||
|
||||
func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
||||
|
||||
func (n *Normalizer) ManifestMetadata() map[string]any {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
metadata := map[string]any{
|
||||
"normalization_policy": normalizationPolicy,
|
||||
"identity_policy": identity.Policy,
|
||||
}
|
||||
seeded := n.npcResolver.Seeded()
|
||||
if seeded.Bound() {
|
||||
metadata["npc_registry_digest"] = seeded.Digest()
|
||||
metadata["npc_count"] = seeded.Count()
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
|
||||
func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
fingerprints := []pipeline.CheckpointFingerprint{
|
||||
{Name: "normalization_policy", Value: normalizationPolicy},
|
||||
{Name: "identity_policy", Value: identity.Policy},
|
||||
}
|
||||
seeded := n.npcResolver.Seeded()
|
||||
fingerprints = append(fingerprints, pipeline.CheckpointFingerprint{Name: "npc_registry", Value: seeded.ProjectionDigest()})
|
||||
return fingerprints
|
||||
}
|
||||
|
||||
func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.CombatTurnList]) (contracts.TypedNormalizeResult[dnd.CombatTurnList], error) {
|
||||
if n == nil {
|
||||
return contracts.TypedNormalizeResult[dnd.CombatTurnList]{}, normalizerErrorf("normalizer must not be nil")
|
||||
|
||||
@@ -109,9 +109,6 @@ func TestNormalizeResolvesOperationNPCOverrideWithoutSingletonMetadata(t *testin
|
||||
if turn.Actor != "Aria" {
|
||||
t.Fatalf("operation-normalized turn = %#v, want Aria", turn)
|
||||
}
|
||||
if metadata := normalizer.ManifestMetadata(); metadata["npc_registry_digest"] != nil || metadata["npc_count"] != nil {
|
||||
t.Fatalf("singleton metadata = %#v, want no operation-varying NPC identity", metadata)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeOrdersBySourcePositionAndCollapsesExactDuplicates(t *testing.T) {
|
||||
@@ -250,29 +247,16 @@ func TestNormalizePreservesNilAndPresentEmptyStorage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizerPreparationMetadataFingerprintsAndModuleContract(t *testing.T) {
|
||||
unbound, err := New(Options{})
|
||||
func TestNormalizerPreparationAndModuleContract(t *testing.T) {
|
||||
_, err := New(Options{})
|
||||
if err != nil {
|
||||
t.Fatalf("New() error = %v", err)
|
||||
}
|
||||
if metadata := unbound.ManifestMetadata(); metadata["npc_registry_digest"] != nil || metadata["npc_count"] != nil || metadata["normalization_policy"] != normalizationPolicy || metadata["identity_policy"] != identity.Policy {
|
||||
t.Fatalf("unbound metadata = %#v", metadata)
|
||||
}
|
||||
if got := unbound.CheckpointFingerprints(); len(got) != 3 || got[0].Name != "normalization_policy" || got[1].Name != "identity_policy" || got[2].Name != "npc_registry" || got[2].Value == "" {
|
||||
t.Fatalf("unbound fingerprints = %#v", got)
|
||||
}
|
||||
|
||||
bound, err := New(Options{}, npcReferences(t))
|
||||
_, err = New(Options{}, npcReferences(t))
|
||||
if err != nil {
|
||||
t.Fatalf("bound New() error = %v", err)
|
||||
}
|
||||
metadata := bound.ManifestMetadata()
|
||||
if metadata["npc_registry_digest"] == "" || metadata["npc_count"] != 2 {
|
||||
t.Fatalf("bound metadata = %#v", metadata)
|
||||
}
|
||||
if got := bound.CheckpointFingerprints(); len(got) != 3 || got[2].Name != "npc_registry" || got[2].Value == "" {
|
||||
t.Fatalf("bound fingerprints = %#v", got)
|
||||
}
|
||||
if spec := ModuleSpec(); spec.Key != Key || spec.Stage != pipeline.StageNormalize || spec.ArtifactKind != dnd.CombatTurnListKind || !reflect.DeepEqual(spec.Requires, []string{"merged"}) || !reflect.DeepEqual(spec.Provides, []string{"normalized"}) || len(spec.ReferenceSlots) != 1 || spec.ReferenceSlots[0].Name != NPCRegistryReferenceSlot {
|
||||
t.Fatalf("ModuleSpec() = %#v", spec)
|
||||
}
|
||||
|
||||
@@ -36,8 +36,6 @@ var requiredCapabilities = []string{"merged"}
|
||||
var providedCapabilities = []string{"normalized"}
|
||||
|
||||
var _ contracts.Normalizer[dnd.EnemyEventList] = (*Normalizer)(nil)
|
||||
var _ contracts.ManifestMetadataProvider = (*Normalizer)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Normalizer)(nil)
|
||||
|
||||
type Options struct{}
|
||||
|
||||
@@ -64,27 +62,6 @@ func (n *Normalizer) Key() string { return Key }
|
||||
|
||||
func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
||||
|
||||
func (n *Normalizer) ManifestMetadata() map[string]any {
|
||||
if n == nil || n.npcResolver == nil {
|
||||
return nil
|
||||
}
|
||||
metadata := map[string]any{"normalization_policy": normalizationPolicy}
|
||||
if seeded := n.npcResolver.Seeded(); seeded.Bound() {
|
||||
metadata["npc_registry_digest"] = seeded.Digest()
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
|
||||
func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
if n == nil || n.npcResolver == nil {
|
||||
return nil
|
||||
}
|
||||
return []pipeline.CheckpointFingerprint{
|
||||
{Name: "normalization_policy", Value: normalizationPolicy},
|
||||
{Name: "npc_registry", Value: n.npcResolver.Seeded().ProjectionDigest()},
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.EnemyEventList]) (contracts.TypedNormalizeResult[dnd.EnemyEventList], error) {
|
||||
if n == nil || n.npcResolver == nil {
|
||||
return contracts.TypedNormalizeResult[dnd.EnemyEventList]{}, normalizerErrorf("normalizer must not be nil")
|
||||
|
||||
@@ -2,7 +2,6 @@ package enemyevents
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -129,9 +128,6 @@ func TestNormalizeRequiresRegistryAndKeepsOperationContentOutOfMetadata(t *testi
|
||||
if err != nil || result.Value.Events == nil {
|
||||
t.Fatalf("Normalize() = %#v, %v", result, err)
|
||||
}
|
||||
if metadata := normalizer.ManifestMetadata(); metadata["npc_registry_digest"] != nil {
|
||||
t.Fatalf("operation registry leaked into metadata: %#v", metadata)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizerContractAndFindingBound(t *testing.T) {
|
||||
@@ -152,15 +148,6 @@ func TestNormalizerContractAndFindingBound(t *testing.T) {
|
||||
if spec, ok := registry.Spec(Key); !ok || spec.ArtifactKind != dnd.EnemyEventListKind {
|
||||
t.Fatalf("registered spec = %#v, %t", spec, ok)
|
||||
}
|
||||
metadata := normalizer.ManifestMetadata()
|
||||
if metadata["normalization_policy"] != normalizationPolicy || !strings.HasPrefix(metadata["npc_registry_digest"].(string), "sha256:") {
|
||||
t.Fatalf("metadata = %#v", metadata)
|
||||
}
|
||||
encoded, err := json.Marshal(metadata)
|
||||
if err != nil || strings.Contains(string(encoded), "Ária") {
|
||||
t.Fatalf("unsafe metadata = %s, %v", encoded, err)
|
||||
}
|
||||
|
||||
count := contracts.MaxDiagnosticSamples + 5
|
||||
document := &source.SourceDocument{ID: "session", Units: make([]source.SourceUnit, count)}
|
||||
input := dnd.EnemyEventList{Events: make([]dnd.EnemyEvent, count)}
|
||||
|
||||
@@ -37,8 +37,6 @@ var requiredCapabilities = []string{"merged"}
|
||||
var providedCapabilities = []string{"normalized"}
|
||||
|
||||
var _ contracts.Normalizer[dnd.ItemOccurrenceList] = (*Normalizer)(nil)
|
||||
var _ contracts.ManifestMetadataProvider = (*Normalizer)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Normalizer)(nil)
|
||||
|
||||
type Options struct{}
|
||||
|
||||
@@ -63,30 +61,6 @@ func (n *Normalizer) Key() string { return Key }
|
||||
|
||||
func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
||||
|
||||
func (n *Normalizer) ManifestMetadata() map[string]any {
|
||||
if n == nil || n.itemResolver == nil {
|
||||
return nil
|
||||
}
|
||||
metadata := map[string]any{"normalization_policy": normalizationPolicy, "comparison_policy": shared.TextComparisonPolicy}
|
||||
seeded := n.itemResolver.Seeded()
|
||||
if seeded.Bound() {
|
||||
metadata["item_registry_digest"] = seeded.Digest()
|
||||
metadata["item_count"] = seeded.Count()
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
|
||||
func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
if n == nil || n.itemResolver == nil {
|
||||
return nil
|
||||
}
|
||||
return []pipeline.CheckpointFingerprint{
|
||||
{Name: "normalization_policy", Value: normalizationPolicy},
|
||||
{Name: "comparison_policy", Value: shared.TextComparisonPolicy},
|
||||
{Name: "item_registry", Value: n.itemResolver.Seeded().ProjectionDigest()},
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.ItemOccurrenceList]) (contracts.TypedNormalizeResult[dnd.ItemOccurrenceList], error) {
|
||||
if n == nil || n.itemResolver == nil {
|
||||
return contracts.TypedNormalizeResult[dnd.ItemOccurrenceList]{}, normalizerErrorf("normalizer must not be nil")
|
||||
|
||||
@@ -305,7 +305,7 @@ func TestNormalizeAppliesIndependentGroupAndCountsAllOmissions(t *testing.T) {
|
||||
warning = group.Samples[0].Message
|
||||
}
|
||||
}
|
||||
if !strings.Contains(warning, "2 proposal group(s)") || !strings.Contains(warning, "preserved separately") || !strings.Contains(warning, "same denomination") {
|
||||
if !strings.Contains(warning, "2 proposal group(s)") || !strings.Contains(warning, "preserved separately") || !strings.Contains(warning, "denomination") {
|
||||
t.Fatalf("fallback warning = %q", warning)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,8 +38,6 @@ var requiredCapabilities = []string{"merged"}
|
||||
var providedCapabilities = []string{"normalized"}
|
||||
|
||||
var _ contracts.Normalizer[dnd.LocationOccurrenceList] = (*Normalizer)(nil)
|
||||
var _ contracts.ManifestMetadataProvider = (*Normalizer)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Normalizer)(nil)
|
||||
|
||||
type Options struct{}
|
||||
|
||||
@@ -66,29 +64,6 @@ func (n *Normalizer) Key() string { return Key }
|
||||
|
||||
func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
||||
|
||||
func (n *Normalizer) ManifestMetadata() map[string]any {
|
||||
if n == nil || n.locationResolver == nil {
|
||||
return nil
|
||||
}
|
||||
metadata := map[string]any{"normalization_policy": normalizationPolicy}
|
||||
seeded := n.locationResolver.Seeded()
|
||||
if seeded.Bound() {
|
||||
metadata["location_registry_digest"] = seeded.Digest()
|
||||
metadata["location_count"] = seeded.Count()
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
|
||||
func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
if n == nil || n.locationResolver == nil {
|
||||
return nil
|
||||
}
|
||||
return []pipeline.CheckpointFingerprint{
|
||||
{Name: "normalization_policy", Value: normalizationPolicy},
|
||||
{Name: "location_registry", Value: n.locationResolver.Seeded().IdentityDigest()},
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.LocationOccurrenceList]) (contracts.TypedNormalizeResult[dnd.LocationOccurrenceList], error) {
|
||||
if n == nil || n.locationResolver == nil {
|
||||
return contracts.TypedNormalizeResult[dnd.LocationOccurrenceList]{}, normalizerErrorf("normalizer must not be nil")
|
||||
|
||||
@@ -114,12 +114,6 @@ func TestNormalizerContractsRequiredRegistryAndFindingBounds(t *testing.T) {
|
||||
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
|
||||
t.Fatal("DecodeOptions() accepted unknown options")
|
||||
}
|
||||
if metadata := normalizer.ManifestMetadata(); metadata["normalization_policy"] != normalizationPolicy || metadata["location_registry_digest"] == "" || metadata["location_count"] != 1 {
|
||||
t.Fatalf("metadata = %#v", metadata)
|
||||
}
|
||||
if fingerprints := normalizer.CheckpointFingerprints(); len(fingerprints) != 2 || fingerprints[1].Name != "location_registry" || fingerprints[1].Value == "" {
|
||||
t.Fatalf("fingerprints = %#v", fingerprints)
|
||||
}
|
||||
if _, err := newNormalizer(t).Normalize(context.Background(), normalizeRequest(dnd.LocationOccurrenceList{}, nil, contracts.ReferenceSet{})); err == nil || !strings.Contains(err.Error(), "required") {
|
||||
t.Fatalf("unbound registry error = %v", err)
|
||||
}
|
||||
|
||||
@@ -35,8 +35,6 @@ var requiredCapabilities = []string{"merged"}
|
||||
var providedCapabilities = []string{"normalized"}
|
||||
|
||||
var _ contracts.Normalizer[dnd.NPCOccurrenceList] = (*Normalizer)(nil)
|
||||
var _ contracts.ManifestMetadataProvider = (*Normalizer)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Normalizer)(nil)
|
||||
|
||||
type Options struct{}
|
||||
|
||||
@@ -63,31 +61,6 @@ func (n *Normalizer) Key() string { return Key }
|
||||
|
||||
func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
||||
|
||||
func (n *Normalizer) ManifestMetadata() map[string]any {
|
||||
if n == nil || n.npcResolver == nil {
|
||||
return nil
|
||||
}
|
||||
metadata := map[string]any{
|
||||
"normalization_policy": normalizationPolicy,
|
||||
}
|
||||
seeded := n.npcResolver.Seeded()
|
||||
if seeded.Bound() {
|
||||
metadata["npc_registry_digest"] = seeded.Digest()
|
||||
metadata["npc_count"] = seeded.Count()
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
|
||||
func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
if n == nil || n.npcResolver == nil {
|
||||
return nil
|
||||
}
|
||||
return []pipeline.CheckpointFingerprint{
|
||||
{Name: "normalization_policy", Value: normalizationPolicy},
|
||||
{Name: "npc_registry", Value: n.npcResolver.Seeded().IdentityDigest()},
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.NPCOccurrenceList]) (contracts.TypedNormalizeResult[dnd.NPCOccurrenceList], error) {
|
||||
if n == nil || n.npcResolver == nil {
|
||||
return contracts.TypedNormalizeResult[dnd.NPCOccurrenceList]{}, normalizerErrorf("normalizer must not be nil")
|
||||
|
||||
@@ -64,9 +64,6 @@ func TestNormalizeRequiresOperationRegistryAndPreservesEmptyRepresentation(t *te
|
||||
t.Fatalf("Normalize() = %#v, %v for input %#v", result, err, input)
|
||||
}
|
||||
}
|
||||
if metadata := normalizer.ManifestMetadata(); metadata["npc_registry_digest"] != nil || metadata["npc_count"] != nil {
|
||||
t.Fatalf("operation registry leaked into metadata: %#v", metadata)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeRejectsUnknownIDsAndMismatchedNames(t *testing.T) {
|
||||
@@ -120,20 +117,14 @@ func TestNormalizeOrdersAndCollapsesExactDuplicatesOnly(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizerContractAndDeterministicFindings(t *testing.T) {
|
||||
normalizer, err := New(Options{}, npcReferences(t))
|
||||
func TestNormalizerContract(t *testing.T) {
|
||||
_, err := New(Options{}, npcReferences(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if spec := ModuleSpec(); spec.Key != Key || spec.Stage != pipeline.StageNormalize || spec.ArtifactKind != dnd.NPCOccurrenceListKind || len(spec.ReferenceSlots) != 1 || spec.ReferenceSlots[0].Name != NPCRegistryReferenceSlot || !spec.ReferenceSlots[0].Required {
|
||||
t.Fatalf("ModuleSpec() = %#v", spec)
|
||||
}
|
||||
if metadata := normalizer.ManifestMetadata(); metadata["normalization_policy"] != normalizationPolicy || metadata["npc_registry_digest"] == "" || metadata["npc_count"] != 2 {
|
||||
t.Fatalf("metadata = %#v", metadata)
|
||||
}
|
||||
if fingerprints := normalizer.CheckpointFingerprints(); len(fingerprints) != 2 || fingerprints[1].Name != "npc_registry" || fingerprints[1].Value == "" {
|
||||
t.Fatalf("fingerprints = %#v", fingerprints)
|
||||
}
|
||||
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
|
||||
t.Fatal("DecodeOptions() accepted an unknown option")
|
||||
}
|
||||
|
||||
@@ -27,8 +27,6 @@ var requiredCapabilities = []string{"merged"}
|
||||
var providedCapabilities = []string{"normalized"}
|
||||
|
||||
var _ contracts.Normalizer[dnd.SceneDescriptionList] = (*Normalizer)(nil)
|
||||
var _ contracts.ManifestMetadataProvider = (*Normalizer)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Normalizer)(nil)
|
||||
|
||||
type Options struct{}
|
||||
type Normalizer struct{}
|
||||
@@ -39,20 +37,6 @@ func (n *Normalizer) Key() string { return Key }
|
||||
|
||||
func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return nil }
|
||||
|
||||
func (n *Normalizer) ManifestMetadata() map[string]any {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
return map[string]any{"normalizer_policy": normalizerPolicy}
|
||||
}
|
||||
|
||||
func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
return []pipeline.CheckpointFingerprint{{Name: "normalizer_policy", Value: normalizerPolicy}}
|
||||
}
|
||||
|
||||
func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.SceneDescriptionList]) (contracts.TypedNormalizeResult[dnd.SceneDescriptionList], error) {
|
||||
if n == nil {
|
||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{}, normalizerErrorf("normalizer must not be nil")
|
||||
|
||||
@@ -131,12 +131,6 @@ func TestNormalizerContractAndCancellation(t *testing.T) {
|
||||
t.Fatalf("Register() error = %v", err)
|
||||
}
|
||||
normalizer := New(Options{})
|
||||
if metadata := normalizer.ManifestMetadata(); metadata["normalizer_policy"] != normalizerPolicy {
|
||||
t.Fatalf("metadata = %#v", metadata)
|
||||
}
|
||||
if got, want := normalizer.CheckpointFingerprints(), []pipeline.CheckpointFingerprint{{Name: "normalizer_policy", Value: normalizerPolicy}}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("fingerprints = %#v, want %#v", got, want)
|
||||
}
|
||||
canceled, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if _, err := normalizer.Normalize(canceled, normalizeRequest(dnd.SceneDescriptionList{}, testDocument())); err == nil || !strings.Contains(err.Error(), "context") {
|
||||
|
||||
@@ -33,8 +33,6 @@ var requiredCapabilities = []string{"merged"}
|
||||
var providedCapabilities = []string{"normalized"}
|
||||
|
||||
var _ contracts.Normalizer[dnd.SpellList] = (*Normalizer)(nil)
|
||||
var _ contracts.ManifestMetadataProvider = (*Normalizer)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Normalizer)(nil)
|
||||
|
||||
type Options struct{}
|
||||
|
||||
@@ -65,28 +63,6 @@ func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot {
|
||||
return referenceSlots()
|
||||
}
|
||||
|
||||
func (n *Normalizer) ManifestMetadata() map[string]any {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
return map[string]any{
|
||||
"catalog_base_id": n.effectiveCatalog.BaseID(),
|
||||
"catalog_digest": n.effectiveCatalog.Digest(),
|
||||
"catalog_overlay_ids": append([]string(nil), n.effectiveCatalog.OverlayIDs()...),
|
||||
"normalization_policy": normalizationPolicy,
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Normalizer) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
return []pipeline.CheckpointFingerprint{
|
||||
{Name: "effective_catalog", Value: n.effectiveCatalog.Digest()},
|
||||
{Name: "normalization_policy", Value: normalizationPolicy},
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.SpellList]) (contracts.TypedNormalizeResult[dnd.SpellList], error) {
|
||||
if n == nil {
|
||||
return contracts.TypedNormalizeResult[dnd.SpellList]{}, normalizerErrorf("normalizer must not be nil")
|
||||
|
||||
@@ -100,35 +100,6 @@ func TestNewRejectsInvalidCatalogReferencesDuringConstruction(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdentityAndMetadataAreDefensive(t *testing.T) {
|
||||
normalizer, err := New(Options{}, overlayReference())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if NormalizationPolicy != "dnd.spells.normalize.v2" {
|
||||
t.Fatalf("NormalizationPolicy = %q, want v2 policy", NormalizationPolicy)
|
||||
}
|
||||
|
||||
fingerprints := normalizer.CheckpointFingerprints()
|
||||
if len(fingerprints) != 2 || fingerprints[0].Name != "effective_catalog" || fingerprints[0].Value != normalizer.effectiveCatalog.Digest() || fingerprints[1] != (pipeline.CheckpointFingerprint{Name: "normalization_policy", Value: normalizationPolicy}) {
|
||||
t.Fatalf("fingerprints = %#v, want catalog and normalization policy fingerprints", fingerprints)
|
||||
}
|
||||
fingerprints[0].Name = "changed"
|
||||
fingerprints[0].Value = "changed"
|
||||
if got := normalizer.CheckpointFingerprints(); len(got) != 2 || got[0].Name != "effective_catalog" || got[0].Value != normalizer.effectiveCatalog.Digest() || got[1] != (pipeline.CheckpointFingerprint{Name: "normalization_policy", Value: normalizationPolicy}) {
|
||||
t.Fatalf("fingerprints were not defensive: %#v", got)
|
||||
}
|
||||
|
||||
metadata := normalizer.ManifestMetadata()
|
||||
if metadata["catalog_base_id"] != spellcatalog.SRD5E2014ID || metadata["catalog_digest"] != normalizer.effectiveCatalog.Digest() || metadata["normalization_policy"] != normalizationPolicy {
|
||||
t.Fatalf("metadata = %#v, want catalog identity", metadata)
|
||||
}
|
||||
metadata["catalog_overlay_ids"].([]string)[0] = "changed"
|
||||
if got := normalizer.ManifestMetadata()["catalog_overlay_ids"].([]string); !reflect.DeepEqual(got, []string{"campaign.example"}) {
|
||||
t.Fatalf("metadata overlay IDs were not defensive: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeCanonicalizesNamesAndReportsUnresolvedNames(t *testing.T) {
|
||||
normalizer := newNormalizer(t)
|
||||
input := dnd.SpellList{SpellCasts: []dnd.SpellCast{
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/assets"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
"go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"strings"
|
||||
|
||||
rootassets "gitea.maximumdirect.net/eric/notarius/assets"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/llm"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/promptfs"
|
||||
)
|
||||
|
||||
// PromptAssetManifest is the ordered set of assets that make up one prompt.
|
||||
// Module files are addressed in the owning module filesystem. SharedFiles use
|
||||
// names in sharedPromptPaths, while ExternalSharedFiles are explicit
|
||||
// caller-owned descriptors. Both kinds are mounted beneath sharedassets.
|
||||
type PromptAssetManifest struct {
|
||||
ModuleDir string
|
||||
ModuleFiles []promptfs.ModulePromptFile
|
||||
SharedFiles []string
|
||||
ExternalSharedFiles []promptfs.SharedPromptFile
|
||||
}
|
||||
|
||||
var sharedPromptPaths = map[string]string{
|
||||
"common-dnd-system.md": "prompts/common-dnd-system.md",
|
||||
"common-dnd-extraction-evidence.md": "prompts/common-dnd-extraction-evidence.md",
|
||||
"common-dnd-identity.md": "prompts/common-dnd-identity.md",
|
||||
"common-dnd-transcript-full.md": "prompts/common-dnd-transcript-full.md",
|
||||
"common-dnd-transcript-chunk.md": "prompts/common-dnd-transcript-chunk.md",
|
||||
"common-dnd-scene-combat-policy.md": "prompts/common-dnd-scene-combat-policy.md",
|
||||
"common-dnd-references.md": "prompts/common-dnd-references.md",
|
||||
"common-dnd-npc-registry.md": "prompts/common-dnd-npc-registry.md",
|
||||
}
|
||||
|
||||
func sharedAssetFS() (fs.FS, error) {
|
||||
assets, err := fs.Sub(rootassets.FS(), "dnd/shared")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scope shared D&D prompt assets: %w", err)
|
||||
}
|
||||
return assets, nil
|
||||
}
|
||||
|
||||
func (manifest PromptAssetManifest) PromptFS(moduleFS fs.FS) (fs.FS, error) {
|
||||
sharedFiles, err := manifest.sharedPromptFiles()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
moduleFiles := append([]promptfs.ModulePromptFile(nil), manifest.ModuleFiles...)
|
||||
return promptfs.ModulePromptFS(manifest.ModuleDir, moduleFS, moduleFiles, sharedFiles...)
|
||||
}
|
||||
|
||||
func (manifest PromptAssetManifest) Hash(moduleFS fs.FS) (string, error) {
|
||||
sharedFiles, err := manifest.sharedPromptFiles()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
moduleFiles := append([]promptfs.ModulePromptFile(nil), manifest.ModuleFiles...)
|
||||
if _, err := promptfs.ModulePromptFS(manifest.ModuleDir, moduleFS, moduleFiles, sharedFiles...); err != nil {
|
||||
return "", err
|
||||
}
|
||||
parts := make([]llm.AssetHashPart, 0, len(manifest.ModuleFiles)+len(sharedFiles))
|
||||
for _, file := range manifest.ModuleFiles {
|
||||
parts = append(parts, llm.AssetHashPart{FS: moduleFS, Path: file.Path})
|
||||
}
|
||||
for _, file := range sharedFiles {
|
||||
parts = append(parts, llm.AssetHashPart{FS: file.FS, Path: file.Path})
|
||||
}
|
||||
return llm.HashAssets(parts)
|
||||
}
|
||||
|
||||
func (manifest PromptAssetManifest) sharedPromptFiles() ([]promptfs.SharedPromptFile, error) {
|
||||
files, err := resolveSharedPromptFiles(manifest.SharedFiles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return append(files, manifest.ExternalSharedFiles...), nil
|
||||
}
|
||||
|
||||
func resolveSharedPromptFiles(names []string) ([]promptfs.SharedPromptFile, error) {
|
||||
assets, err := sharedAssetFS()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
files := make([]promptfs.SharedPromptFile, 0, len(names))
|
||||
seen := make(map[string]struct{}, len(names))
|
||||
for _, name := range names {
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("shared prompt asset name must not be empty")
|
||||
}
|
||||
if strings.ContainsAny(name, `/\\`) {
|
||||
return nil, fmt.Errorf("shared prompt asset name %q must not contain path separators", name)
|
||||
}
|
||||
if _, ok := seen[name]; ok {
|
||||
return nil, fmt.Errorf("duplicate shared prompt asset name %q", name)
|
||||
}
|
||||
path, ok := sharedPromptPaths[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown shared prompt asset name %q", name)
|
||||
}
|
||||
seen[name] = struct{}{}
|
||||
files = append(files, promptfs.SharedPromptFile{
|
||||
Name: name,
|
||||
FS: assets,
|
||||
Path: path,
|
||||
})
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
@@ -1,310 +0,0 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/llm"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/promptfs"
|
||||
)
|
||||
|
||||
func TestPromptAssetManifestPromptFS(t *testing.T) {
|
||||
manifest := PromptAssetManifest{
|
||||
ModuleDir: "dnd.test",
|
||||
ModuleFiles: []promptfs.ModulePromptFile{
|
||||
{Name: "prompt.yaml", Path: "assets/prompts/prompt.yaml"},
|
||||
{Name: "instructions.md", Path: "assets/prompts/instructions.md"},
|
||||
},
|
||||
SharedFiles: []string{
|
||||
"common-dnd-system.md",
|
||||
"common-dnd-transcript-full.md",
|
||||
"common-dnd-transcript-chunk.md",
|
||||
},
|
||||
}
|
||||
|
||||
fsys, err := manifest.PromptFS(fstest.MapFS{
|
||||
"assets/prompts/prompt.yaml": {Data: []byte("id: dnd.test")},
|
||||
"assets/prompts/instructions.md": {Data: []byte("instructions")},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("PromptFS() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
wantModule := map[string]string{
|
||||
"assets/prompts/dnd.test/prompt.yaml": "id: dnd.test",
|
||||
"assets/prompts/dnd.test/instructions.md": "instructions",
|
||||
}
|
||||
for path, wantContent := range wantModule {
|
||||
content, err := fs.ReadFile(fsys, path)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile(%q) error = %v, want nil", path, err)
|
||||
}
|
||||
if string(content) != wantContent {
|
||||
t.Fatalf("ReadFile(%q) = %q, want %q", path, content, wantContent)
|
||||
}
|
||||
}
|
||||
for _, path := range []string{
|
||||
"assets/prompts/dnd.test/sharedassets/common-dnd-system.md",
|
||||
"assets/prompts/dnd.test/sharedassets/common-dnd-transcript-full.md",
|
||||
"assets/prompts/dnd.test/sharedassets/common-dnd-transcript-chunk.md",
|
||||
} {
|
||||
content, err := fs.ReadFile(fsys, path)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile(%q) error = %v, want nil", path, err)
|
||||
}
|
||||
if len(content) == 0 {
|
||||
t.Fatalf("ReadFile(%q) returned empty content, want mounted asset", path)
|
||||
}
|
||||
}
|
||||
for _, path := range []string{
|
||||
"assets/prompts/dnd.test/sharedassets/common-dnd-references.md",
|
||||
"assets/prompts/dnd.test/sharedassets/common-dnd-npc-registry.md",
|
||||
} {
|
||||
if _, err := fs.ReadFile(fsys, path); err == nil {
|
||||
t.Fatalf("ReadFile(%q) succeeded, want unlisted shared asset to be absent", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptAssetManifestRejectsInvalidSharedNames(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
sharedFiles []string
|
||||
wantError string
|
||||
}{
|
||||
{name: "unknown", sharedFiles: []string{"missing.md"}, wantError: "unknown shared prompt asset name"},
|
||||
{name: "duplicate", sharedFiles: []string{"common-dnd-system.md", "common-dnd-system.md"}, wantError: "duplicate shared prompt asset name"},
|
||||
{name: "empty", sharedFiles: []string{""}, wantError: "must not be empty"},
|
||||
{name: "slash path", sharedFiles: []string{"nested/common-dnd-system.md"}, wantError: "must not contain path separators"},
|
||||
{name: "backslash path", sharedFiles: []string{`nested\common-dnd-system.md`}, wantError: "must not contain path separators"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
_, err := (PromptAssetManifest{
|
||||
ModuleDir: "dnd.test",
|
||||
ModuleFiles: []promptfs.ModulePromptFile{
|
||||
{Name: "prompt.yaml", Path: "assets/prompts/prompt.yaml"},
|
||||
},
|
||||
SharedFiles: test.sharedFiles,
|
||||
}).PromptFS(fstest.MapFS{
|
||||
"assets/prompts/prompt.yaml": {Data: []byte("id: dnd.test")},
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), test.wantError) {
|
||||
t.Fatalf("PromptFS() error = %v, want %q", err, test.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptAssetManifestRejectsMissingModuleFile(t *testing.T) {
|
||||
_, err := (PromptAssetManifest{
|
||||
ModuleDir: "dnd.test",
|
||||
ModuleFiles: []promptfs.ModulePromptFile{
|
||||
{Name: "instructions.md", Path: "assets/prompts/instructions.md"},
|
||||
},
|
||||
}).PromptFS(fstest.MapFS{})
|
||||
if err == nil || !strings.Contains(err.Error(), "read module prompt asset assets/prompts/instructions.md") {
|
||||
t.Fatalf("PromptFS() error = %v, want missing module asset context", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptAssetManifestRejectsMissingSharedFile(t *testing.T) {
|
||||
const name = "missing-for-test.md"
|
||||
const path = "prompts/missing-for-test.md"
|
||||
previous, existed := sharedPromptPaths[name]
|
||||
sharedPromptPaths[name] = path
|
||||
t.Cleanup(func() {
|
||||
if existed {
|
||||
sharedPromptPaths[name] = previous
|
||||
} else {
|
||||
delete(sharedPromptPaths, name)
|
||||
}
|
||||
})
|
||||
|
||||
_, err := (PromptAssetManifest{
|
||||
ModuleDir: "dnd.test",
|
||||
ModuleFiles: []promptfs.ModulePromptFile{
|
||||
{Name: "prompt.yaml", Path: "assets/prompts/prompt.yaml"},
|
||||
},
|
||||
SharedFiles: []string{name},
|
||||
}).PromptFS(fstest.MapFS{
|
||||
"assets/prompts/prompt.yaml": {Data: []byte("id: dnd.test")},
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "read shared prompt asset "+path) {
|
||||
t.Fatalf("PromptFS() error = %v, want missing shared asset context", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptAssetManifestHashMatchesManifestParts(t *testing.T) {
|
||||
moduleFS := fstest.MapFS{
|
||||
"assets/prompts/prompt.yaml": {Data: []byte("id: dnd.test")},
|
||||
"assets/prompts/instructions.md": {Data: []byte("instructions")},
|
||||
}
|
||||
manifest := PromptAssetManifest{
|
||||
ModuleDir: "dnd.test",
|
||||
ModuleFiles: []promptfs.ModulePromptFile{
|
||||
{Name: "prompt.yaml", Path: "assets/prompts/prompt.yaml"},
|
||||
{Name: "instructions.md", Path: "assets/prompts/instructions.md"},
|
||||
},
|
||||
SharedFiles: []string{
|
||||
"common-dnd-transcript-chunk.md",
|
||||
"common-dnd-system.md",
|
||||
},
|
||||
}
|
||||
|
||||
got, err := manifest.Hash(moduleFS)
|
||||
if err != nil {
|
||||
t.Fatalf("Hash() error = %v, want nil", err)
|
||||
}
|
||||
sharedFS, err := sharedAssetFS()
|
||||
if err != nil {
|
||||
t.Fatalf("sharedAssetFS() error = %v, want nil", err)
|
||||
}
|
||||
want, err := llm.HashAssets([]llm.AssetHashPart{
|
||||
{FS: moduleFS, Path: "assets/prompts/prompt.yaml"},
|
||||
{FS: moduleFS, Path: "assets/prompts/instructions.md"},
|
||||
{FS: sharedFS, Path: "prompts/common-dnd-transcript-chunk.md"},
|
||||
{FS: sharedFS, Path: "prompts/common-dnd-system.md"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("HashAssets() error = %v, want nil", err)
|
||||
}
|
||||
if got != want {
|
||||
t.Fatalf("Hash() = %q, want independently assembled manifest hash %q", got, want)
|
||||
}
|
||||
withUnused, err := llm.HashAssets([]llm.AssetHashPart{
|
||||
{FS: moduleFS, Path: "assets/prompts/prompt.yaml"},
|
||||
{FS: moduleFS, Path: "assets/prompts/instructions.md"},
|
||||
{FS: sharedFS, Path: "prompts/common-dnd-transcript-chunk.md"},
|
||||
{FS: sharedFS, Path: "prompts/common-dnd-system.md"},
|
||||
{FS: sharedFS, Path: "prompts/common-dnd-npc-registry.md"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("HashAssets() with unused asset error = %v, want nil", err)
|
||||
}
|
||||
if got == withUnused {
|
||||
t.Fatalf("Hash() included an unlisted shared asset")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSharedPromptDescriptorsReturnFreshCopies(t *testing.T) {
|
||||
first, err := resolveSharedPromptFiles([]string{"common-dnd-system.md"})
|
||||
if err != nil {
|
||||
t.Fatalf("resolveSharedPromptFiles() error = %v, want nil", err)
|
||||
}
|
||||
second, err := resolveSharedPromptFiles([]string{"common-dnd-system.md"})
|
||||
if err != nil {
|
||||
t.Fatalf("resolveSharedPromptFiles() second error = %v, want nil", err)
|
||||
}
|
||||
first[0].Name = "changed.md"
|
||||
first[0].Path = "changed.md"
|
||||
if reflect.DeepEqual(first, second) || second[0].Name != "common-dnd-system.md" || second[0].Path != "prompts/common-dnd-system.md" {
|
||||
t.Fatalf("resolveSharedPromptFiles() reused descriptor state: first=%#v second=%#v", first, second)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptAssetManifestMountsExternalSharedFiles(t *testing.T) {
|
||||
external := fstest.MapFS{"core/protocol.md": {Data: []byte("integer protocol")}}
|
||||
manifest := PromptAssetManifest{
|
||||
ModuleDir: "dnd.test",
|
||||
ModuleFiles: []promptfs.ModulePromptFile{
|
||||
{Name: "prompt.yaml", Path: "prompts/prompt.yaml"},
|
||||
},
|
||||
ExternalSharedFiles: []promptfs.SharedPromptFile{
|
||||
{Name: "protocol.md", FS: external, Path: "core/protocol.md"},
|
||||
},
|
||||
}
|
||||
fys, err := manifest.PromptFS(fstest.MapFS{
|
||||
"prompts/prompt.yaml": {Data: []byte("id: dnd.test")},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("PromptFS() error = %v, want nil", err)
|
||||
}
|
||||
content, err := fs.ReadFile(fys, "assets/prompts/dnd.test/sharedassets/protocol.md")
|
||||
if err != nil || string(content) != "integer protocol" {
|
||||
t.Fatalf("mounted external protocol = %q, %v", content, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptAssetManifestHashIncludesExternalSharedFiles(t *testing.T) {
|
||||
moduleFS := fstest.MapFS{"prompts/prompt.yaml": {Data: []byte("id: dnd.test")}}
|
||||
external := fstest.MapFS{"core/protocol.md": {Data: []byte("integer protocol")}}
|
||||
manifest := PromptAssetManifest{
|
||||
ModuleDir: "dnd.test",
|
||||
ModuleFiles: []promptfs.ModulePromptFile{
|
||||
{Name: "prompt.yaml", Path: "prompts/prompt.yaml"},
|
||||
},
|
||||
ExternalSharedFiles: []promptfs.SharedPromptFile{
|
||||
{Name: "protocol.md", FS: external, Path: "core/protocol.md"},
|
||||
},
|
||||
}
|
||||
got, err := manifest.Hash(moduleFS)
|
||||
if err != nil {
|
||||
t.Fatalf("Hash() error = %v, want nil", err)
|
||||
}
|
||||
want, err := llm.HashAssets([]llm.AssetHashPart{
|
||||
{FS: moduleFS, Path: "prompts/prompt.yaml"},
|
||||
{FS: external, Path: "core/protocol.md"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != want {
|
||||
t.Fatalf("Hash() = %q, want external-aware hash %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptAssetManifestRejectsInvalidExternalSharedFiles(t *testing.T) {
|
||||
moduleFS := fstest.MapFS{"prompts/prompt.yaml": {Data: []byte("id: dnd.test")}}
|
||||
tests := []struct {
|
||||
name string
|
||||
external []promptfs.SharedPromptFile
|
||||
shared []string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "missing",
|
||||
external: []promptfs.SharedPromptFile{
|
||||
{Name: "protocol.md", FS: fstest.MapFS{}, Path: "core/protocol.md"},
|
||||
},
|
||||
want: "read shared prompt asset core/protocol.md",
|
||||
},
|
||||
{
|
||||
name: "duplicate external destinations",
|
||||
external: []promptfs.SharedPromptFile{
|
||||
{Name: "protocol.md", FS: fstest.MapFS{"a.md": {Data: []byte("a")}}, Path: "a.md"},
|
||||
{Name: " protocol.md ", FS: fstest.MapFS{"b.md": {Data: []byte("b")}}, Path: "b.md"},
|
||||
},
|
||||
want: `duplicate sharedassets prompt destination "assets/prompts/dnd.test/sharedassets/protocol.md"`,
|
||||
},
|
||||
{
|
||||
name: "duplicate named and external destinations",
|
||||
shared: []string{"common-dnd-system.md"},
|
||||
external: []promptfs.SharedPromptFile{
|
||||
{Name: "common-dnd-system.md", FS: fstest.MapFS{"system.md": {Data: []byte("external")}}, Path: "system.md"},
|
||||
},
|
||||
want: `duplicate sharedassets prompt destination "assets/prompts/dnd.test/sharedassets/common-dnd-system.md"`,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
manifest := PromptAssetManifest{
|
||||
ModuleDir: "dnd.test",
|
||||
ModuleFiles: []promptfs.ModulePromptFile{
|
||||
{Name: "prompt.yaml", Path: "prompts/prompt.yaml"},
|
||||
},
|
||||
SharedFiles: test.shared,
|
||||
ExternalSharedFiles: test.external,
|
||||
}
|
||||
if _, err := manifest.PromptFS(moduleFS); err == nil || !strings.Contains(err.Error(), test.want) {
|
||||
t.Fatalf("PromptFS() error = %v, want %q", err, test.want)
|
||||
}
|
||||
if _, err := manifest.Hash(moduleFS); err == nil || !strings.Contains(err.Error(), test.want) {
|
||||
t.Fatalf("Hash() error = %v, want %q", err, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package shared
|
||||
|
||||
import "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
|
||||
// ModelCandidateFromResponse returns an owned candidate for the exact validated
|
||||
// structured response produced by the shared LLM boundary.
|
||||
func ModelCandidateFromResponse(response contracts.StructuredCompletionResponse) (*contracts.ModelCandidate, error) {
|
||||
return contracts.NewModelCandidate(response.Content, contracts.CorrectionProtocolSingleResponseV1)
|
||||
}
|
||||
@@ -27,16 +27,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.CombatTurnList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.CombatTurnList]) (contracts.ValidationResult, error) {
|
||||
if err := Validate(req.Source, req.Value); err != nil {
|
||||
|
||||
@@ -2,7 +2,6 @@ package invariants
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
@@ -102,9 +101,6 @@ func TestValidatorBoundsDiagnosticsAndRegistration(t *testing.T) {
|
||||
if err != nil || result.Approved || !utf8.ValidString(result.Message) || len([]byte(result.Message)) > 4096 || !strings.Contains(result.Message, "additional issue(s) omitted") {
|
||||
t.Fatalf("bounded result = %#v, %v; want bounded rejection", result, err)
|
||||
}
|
||||
if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Name != "policy" || got[0].Value != policy {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v, want policy fingerprint", got)
|
||||
}
|
||||
if spec := Spec(); spec.Key != Key || spec.ExecutionClass != contracts.ExecutionClassDeterministic {
|
||||
t.Fatalf("Spec() = %#v", spec)
|
||||
}
|
||||
@@ -115,9 +111,6 @@ func TestValidatorBoundsDiagnosticsAndRegistration(t *testing.T) {
|
||||
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
|
||||
t.Fatal("DecodeOptions() accepted unknown option")
|
||||
}
|
||||
if !reflect.DeepEqual(New(Options{}).CheckpointFingerprints(), []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}) {
|
||||
t.Fatal("policy fingerprint changed unexpectedly")
|
||||
}
|
||||
}
|
||||
|
||||
func normalizedList() dnd.CombatTurnList {
|
||||
|
||||
@@ -21,16 +21,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.CombatTurnList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.CombatTurnList]) (contracts.ValidationResult, error) {
|
||||
issues, corrections := assess(req.Value)
|
||||
|
||||
@@ -61,9 +61,6 @@ func TestValidatorBoundsAggregateDiagnostics(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSpecRegisterOptionsAndPolicy(t *testing.T) {
|
||||
if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Name != "policy" || got[0].Value != policy {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v, want shape policy", got)
|
||||
}
|
||||
if Spec().Key != Key || Spec().ExecutionClass != contracts.ExecutionClassDeterministic {
|
||||
t.Fatalf("Spec() = %#v, want deterministic shape validator", Spec())
|
||||
}
|
||||
|
||||
@@ -23,16 +23,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.CombatTurnList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.CombatTurnList]) (contracts.ValidationResult, error) {
|
||||
if req.Stage == string(pipeline.StageExtract) && req.Chunk == nil {
|
||||
|
||||
@@ -107,9 +107,6 @@ func TestValidatorBoundsAggregateDiagnostics(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSpecRegisterOptionsAndPolicy(t *testing.T) {
|
||||
if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Name != "policy" || got[0].Value != policy {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v, want source-reference policy", got)
|
||||
}
|
||||
if Spec().Key != Key || Spec().ExecutionClass != contracts.ExecutionClassDeterministic {
|
||||
t.Fatalf("Spec() = %#v, want deterministic source-reference validator", Spec())
|
||||
}
|
||||
|
||||
@@ -22,16 +22,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.CombatTurnList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.CombatTurnList]) (contracts.ValidationResult, error) {
|
||||
if combatshape.Validate(req.Value) != nil {
|
||||
|
||||
@@ -101,9 +101,6 @@ func TestValidatorIgnoresReferenceMaterialAndRegistersPolicy(t *testing.T) {
|
||||
if err != nil || !result.Approved || len(result.Diagnostics) != 1 {
|
||||
t.Fatalf("reference-only relatedness = %#v, %v; want advisory from transcript-only evidence", result, err)
|
||||
}
|
||||
if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Name != "policy" || got[0].Value != "dnd.combat_turns.validator.source_relatedness.v2" {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v, want relatedness policy", got)
|
||||
}
|
||||
if Spec().Key != Key || Spec().ExecutionClass != contracts.ExecutionClassDeterministic {
|
||||
t.Fatalf("Spec() = %#v, want deterministic relatedness validator", Spec())
|
||||
}
|
||||
|
||||
@@ -23,16 +23,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.EnemyEventList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.EnemyEventList]) (contracts.ValidationResult, error) {
|
||||
if enemyeventshape.Validate(req.Value) != nil {
|
||||
|
||||
@@ -58,9 +58,6 @@ func TestValidatorRegistersStrictOptionsAndPolicy(t *testing.T) {
|
||||
if Spec().Key != Key || Spec().ExecutionClass != contracts.ExecutionClassDeterministic {
|
||||
t.Fatalf("Spec() = %#v", Spec())
|
||||
}
|
||||
if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Name != "policy" || got[0].Value != policy {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v", got)
|
||||
}
|
||||
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil || !strings.Contains(err.Error(), "unknown option") {
|
||||
t.Fatalf("DecodeOptions() error = %v, want strict option rejection", err)
|
||||
}
|
||||
|
||||
@@ -30,8 +30,6 @@ type Validator struct {
|
||||
}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.EnemyEventList] = (*Validator)(nil)
|
||||
var _ contracts.ManifestMetadataProvider = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(_ Options, references ...contracts.ReferenceSet) (*Validator, error) {
|
||||
if len(references) > 1 {
|
||||
@@ -53,27 +51,6 @@ func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
|
||||
func (v *Validator) ManifestMetadata() map[string]any {
|
||||
if v == nil || v.npcResolver == nil {
|
||||
return nil
|
||||
}
|
||||
metadata := map[string]any{"policy": policy}
|
||||
if seeded := v.npcResolver.Seeded(); seeded.Bound() {
|
||||
metadata["npc_registry_digest"] = seeded.Digest()
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
if v == nil || v.npcResolver == nil {
|
||||
return nil
|
||||
}
|
||||
return []pipeline.CheckpointFingerprint{
|
||||
{Name: "policy", Value: policy},
|
||||
{Name: "npc_registry", Value: v.npcResolver.Seeded().ProjectionDigest()},
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.EnemyEventList]) (contracts.ValidationResult, error) {
|
||||
if enemyeventshape.Validate(req.Value) != nil {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
|
||||
@@ -2,7 +2,6 @@ package invariants
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -77,15 +76,7 @@ func TestValidatorDefersEarlierFailuresAndResolvesOperationRegistryWithoutMutati
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatorMetadataFingerprintsAndRegistrationAreSafe(t *testing.T) {
|
||||
references := registryReferences(t, "Ária")
|
||||
metadata, err := json.Marshal(newValidator(t, references).ManifestMetadata())
|
||||
if err != nil || strings.Contains(string(metadata), "Ária") {
|
||||
t.Fatalf("ManifestMetadata() = %s, %v", metadata, err)
|
||||
}
|
||||
if got := newValidator(t, references).CheckpointFingerprints(); len(got) != 2 || got[0].Value != policy || !strings.HasPrefix(got[1].Value, "sha256:") {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v", got)
|
||||
}
|
||||
func TestValidatorRegistrationAndOptions(t *testing.T) {
|
||||
registry := pipeline.NewValidatorRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -23,16 +23,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.EnemyEventList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.EnemyEventList]) (contracts.ValidationResult, error) {
|
||||
issues, corrections := assess(req.Value)
|
||||
|
||||
@@ -53,9 +53,6 @@ func TestValidatorBoundsDiagnosticsAndRegistersStrictly(t *testing.T) {
|
||||
if err != nil || result.Approved || !utf8.ValidString(result.Message) || len([]byte(result.Message)) > 4096 || !strings.Contains(result.Message, "additional issue(s) omitted") {
|
||||
t.Fatalf("Validate() = %#v, %v", result, err)
|
||||
}
|
||||
if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Name != "policy" || got[0].Value != policy {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v", got)
|
||||
}
|
||||
registry := pipeline.NewValidatorRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -24,16 +24,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.EnemyEventList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.EnemyEventList]) (contracts.ValidationResult, error) {
|
||||
if req.Stage == string(pipeline.StageExtract) && req.Chunk == nil {
|
||||
|
||||
@@ -69,9 +69,6 @@ func TestValidatorDefersShapeFailureAndRegistersStrictly(t *testing.T) {
|
||||
if Spec().Key != Key || Spec().ExecutionClass != contracts.ExecutionClassDeterministic {
|
||||
t.Fatalf("Spec() = %#v", Spec())
|
||||
}
|
||||
if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Value != policy {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v", got)
|
||||
}
|
||||
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
|
||||
t.Fatal("DecodeOptions() accepted unknown option")
|
||||
}
|
||||
|
||||
@@ -23,16 +23,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.EnemyEventList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.EnemyEventList]) (contracts.ValidationResult, error) {
|
||||
if enemyeventshape.Validate(req.Value) != nil {
|
||||
|
||||
@@ -49,9 +49,6 @@ func TestValidatorDefersMalformedValuesAndBoundsAdvisories(t *testing.T) {
|
||||
if Spec().Key != Key || Spec().ExecutionClass != contracts.ExecutionClassDeterministic {
|
||||
t.Fatalf("Spec() = %#v", Spec())
|
||||
}
|
||||
if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Value != policy {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v", got)
|
||||
}
|
||||
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
|
||||
t.Fatal("DecodeOptions() accepted unknown option")
|
||||
}
|
||||
|
||||
@@ -25,16 +25,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.ItemOccurrenceList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.ItemOccurrenceList]) (contracts.ValidationResult, error) {
|
||||
if err := Validate(req.Source, req.Value); err != nil {
|
||||
|
||||
@@ -2,7 +2,6 @@ package invariants
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
@@ -97,9 +96,6 @@ func TestValidatorContractAndBoundedDiagnostics(t *testing.T) {
|
||||
if err != nil || result.Approved || !utf8.ValidString(result.Message) || len([]byte(result.Message)) > 4096 || !strings.Contains(result.Message, "additional issue(s) omitted") {
|
||||
t.Fatalf("bounded result = %#v, %v", result, err)
|
||||
}
|
||||
if got := New(Options{}).CheckpointFingerprints(); !reflect.DeepEqual(got, []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}) {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v", got)
|
||||
}
|
||||
if spec := Spec(); spec.Key != Key || spec.ExecutionClass != contracts.ExecutionClassDeterministic {
|
||||
t.Fatalf("Spec() = %#v", spec)
|
||||
}
|
||||
|
||||
@@ -23,8 +23,6 @@ type Options struct{}
|
||||
type Validator struct{ itemResolver *itemregistry.Resolver }
|
||||
|
||||
var _ contracts.TypedValidator[dnd.ItemOccurrenceList] = (*Validator)(nil)
|
||||
var _ contracts.ManifestMetadataProvider = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(_ Options, references ...contracts.ReferenceSet) (*Validator, error) {
|
||||
if len(references) > 1 {
|
||||
@@ -45,24 +43,6 @@ func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) ManifestMetadata() map[string]any {
|
||||
if v == nil || v.itemResolver == nil {
|
||||
return nil
|
||||
}
|
||||
metadata := map[string]any{"policy": policy}
|
||||
seeded := v.itemResolver.Seeded()
|
||||
if seeded.Bound() {
|
||||
metadata["item_registry_digest"] = seeded.Digest()
|
||||
metadata["item_count"] = seeded.Count()
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
if v == nil || v.itemResolver == nil {
|
||||
return nil
|
||||
}
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}, {Name: "item_registry", Value: v.itemResolver.Seeded().ProjectionDigest()}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.ItemOccurrenceList]) (contracts.ValidationResult, error) {
|
||||
if occurrenceshape.Validate(req.Value) != nil {
|
||||
|
||||
@@ -23,16 +23,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.ItemOccurrenceList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.ItemOccurrenceList]) (contracts.ValidationResult, error) {
|
||||
assessment := assess(req.Value)
|
||||
|
||||
@@ -159,9 +159,6 @@ func TestValidatorAggregatesBoundedIndexedDiagnosticsAndRegistration(t *testing.
|
||||
if value.Occurrences[0].Name != "Item 0" {
|
||||
t.Fatal("Validate() mutated input")
|
||||
}
|
||||
if got := New(Options{}).CheckpointFingerprints(); !reflect.DeepEqual(got, []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}) {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v", got)
|
||||
}
|
||||
registry := pipeline.NewValidatorRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -24,16 +24,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.ItemOccurrenceList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.ItemOccurrenceList]) (contracts.ValidationResult, error) {
|
||||
if req.Stage == string(pipeline.StageExtract) && req.Chunk == nil {
|
||||
|
||||
@@ -93,9 +93,6 @@ func TestValidatorBoundsDiagnosticsAndRegistration(t *testing.T) {
|
||||
if _, err := New(Options{}).Validate(context.Background(), request(document(), before)); err != nil || !reflect.DeepEqual(before, copy) {
|
||||
t.Fatal("Validate() mutated input")
|
||||
}
|
||||
if got := New(Options{}).CheckpointFingerprints(); !reflect.DeepEqual(got, []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}) {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v", got)
|
||||
}
|
||||
registry := pipeline.NewValidatorRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -23,16 +23,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.ItemOccurrenceList] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}
|
||||
}
|
||||
|
||||
// Validate performs advisory source grounding only. Shape and range failures
|
||||
// are intentionally left to their blocking owners.
|
||||
|
||||
@@ -2,7 +2,6 @@ package sourcerelatedness
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -59,9 +58,6 @@ func TestValidatorBoundsAdvisoriesAndRegistersPolicy(t *testing.T) {
|
||||
if err != nil || !result.Approved || len(result.Diagnostics) != 1 || result.Diagnostics[0].OccurrenceCount != count || len(result.Diagnostics[0].Samples) != contracts.MaxDiagnosticSamples {
|
||||
t.Fatalf("Validate() = %#v, %v", result, err)
|
||||
}
|
||||
if got := New(Options{}).CheckpointFingerprints(); !reflect.DeepEqual(got, []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}) {
|
||||
t.Fatalf("CheckpointFingerprints() = %#v", got)
|
||||
}
|
||||
registry := pipeline.NewValidatorRegistry()
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatalf("Register() error = %v", err)
|
||||
|
||||
@@ -24,16 +24,12 @@ type Options struct{}
|
||||
type Validator struct{}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.ItemRegistry] = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
func New(Options) *Validator { return &Validator{} }
|
||||
func (v *Validator) Name() string { return Key }
|
||||
func (v *Validator) ExecutionClass() contracts.ExecutionClass {
|
||||
return contracts.ExecutionClassDeterministic
|
||||
}
|
||||
func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
return []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}, {Name: "correction_policy", Value: correctionPolicy}}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.ItemRegistry]) (contracts.ValidationResult, error) {
|
||||
if err := itemshape.Validate(req.Value); err != nil {
|
||||
|
||||
@@ -2,6 +2,7 @@ package identity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -31,12 +32,9 @@ func TestValidatorRegistersPolicy(t *testing.T) {
|
||||
if err := Register(registry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, ok := registry.Spec(Key); !ok || got != Spec() || got.ExecutionClass != contracts.ExecutionClassDeterministic {
|
||||
if got, ok := registry.Spec(Key); !ok || !reflect.DeepEqual(got, Spec()) || got.ExecutionClass != contracts.ExecutionClassDeterministic {
|
||||
t.Fatalf("registered spec = %#v, present = %t", got, ok)
|
||||
}
|
||||
if got := New(Options{}).CheckpointFingerprints(); len(got) != 2 || got[0].Value != domainidentity.Policy || got[1].Name != "correction_policy" || got[1].Value != correctionPolicy {
|
||||
t.Fatalf("fingerprints = %#v", got)
|
||||
}
|
||||
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
|
||||
t.Fatal("DecodeOptions accepted unknown options")
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user