Verify prerequisite history through preparation and analysis
This commit is contained in:
@@ -142,7 +142,7 @@ Verify that force/publication callers still receive the original target set.
|
|||||||
|
|
||||||
## Stage 3: Protect the real preparation-to-analysis workflow
|
## Stage 3: Protect the real preparation-to-analysis workflow
|
||||||
|
|
||||||
Status: Pending
|
Status: Complete
|
||||||
|
|
||||||
Use `prepare_effective_artifacts_test.go`, `prepare_previous_test.go`,
|
Use `prepare_effective_artifacts_test.go`, `prepare_previous_test.go`,
|
||||||
`prepare_test.go`, and `analyze_incremental_execution_test.go` as fixture
|
`prepare_test.go`, and `analyze_incremental_execution_test.go` as fixture
|
||||||
|
|||||||
@@ -645,3 +645,41 @@ func semanticFingerprint(version int, seed string) manifest.SemanticConfigFinger
|
|||||||
func fingerprintPointer(value manifest.SemanticConfigFingerprint) *manifest.SemanticConfigFingerprint {
|
func fingerprintPointer(value manifest.SemanticConfigFingerprint) *manifest.SemanticConfigFingerprint {
|
||||||
return &value
|
return &value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPrepareSemanticResumeTracksPrerequisiteHistory(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
change bool
|
||||||
|
wantRuns int
|
||||||
|
}{{"unchanged", false, 0}, {"new prerequisite history", true, 1}} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
cfg := testConfig(t)
|
||||||
|
definitions := map[string]config.ScriptoriumArtifactConfig{"child": {Enabled: true, DependsOn: []string{"parent"}, PromptID: "child", OutputPath: "artifacts/child.md"}, "parent": {PromptID: "parent", OutputPath: "artifacts/parent.md"}}
|
||||||
|
cfg.Pipeline.Scriptorium = &config.ScriptoriumConfig{Artifacts: definitions}
|
||||||
|
cfg.ArtifactCatalog = &config.ArtifactCatalog{Artifacts: definitions}
|
||||||
|
provider := canonicalSemanticProvider(t, "prepare")
|
||||||
|
fingerprint, err := provider.SemanticConfigFingerprint(&stage.Env{Config: cfg})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
seed := manifest.New(cfg.Session.SessionID, time.Now().UTC())
|
||||||
|
seed.MarkStageSucceeded("prepare", time.Now().UTC(), nil)
|
||||||
|
seed.Stages["prepare"].SemanticConfig = &fingerprint
|
||||||
|
if err := (&manifest.LocalStore{}).Save(context.Background(), manifestPathFor(cfg), seed); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if tc.change {
|
||||||
|
parent := definitions["parent"]
|
||||||
|
parent.Inputs = map[string]config.ScriptoriumInputConfig{"history": {Source: "narratio.previous_session.artifact.history", Required: true}}
|
||||||
|
definitions["parent"] = parent
|
||||||
|
}
|
||||||
|
runs := 0
|
||||||
|
if _, err := executeStages(context.Background(), cfg, []stage.Stage{semanticContractRunStub{name: "prepare", provider: provider, runs: &runs}}, RunOptions{}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if runs != tc.wantRuns {
|
||||||
|
t.Fatalf("runs = %d, want %d", runs, tc.wantRuns)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
119
internal/stage/prepare_analyze_history_test.go
Normal file
119
internal/stage/prepare_analyze_history_test.go
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package stage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPrepareAndAnalyzeHydratesPrerequisiteHistory(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
explicit, required, present, inferred bool
|
||||||
|
}{
|
||||||
|
{"enabled required explicit dependency", false, true, true, false},
|
||||||
|
{"selected optional inferred dependency", true, false, true, true},
|
||||||
|
{"selected optional missing", true, false, false, false},
|
||||||
|
{"enabled required missing", false, true, false, true},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
env, paths, store := previousHydrationFixture(t)
|
||||||
|
env.Config.Session.Inputs.AudioDir = ""
|
||||||
|
audio := filepath.Join(filepath.Dir(env.Config.SessionPath), "audio.flac")
|
||||||
|
writeFile(t, audio, "audio")
|
||||||
|
env.Config.Session.Inputs.AudioFiles = []string{audio}
|
||||||
|
parent := config.ScriptoriumArtifactConfig{PromptID: "parent", OutputPath: "artifacts/session_recap.md", Inputs: map[string]config.ScriptoriumInputConfig{"history": {Source: "narratio.previous_session.artifact.session_recap", Required: tc.required}}}
|
||||||
|
child := config.ScriptoriumArtifactConfig{Enabled: true, PromptID: "child", OutputPath: "artifacts/child.md", DependsOn: []string{"session_recap"}}
|
||||||
|
if tc.inferred {
|
||||||
|
child.DependsOn = nil
|
||||||
|
child.Inputs = map[string]config.ScriptoriumInputConfig{"recap": {Source: "narratio.artifact.session_recap", Required: true}}
|
||||||
|
}
|
||||||
|
unrelated := config.ScriptoriumArtifactConfig{Enabled: tc.explicit, PromptID: "other", OutputPath: "artifacts/other.md", Inputs: map[string]config.ScriptoriumInputConfig{"history": {Source: "narratio.previous_session.artifact.other", Required: true}}}
|
||||||
|
env.Config.Pipeline.Scriptorium.Artifacts = map[string]config.ScriptoriumArtifactConfig{"session_recap": parent, "child": child, "other": unrelated}
|
||||||
|
catalog, err := config.DeriveArtifactCatalog(env.Config.Pipeline, env.Config.Party)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
env.Config.ArtifactCatalog = catalog
|
||||||
|
if tc.explicit {
|
||||||
|
env.SelectedArtifactKeys = []string{"child"}
|
||||||
|
env.EffectiveArtifacts, err = artifacts.ResolveEffectiveArtifactSet(artifacts.ConfiguredArtifactDefinitions(env.Config.ConcreteArtifacts()), env.SelectedArtifactKeys)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
seed := seedPreviousCurrentState(t, env, store, previousStateSeedOptions{includeRunPointerObject: true, includeManifestObject: true, includeArtifactObject: tc.present, artifactBody: "history bytes\n"})
|
||||||
|
capture := &preparePreviousCaptureStore{delegate: store}
|
||||||
|
env.ObjectStore = capture
|
||||||
|
fake := &scriptorium.FakeRunner{}
|
||||||
|
env.Scriptorium = fake
|
||||||
|
env.ManifestStore = &manifest.LocalStore{}
|
||||||
|
m := manifest.New(env.Config.Session.SessionID, time.Now().UTC())
|
||||||
|
m.Campaign = env.Config.Session.Campaign
|
||||||
|
m.RunID = "history-test"
|
||||||
|
m.LocalWorkDir = paths.Root
|
||||||
|
result, err := (prepareStage{}).Run(context.Background(), env, m)
|
||||||
|
if tc.required && !tc.present {
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("missing required history succeeded")
|
||||||
|
}
|
||||||
|
if len(fake.RunRequests) != 0 {
|
||||||
|
t.Fatal("analysis ran after failed preparation")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if tc.present {
|
||||||
|
input := findInputByKind(m.Inputs, preparePreviousInputKindArtifact)
|
||||||
|
if input == nil || input.Source != "narratio.previous_session.artifact.session_recap" || input.Checksum != preparePreviousSHA256([]byte("history bytes\n")) {
|
||||||
|
t.Fatalf("history record = %#v", input)
|
||||||
|
}
|
||||||
|
if !containsString(capture.accessKeys, seed.ArtifactKey) {
|
||||||
|
t.Fatalf("history not fetched: %v", capture.accessKeys)
|
||||||
|
}
|
||||||
|
} else if result.Metadata["previous_artifacts_missing_optional_count"] != 1 {
|
||||||
|
t.Fatalf("missing optional metadata = %#v", result.Metadata)
|
||||||
|
}
|
||||||
|
trimmed := filepath.Join(paths.TranscriptsDir, "final.trimmed.json")
|
||||||
|
writeFile(t, trimmed, `{"segments":[]}`)
|
||||||
|
checksum, err := artifacts.SHA256File(trimmed)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
m.MarkStageSucceeded("trim", time.Now().UTC(), []manifest.ArtifactRecord{{Kind: artifacts.TranscriptOutputKindFinalTrimmed, SourceID: artifacts.ArtifactTranscriptFinalTrimmed, LocalPath: trimmed, ProducerRunID: "trim", Checksum: checksum}})
|
||||||
|
m.MarkStageRunning("analyze", time.Now().UTC())
|
||||||
|
if _, err := (analyzeStage{}).Run(context.Background(), env, m); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(fake.RunRequests) != 2 || !reflect.DeepEqual([]string{fake.RunRequests[0].PromptID, fake.RunRequests[1].PromptID}, []string{"parent", "child"}) {
|
||||||
|
t.Fatalf("requests = %#v", fake.RunRequests)
|
||||||
|
}
|
||||||
|
for _, key := range capture.accessKeys {
|
||||||
|
if strings.Contains(key, "artifacts/other") {
|
||||||
|
t.Fatalf("unrelated history fetched: %q", key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
path, exists := fake.RunRequests[0].InputPaths["history"]
|
||||||
|
if exists != tc.present {
|
||||||
|
t.Fatalf("history binding = %q, present = %v", path, exists)
|
||||||
|
}
|
||||||
|
if tc.present {
|
||||||
|
bytes, err := os.ReadFile(path)
|
||||||
|
if err != nil || string(bytes) != "history bytes\n" {
|
||||||
|
t.Fatalf("history = %q, error = %v", bytes, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user