Reuse supplied artifacts during incremental analysis
This commit is contained in:
@@ -110,19 +110,36 @@ func (state *analyzeExecutionState) acceptArtifact(
|
||||
|
||||
func (state *analyzeExecutionState) checkpointStartingCollection(ctx context.Context) error {
|
||||
m := state.execution.Manifest
|
||||
retired := make([]string, 0)
|
||||
if m != nil && m.SuppliedProducts != nil {
|
||||
for _, item := range state.workPlan.ExecutionOrder {
|
||||
source := artifacts.ConfiguredArtifactSourceID(item.Key)
|
||||
if _, exists := m.SuppliedProducts.Products[source]; exists {
|
||||
retired = append(retired, source)
|
||||
}
|
||||
}
|
||||
}
|
||||
if m != nil {
|
||||
if record := m.Stages["analyze"]; record != nil &&
|
||||
record.AnalyzeStateVersion == manifest.AnalyzeStateContractVersion &&
|
||||
reflect.DeepEqual(record.AnalyzeArtifacts, state.records) {
|
||||
reflect.DeepEqual(record.AnalyzeArtifacts, state.records) && len(retired) == 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return state.checkpointCollection(ctx, state.records)
|
||||
return state.checkpointCollectionRetiring(ctx, state.records, retired)
|
||||
}
|
||||
|
||||
func (state *analyzeExecutionState) checkpointCollection(
|
||||
ctx context.Context,
|
||||
records map[string]manifest.AnalyzeArtifactRecord,
|
||||
) error {
|
||||
return state.checkpointCollectionRetiring(ctx, records, nil)
|
||||
}
|
||||
|
||||
func (state *analyzeExecutionState) checkpointCollectionRetiring(
|
||||
ctx context.Context,
|
||||
records map[string]manifest.AnalyzeArtifactRecord,
|
||||
retired []string,
|
||||
) error {
|
||||
m := state.execution.Manifest
|
||||
if m == nil {
|
||||
@@ -143,12 +160,13 @@ func (state *analyzeExecutionState) checkpointCollection(
|
||||
m.Campaign,
|
||||
m.SessionID,
|
||||
)
|
||||
if err := checkpointStageCollection(
|
||||
if err := checkpointStageCollectionRetiring(
|
||||
ctx,
|
||||
state.execution.Env.ManifestStore,
|
||||
manifestPath,
|
||||
m,
|
||||
"analyze",
|
||||
retired,
|
||||
func(record *manifest.StageRecord) error {
|
||||
record.AnalyzeStateVersion = manifest.AnalyzeStateContractVersion
|
||||
record.AnalyzeArtifacts = manifest.CloneAnalyzeArtifactCollection(records)
|
||||
|
||||
@@ -112,6 +112,10 @@ func computeAnalyzeFingerprints(
|
||||
result := analyzeFingerprintSet{Ordered: make([]analyzeFingerprintCandidate, 0, len(order))}
|
||||
for _, key := range order {
|
||||
candidate := analyzeFingerprintCandidate{Key: key}
|
||||
if entry, ok := execution.Catalog.Lookup(artifacts.ConfiguredArtifactSourceID(key)); ok && entry.Available && entry.Provenance == artifacts.ArtifactProvenanceSupplied {
|
||||
result.Ordered = append(result.Ordered, candidate)
|
||||
continue
|
||||
}
|
||||
candidate.Fingerprint, candidate.Inputs, candidate.DependencyOutputs, candidate.Err =
|
||||
computeAnalyzeArtifactFingerprint(key, scriptoriumCfg, execution)
|
||||
result.Ordered = append(result.Ordered, candidate)
|
||||
|
||||
@@ -109,7 +109,7 @@ func planAnalyzeWork(
|
||||
}
|
||||
_, explicit := targetSet[key]
|
||||
forced := force && explicit
|
||||
needsWork := forced || state.Reason != analyzeReconciliationCurrent || dependencyWork
|
||||
needsWork := forced || (state.Reason != analyzeReconciliationCurrent && state.Reason != analyzeReconciliationSupplied) || (dependencyWork && state.Reason != analyzeReconciliationSupplied)
|
||||
item := analyzePlanItem{
|
||||
Key: key, Reason: state.Reason, Forced: forced,
|
||||
Dependencies: append([]string(nil), dependencies...),
|
||||
|
||||
@@ -15,6 +15,7 @@ type analyzeReconciliationReason string
|
||||
|
||||
const (
|
||||
analyzeReconciliationCurrent analyzeReconciliationReason = "current"
|
||||
analyzeReconciliationSupplied analyzeReconciliationReason = "supplied"
|
||||
analyzeReconciliationStale analyzeReconciliationReason = "stale"
|
||||
analyzeReconciliationMissing analyzeReconciliationReason = "missing"
|
||||
analyzeReconciliationFailed analyzeReconciliationReason = "failed"
|
||||
@@ -97,6 +98,18 @@ func reconcileAnalyzeArtifact(
|
||||
result := analyzeArtifactReconciliation{
|
||||
Key: candidate.Key, ExpectedFingerprint: candidate.Fingerprint,
|
||||
}
|
||||
if entry, ok := execution.Catalog.Lookup(artifacts.ConfiguredArtifactSourceID(candidate.Key)); ok && entry.Available && entry.Provenance == artifacts.ArtifactProvenanceSupplied {
|
||||
result.Reason = analyzeReconciliationSupplied
|
||||
result.Detail = "validated supplied product"
|
||||
return result
|
||||
}
|
||||
if execution.Manifest != nil && execution.Manifest.SuppliedProducts != nil {
|
||||
if _, exists := execution.Manifest.SuppliedProducts.Products[artifacts.ConfiguredArtifactSourceID(candidate.Key)]; exists {
|
||||
result.Reason = analyzeReconciliationMissing
|
||||
result.Detail = "supplied product is unavailable or invalid"
|
||||
return result
|
||||
}
|
||||
}
|
||||
if stageRecord == nil {
|
||||
result.Reason = analyzeReconciliationMissing
|
||||
result.Detail = "analyze manifest record is absent"
|
||||
|
||||
95
internal/stage/analyze_supplied_test.go
Normal file
95
internal/stage/analyze_supplied_test.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
|
||||
func suppliedAnalyzeFixture(t *testing.T) (*Env, *manifest.Manifest) {
|
||||
t.Helper()
|
||||
env, m, _ := setupAnalyzeEnv(t)
|
||||
paths := sessionPathsForEnv(env, m.SessionID)
|
||||
path := filepath.Join(paths.ArtifactsDir, "session_recap.md")
|
||||
body := "supplied recap\n"
|
||||
writeAnalyzeFile(t, path, body)
|
||||
checksum, err := artifacts.SHA256File(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.SuppliedProducts = &manifest.SuppliedProductCollection{Version: manifest.SuppliedProductVersion, Products: map[string]manifest.SuppliedProduct{
|
||||
artifacts.ConfiguredArtifactSourceID("session_recap"): {Path: "artifacts/session_recap.md", Checksum: checksum, Size: int64(len(body)), Contract: &artifactmodel.ContractMetadata{MediaType: "text/markdown", SchemaID: "test", SchemaVersion: "1"}},
|
||||
}}
|
||||
return env, m
|
||||
}
|
||||
|
||||
func TestAnalyzeSuppliedPrerequisiteAndNewTarget(t *testing.T) {
|
||||
env, m := suppliedAnalyzeFixture(t)
|
||||
addAnalyzeDependentArtifact(env)
|
||||
parent := env.Config.ArtifactCatalog.Artifacts["session_recap"]
|
||||
parent.Enabled = false
|
||||
parent.PromptID = "changed-historical-prompt"
|
||||
env.Config.ArtifactCatalog.Artifacts["session_recap"] = parent
|
||||
env.SelectedArtifactKeys = []string{"player_handout"}
|
||||
env.Force = true
|
||||
runner := &orderedScriptoriumRunner{RunBody: "new handout\n"}
|
||||
env.Scriptorium = runner
|
||||
_, err := (analyzeStage{}).Run(context.Background(), env, m)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(runner.Calls) != 1 || runner.Calls[0] != "run" {
|
||||
t.Fatalf("calls %v", runner.Calls)
|
||||
}
|
||||
if _, ok := m.SuppliedProducts.Products[artifacts.ConfiguredArtifactSourceID("session_recap")]; !ok {
|
||||
t.Fatal("lost supplied prerequisite")
|
||||
}
|
||||
if m.Stages["analyze"].AnalyzeArtifacts["player_handout"].Status != manifest.AnalyzeArtifactCurrent {
|
||||
t.Fatal("missing local checkpoint")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnalyzeForcedSuppliedTargetBecomesLocalCheckpoint(t *testing.T) {
|
||||
env, m := suppliedAnalyzeFixture(t)
|
||||
env.SelectedArtifactKeys = []string{"session_recap"}
|
||||
env.Force = true
|
||||
if _, err := (analyzeStage{}).Run(context.Background(), env, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := m.SuppliedProducts.Products[artifacts.ConfiguredArtifactSourceID("session_recap")]; ok {
|
||||
t.Fatal("supplied authority retained after replacement")
|
||||
}
|
||||
if m.Stages["analyze"].AnalyzeArtifacts["session_recap"].Status != manifest.AnalyzeArtifactCurrent {
|
||||
t.Fatal("replacement has no local checkpoint")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnalyzeForceRetiresSuppliedBeforeAttempt(t *testing.T) {
|
||||
env, m := suppliedAnalyzeFixture(t)
|
||||
env.SelectedArtifactKeys = []string{"session_recap"}
|
||||
env.Force = true
|
||||
parent := env.Config.ArtifactCatalog.Artifacts["session_recap"]
|
||||
parent.Inputs = map[string]config.ScriptoriumInputConfig{"missing": {Source: artifacts.ArtifactTranscriptFinal, Required: true}}
|
||||
env.Config.ArtifactCatalog.Artifacts["session_recap"] = parent
|
||||
// A missing generation input fails after the retirement checkpoint.
|
||||
_, err := (analyzeStage{}).Run(context.Background(), env, m)
|
||||
if err == nil {
|
||||
t.Fatal("expected missing generation input")
|
||||
}
|
||||
if _, ok := m.SuppliedProducts.Products[artifacts.ConfiguredArtifactSourceID("session_recap")]; ok {
|
||||
t.Fatal("supplied record revived after failed attempt")
|
||||
}
|
||||
path := artifacts.SessionManifestPathForCampaign(env.Config.Pipeline.Workspace.Root, m.Campaign, m.SessionID)
|
||||
saved, err := env.ManifestStore.Load(context.Background(), path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := saved.SuppliedProducts.Products[artifacts.ConfiguredArtifactSourceID("session_recap")]; ok {
|
||||
t.Fatal("retirement was not persisted")
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,18 @@ func checkpointStageCollection(
|
||||
live *manifest.Manifest,
|
||||
stageName string,
|
||||
update func(*manifest.StageRecord) error,
|
||||
) error {
|
||||
return checkpointStageCollectionRetiring(ctx, store, path, live, stageName, nil, update)
|
||||
}
|
||||
|
||||
func checkpointStageCollectionRetiring(
|
||||
ctx context.Context,
|
||||
store manifest.Store,
|
||||
path string,
|
||||
live *manifest.Manifest,
|
||||
stageName string,
|
||||
retired []string,
|
||||
update func(*manifest.StageRecord) error,
|
||||
) error {
|
||||
if live == nil {
|
||||
return fmt.Errorf("session manifest is required")
|
||||
@@ -29,6 +41,11 @@ func checkpointStageCollection(
|
||||
}
|
||||
|
||||
candidate := cloneManifestForCheckpoint(live)
|
||||
if candidate.SuppliedProducts != nil {
|
||||
for _, source := range retired {
|
||||
delete(candidate.SuppliedProducts.Products, source)
|
||||
}
|
||||
}
|
||||
if candidate.Stages == nil {
|
||||
candidate.Stages = make(map[string]*manifest.StageRecord)
|
||||
}
|
||||
@@ -63,6 +80,9 @@ func checkpointStageCollection(
|
||||
live.Stages = map[string]*manifest.StageRecord{stageName: savedRecord}
|
||||
}
|
||||
live.UpdatedAt = candidate.UpdatedAt
|
||||
if len(retired) != 0 {
|
||||
live.SuppliedProducts = manifest.CloneSuppliedProducts(candidate.SuppliedProducts)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user