2026-11-20 04:42:23 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2026-11-20 06:50:56 +00:00
|
|
|
"errors"
|
2026-11-20 04:42:23 +00:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
2026-11-20 06:50:56 +00:00
|
|
|
"go.maximumdirect.net/commandkit"
|
2026-11-20 04:42:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestSelectedPartyCatalogDrivesBoundedAnalysis(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
pipelinePath := filepath.Join(dir, "pipeline.yml")
|
2026-11-20 06:50:56 +00:00
|
|
|
const secret = "synthetic-family-workflow-credential"
|
|
|
|
|
t.Setenv("NARRATIO_WORKFLOW_TEST_KEY", secret)
|
|
|
|
|
mustWriteTestFile(t, pipelinePath, "config:\n imports:\n - {path: family.yml, required: true}\n")
|
|
|
|
|
mustWriteTestFile(t, filepath.Join(dir, "overlays", "testing.yml"), "scriptorium:\n artifact_families:\n character_note:\n prompt_id: note-testing\n")
|
|
|
|
|
mustWriteTestFile(t, filepath.Join(dir, "family.yml"), `whisperx:
|
2026-11-20 05:02:46 +00:00
|
|
|
transcribe_url: https://transcription.example.test/transcribe
|
|
|
|
|
workspace:
|
2026-11-20 04:42:23 +00:00
|
|
|
root: `+filepath.Join(dir, "work")+`
|
|
|
|
|
scriptorium:
|
2026-11-20 06:50:56 +00:00
|
|
|
credentials: [NARRATIO_WORKFLOW_TEST_KEY]
|
2026-11-20 04:42:23 +00:00
|
|
|
artifact_families:
|
|
|
|
|
character_meta:
|
|
|
|
|
enabled: false
|
|
|
|
|
for_each: party.characters
|
|
|
|
|
prompt_id: meta
|
|
|
|
|
output_path_pattern: artifacts/{character_id}/meta.md
|
|
|
|
|
inputs:
|
|
|
|
|
players: {source: narratio.input.players, required: true}
|
|
|
|
|
character_note:
|
|
|
|
|
enabled: false
|
|
|
|
|
for_each: party.characters
|
|
|
|
|
prompt_id: note
|
|
|
|
|
output_path_pattern: artifacts/{character_id}/note.md
|
|
|
|
|
inputs:
|
|
|
|
|
meta: {source: narratio.member_artifact.character_meta, required: true}
|
|
|
|
|
member_vars:
|
|
|
|
|
character_name: character.name
|
|
|
|
|
`)
|
|
|
|
|
campaignPath := filepath.Join(dir, "campaign.yml")
|
|
|
|
|
mustWriteTestFile(t, campaignPath, "campaign_id: campaign\ninputs: {speakers_file: speakers.yml, autocorrect_file: autocorrect.yml, glossary_file: glossary.yml, party_file: default-party.yml}\n")
|
|
|
|
|
partyPath := filepath.Join(dir, "selected-party.yml")
|
|
|
|
|
mustWriteTestFile(t, partyPath, "schema_version: narratio.party.v1\ncharacters:\n ivy:\n player: {name: Selected Player}\n character: {name: Ivy, classes: [{name: ranger}]}\n")
|
|
|
|
|
sessionPath := filepath.Join(dir, "session.yml")
|
|
|
|
|
mustWriteTestFile(t, sessionPath, "session_id: session\ninputs: {audio_dir: audio}\n")
|
2026-11-20 06:50:56 +00:00
|
|
|
overlay := "testing"
|
|
|
|
|
pipeline, err := config.LoadPipelineWithOptions(pipelinePath, config.PipelineLoadOptions{Overlay: &overlay})
|
2026-11-20 04:42:23 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
campaign, err := config.LoadCampaign(campaignPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
loaded, err := config.LoadPipelineCampaignWithPartyFile(pipelinePath, pipeline, campaignPath, campaign, &partyPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
cfg, err := config.LoadSessionWithPipelineCampaignOptions(loaded, sessionPath, config.SessionLoadOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
before, err := json.Marshal(cfg.Pipeline)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-20 06:50:56 +00:00
|
|
|
catalogSelection := []string{"--config", pipelinePath, "--overlay", overlay, "--campaign-file", campaignPath, "--party-file", partyPath, "--secrets-dir", t.TempDir(), "--format", "json"}
|
|
|
|
|
common := append(append([]string{}, catalogSelection...), "--session-file", sessionPath)
|
|
|
|
|
for _, scope := range []string{"app", "session", "party"} {
|
|
|
|
|
output := nativeInspectionForTest(t, append([]string{"config", "show", "--scope", scope}, common...))
|
|
|
|
|
if strings.Contains(output, secret) {
|
|
|
|
|
t.Fatal("native inspection exposed credentials")
|
|
|
|
|
}
|
|
|
|
|
if scope == "app" && (!strings.Contains(output, "artifact_families") || strings.Contains(output, "character_meta_ivy")) {
|
|
|
|
|
t.Fatal("native app inspection substituted derived configuration")
|
|
|
|
|
}
|
|
|
|
|
if scope == "session" {
|
|
|
|
|
var effective map[string]any
|
|
|
|
|
if err := json.Unmarshal([]byte(output), &effective); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if effective["campaign"] != "campaign" || effective["session_id"] != "session" {
|
|
|
|
|
t.Fatalf("inspection lost resolved session defaults: %#v", effective)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if scope == "party" && !strings.Contains(output, "Selected Player") {
|
|
|
|
|
t.Fatal("party inspection used a different roster")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
diff := nativeInspectionForTest(t, []string{"config", "diff", "--config", pipelinePath, "--against-config", pipelinePath, "--against-overlay", overlay, "--details", "--format", "json"})
|
|
|
|
|
if !strings.Contains(diff, "/scriptorium/artifact_families/character_note/prompt_id") || strings.Contains(diff, secret) {
|
|
|
|
|
t.Fatalf("unexpected independent comparison: %s", diff)
|
|
|
|
|
}
|
|
|
|
|
catalog := nativeInspectionForTest(t, append([]string{"artifacts", "list"}, catalogSelection...))
|
|
|
|
|
if !strings.Contains(catalog, "character_note_ivy") {
|
|
|
|
|
t.Fatal("catalog omitted selected member")
|
|
|
|
|
}
|
|
|
|
|
selected := []string{"--stage", "analyze", "--artifacts", "character_note"}
|
|
|
|
|
planned := nativeInspectionForTest(t, append(append([]string{"session", "plan", "session"}, common...), "--stage", "prepare"))
|
|
|
|
|
if !strings.Contains(planned, "prepare") || strings.Contains(planned, secret) {
|
|
|
|
|
t.Fatal("initial plan lost selection or exposed credentials")
|
|
|
|
|
}
|
|
|
|
|
if _, err := os.Stat(cfg.Pipeline.Workspace.Root); !os.IsNotExist(err) {
|
|
|
|
|
t.Fatalf("inspection or planning created workspace: %v", err)
|
|
|
|
|
}
|
2026-11-20 04:42:23 +00:00
|
|
|
plan, err := BuildBoundedPlan("analyze", "analyze")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor("campaign", "session")
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
seed := manifest.New("session", now)
|
|
|
|
|
seed.Campaign = "campaign"
|
|
|
|
|
for _, name := range plan.PrefixNames() {
|
|
|
|
|
seed.MarkStageSucceeded(name, now, nil)
|
|
|
|
|
}
|
|
|
|
|
playersPath := filepath.Join(paths.InputsDir, "players.yml")
|
|
|
|
|
mustWriteTestFile(t, playersPath, string(cfg.Party.Players))
|
|
|
|
|
checksum, err := artifacts.SHA256File(playersPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
descriptor, ok := artifactpolicy.DescribePreparedInputSource("narratio.input.players")
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatal("players source is unavailable")
|
|
|
|
|
}
|
|
|
|
|
seed.Inputs = append(seed.Inputs, manifest.InputRecord{Kind: descriptor.ManifestKind, Path: playersPath, Checksum: checksum})
|
|
|
|
|
saveBoundedManifest(t, cfg, seed)
|
2026-11-20 06:50:56 +00:00
|
|
|
persisted, err := os.ReadFile(manifestPathFor(cfg))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
planned = nativeInspectionForTest(t, append(append([]string{"session", "plan", "session"}, common...), selected...))
|
|
|
|
|
if !strings.Contains(planned, "character_meta_ivy") || strings.Contains(planned, secret) {
|
|
|
|
|
t.Fatal("plan lost prerequisites or exposed credentials")
|
|
|
|
|
}
|
|
|
|
|
afterPlan, err := os.ReadFile(manifestPathFor(cfg))
|
|
|
|
|
if err != nil || !bytes.Equal(persisted, afterPlan) {
|
|
|
|
|
t.Fatal("planning changed durable state")
|
|
|
|
|
}
|
|
|
|
|
failure := errors.New("synthetic later artifact failure")
|
|
|
|
|
runner := &familyWorkflowRunner{failure: failure}
|
|
|
|
|
var captured *config.Config
|
|
|
|
|
application, err := newApplication(commandDependencies{}, func(ctx context.Context, acquired *config.Config, selected BoundedPlan, options RunOptions) (*RunSummary, error) {
|
|
|
|
|
captured = acquired
|
|
|
|
|
before, err := json.Marshal(acquired.Pipeline)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
options.Env = &Env{Scriptorium: runner}
|
|
|
|
|
summary, runErr := executePlan(ctx, acquired, selected, options)
|
|
|
|
|
after, err := json.Marshal(acquired.Pipeline)
|
|
|
|
|
if err != nil || !bytes.Equal(before, after) {
|
|
|
|
|
t.Fatal("native execution mutated completed configuration")
|
|
|
|
|
}
|
|
|
|
|
return summary, runErr
|
2026-11-20 04:42:23 +00:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-20 06:50:56 +00:00
|
|
|
args := append(append([]string{"run", "session"}, common...), selected...)
|
|
|
|
|
var output, diagnostics bytes.Buffer
|
|
|
|
|
outcome := application.Execute(context.Background(), args, commandkit.Streams{Out: &output, Err: &diagnostics})
|
|
|
|
|
if outcome.Status != 1 || !errors.Is(outcome.Cause, failure) {
|
|
|
|
|
t.Fatalf("failed outcome=%#v diagnostics=%s", outcome, diagnostics.String())
|
|
|
|
|
}
|
|
|
|
|
partial := decodeNativeReport[runReport](t, output.Bytes())
|
|
|
|
|
if partial.Complete || len(partial.StageResults) != 1 || partial.StageResults[0].Outcome != "failed" || len(partial.AvailableArtifacts) == 0 {
|
|
|
|
|
t.Fatalf("partial report=%#v", partial)
|
|
|
|
|
}
|
|
|
|
|
failed, err := (&manifest.LocalStore{}).Load(context.Background(), manifestPathFor(cfg))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if failed.Stages["analyze"].AnalyzeArtifacts["character_meta_ivy"].Status != manifest.AnalyzeArtifactCurrent {
|
|
|
|
|
t.Fatal("earlier family checkpoint was lost")
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(output.String()+diagnostics.String(), secret) {
|
|
|
|
|
t.Fatal("failure report exposed credentials")
|
|
|
|
|
}
|
|
|
|
|
runner.failure = nil
|
|
|
|
|
output.Reset()
|
|
|
|
|
diagnostics.Reset()
|
|
|
|
|
outcome = application.Execute(context.Background(), args, commandkit.Streams{Out: &output, Err: &diagnostics})
|
|
|
|
|
if outcome.Status != 0 {
|
|
|
|
|
t.Fatalf("retry status=%d diagnostics=%s", outcome.Status, diagnostics.String())
|
|
|
|
|
}
|
|
|
|
|
summary := decodeNativeReport[runReport](t, output.Bytes())
|
|
|
|
|
if !summary.Complete || !reflect.DeepEqual(summary.Executed, []string{"analyze"}) || len(runner.RunRequests) != 2 || runner.RunRequests[0].PromptID != "meta" || runner.RunRequests[1].PromptID != "note-testing" {
|
|
|
|
|
t.Fatalf("bounded family retry: summary=%+v requests=%+v", summary, runner.RunRequests)
|
|
|
|
|
}
|
|
|
|
|
if captured == nil || len(captured.Pipeline.Scriptorium.Artifacts) != 0 || strings.Contains(output.String()+diagnostics.String(), secret) {
|
|
|
|
|
t.Fatal("retry lost the configuration/report boundary")
|
2026-11-20 04:42:23 +00:00
|
|
|
}
|
|
|
|
|
players, err := os.ReadFile(runner.RunRequests[0].InputPaths["players"])
|
|
|
|
|
if err != nil || !strings.Contains(string(players), "Selected Player") || runner.RunRequests[1].Vars["character_name"] != "Ivy" {
|
|
|
|
|
t.Fatalf("selected roster did not reach both domain consumers: %v", err)
|
|
|
|
|
}
|
|
|
|
|
current, err := (&manifest.LocalStore{}).Load(context.Background(), manifestPathFor(cfg))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
for _, key := range []string{"character_meta_ivy", "character_note_ivy"} {
|
|
|
|
|
record := current.Stages["analyze"].AnalyzeArtifacts[key]
|
|
|
|
|
if record.Status != manifest.AnalyzeArtifactCurrent || record.CharacterID != "ivy" || record.Family == "" {
|
|
|
|
|
t.Fatalf("family output identity: %+v", record)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
after, err := json.Marshal(cfg.Pipeline)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if !bytes.Equal(before, after) || len(cfg.Pipeline.Scriptorium.Artifacts) != 0 {
|
|
|
|
|
t.Fatal("execution changed completed pipeline configuration")
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-20 06:50:56 +00:00
|
|
|
|
|
|
|
|
// familyWorkflowRunner replaces only the downstream process, retaining real
|
|
|
|
|
// acquisition, catalog derivation, execution, persistence, and reporting.
|
|
|
|
|
type familyWorkflowRunner struct {
|
|
|
|
|
scriptorium.FakeRunner
|
|
|
|
|
failure error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *familyWorkflowRunner) RunArtifact(ctx context.Context, request scriptorium.RunArtifactRequest) (scriptorium.ArtifactResult, error) {
|
|
|
|
|
if request.PromptID == "note-testing" && r.failure != nil {
|
|
|
|
|
return scriptorium.ArtifactResult{}, r.failure
|
|
|
|
|
}
|
|
|
|
|
return r.FakeRunner.RunArtifact(ctx, request)
|
|
|
|
|
}
|