diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 87302a3..1398e5f 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -281,7 +281,13 @@ old configured source references fail with actionable migration guidance. ## Stage 7. Materialize shared collections in prepare -Status: Pending +Status: Complete + +Preparation resolves vocabulary before audio work and writes all four named +collections with manifest checksums, sizes, and provenance. Focused prepare, +artifact, and manifest tests verify nested workspace modes, empty roles, sorted +records, failure without manifest replacement, and existing audio/party inputs. +Resume comparison and downstream consumer cutover remain in later stages. Start with `stage/prepare.go`, `prepare_party.go`, and configuration rendering. Call Stage 5's resolver as part of ordinary preparation and fail on invalid diff --git a/internal/artifacts/prepared_input.go b/internal/artifacts/prepared_input.go index 43a3d82..1688c5e 100644 --- a/internal/artifacts/prepared_input.go +++ b/internal/artifacts/prepared_input.go @@ -128,6 +128,9 @@ func ResolvePreparedInput(paths SessionPaths, m *manifest.Manifest, sourceID str if closeErr != nil { return PreparedInputIdentity{}, fmt.Errorf("close prepared input source %q: %w", descriptor.SourceID, closeErr) } + if record.Size != 0 && record.Size != size { + return PreparedInputIdentity{}, fmt.Errorf("prepared input source %q size mismatch: manifest=%d actual=%d", sourceID, record.Size, size) + } if size == 0 { return PreparedInputIdentity{}, fmt.Errorf("prepared input source %q is empty", descriptor.SourceID) } diff --git a/internal/artifacts/prepared_input_test.go b/internal/artifacts/prepared_input_test.go index 32e70e5..f233647 100644 --- a/internal/artifacts/prepared_input_test.go +++ b/internal/artifacts/prepared_input_test.go @@ -217,6 +217,11 @@ func TestResolvePreparedGlossaryDocuments(t *testing.T) { if got.Path != file || got.Checksum != checksum || got.Size != int64(len(payload)) || !strings.Contains(got.RelativePath, "inputs/glossaries/") { t.Fatalf("identity: %+v", got) } + m.Inputs[0].Size = int64(len(payload)) + 1 + if _, err := ResolvePreparedInput(paths, m, source); err == nil || !strings.Contains(err.Error(), "size mismatch") { + t.Fatalf("incorrect declared size: %v", err) + } + m.Inputs[0].Size = 0 if err := os.WriteFile(file, []byte("changed"), 0644); err != nil { t.Fatal(err) } diff --git a/internal/manifest/manifest.go b/internal/manifest/manifest.go index fa76fb5..345bd2f 100644 --- a/internal/manifest/manifest.go +++ b/internal/manifest/manifest.go @@ -25,6 +25,7 @@ type InputRecord struct { Kind string `json:"kind"` Path string `json:"path"` Checksum string `json:"checksum,omitempty"` + Size int64 `json:"size,omitempty"` Source string `json:"source,omitempty"` S3Bucket string `json:"s3_bucket,omitempty"` S3Key string `json:"s3_key,omitempty"` diff --git a/internal/stage/prepare.go b/internal/stage/prepare.go index 69ce053..c560a73 100644 --- a/internal/stage/prepare.go +++ b/internal/stage/prepare.go @@ -43,6 +43,11 @@ func (prepareStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S return nil, fmt.Errorf("prepare: session id is required") } + glossaryDocuments, err := config.ResolveGlossaries(env.Config) + if err != nil { + return nil, fmt.Errorf("prepare: resolve glossaries: %w", err) + } + paths, err := ensureLayoutForEnv(env, sessionID) if err != nil { return nil, fmt.Errorf("prepare: ensure workdir layout: %w", err) @@ -53,7 +58,6 @@ func (prepareStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S speakersInput := stableInputSource(env.Config.StableInputs.SpeakersFile, env.Config.Session.Inputs.SpeakersFile, sessionSrc) autocorrectInput := stableInputSource(env.Config.StableInputs.AutocorrectFile, env.Config.Session.Inputs.AutocorrectFile, sessionSrc) - glossaryInput := stableInputSource(env.Config.StableInputs.GlossaryFile, config.OptionalString(env.Config.Session.Inputs.GlossaryFile), sessionSrc) spellCatalogInput := stableInputSource(env.Config.StableInputs.SpellCatalogFile, env.Config.Session.Inputs.SpellCatalogFile, sessionSrc) speakersSrc, err := resolveConfigRelativePath(speakersInput) @@ -64,10 +68,6 @@ func (prepareStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S if err != nil { return nil, fmt.Errorf("prepare: autocorrect path: %w", err) } - glossarySrc, err := resolveConfigRelativePath(glossaryInput) - if err != nil { - return nil, fmt.Errorf("prepare: glossary path: %w", err) - } spellCatalogConfigured := strings.TrimSpace(spellCatalogInput.Path) != "" spellCatalogSrc := "" if spellCatalogConfigured { @@ -86,7 +86,6 @@ func (prepareStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S }{ {path: speakersSrc, name: "speakers.yml"}, {path: autocorrectSrc, name: "autocorrect.yml"}, - {path: glossarySrc, name: "glossary.yml"}, } { if err := requireFile(required.path, required.name); err != nil { return nil, fmt.Errorf("prepare: %w", err) @@ -165,20 +164,6 @@ func (prepareStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S {kind: "speakers", src: speakersSrc, dst: filepath.Join(paths.InputsDir, "speakers.yml"), source: speakersInput.Source}, {kind: "autocorrect", src: autocorrectSrc, dst: filepath.Join(paths.InputsDir, "autocorrect.yml"), source: autocorrectInput.Source}, } - for _, prepared := range []struct { - sourceID string - input config.ResolvedInputFile - src string - }{{sourceID: artifactpolicy.SourceInputGlossary, input: glossaryInput, src: glossarySrc}} { - descriptor, ok := artifactpolicy.DescribePreparedInputSource(prepared.sourceID) - if !ok { - return nil, fmt.Errorf("prepare: prepared-input descriptor %q is unavailable", prepared.sourceID) - } - preparedConfigFiles = append(preparedConfigFiles, preparedConfigFile{ - kind: descriptor.ManifestKind, src: prepared.src, - dst: filepath.Join(paths.InputsDir, descriptor.Filename), source: prepared.input.Source, - }) - } spellDescriptor, ok := artifactpolicy.DescribePreparedInputSource(artifactpolicy.SourceInputSpellCatalog) if !ok { return nil, fmt.Errorf("prepare: spell catalog prepared-input descriptor is unavailable") @@ -202,6 +187,27 @@ func (prepareStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S } registerConfigInput(cfgFile.kind, cfgFile.dst, checksum, cfgFile.source) } + for _, item := range []struct{ name, source string }{ + {"party", artifactpolicy.SourceInputGlossaryParty}, + {"campaign", artifactpolicy.SourceInputGlossaryCampaign}, + {"setting", artifactpolicy.SourceInputGlossarySetting}, + {"spells", artifactpolicy.SourceInputGlossarySpells}, + } { + descriptor, ok := artifactpolicy.DescribePreparedInputSource(item.source) + if !ok { + return nil, fmt.Errorf("prepare: missing descriptor %q", item.source) + } + dst := filepath.Join(paths.InputsDir, descriptor.Filename) + if err := fileops.EnsureWorkspaceDirectory(filepath.Dir(dst)); err != nil { + return nil, fmt.Errorf("prepare: create glossary directory: %w", err) + } + data := glossaryDocuments[item.name] + checksum, err := writeBytesIfChanged(env.ArtifactStore, dst, data) + if err != nil { + return nil, fmt.Errorf("prepare: materialize %s: %w", descriptor.ManifestKind, err) + } + inputs = append(inputs, manifest.InputRecord{Kind: descriptor.ManifestKind, Path: dst, Checksum: checksum, Size: int64(len(data)), Source: "derived_from_configuration"}) + } partyInputs, err := materializePreparedPartyInputs(env.ArtifactStore, env.Config, paths.InputsDir) if err != nil { return nil, fmt.Errorf("prepare: %w", err) diff --git a/internal/stage/prepare_test.go b/internal/stage/prepare_test.go index ee16784..ae6e096 100644 --- a/internal/stage/prepare_test.go +++ b/internal/stage/prepare_test.go @@ -43,7 +43,10 @@ func TestPrepareStageExplicitAudioFiles(t *testing.T) { filepath.Join(paths.InputsDir, "pipeline.resolved.yml"), filepath.Join(paths.InputsDir, "speakers.yml"), filepath.Join(paths.InputsDir, "autocorrect.yml"), - filepath.Join(paths.InputsDir, "glossary.yml"), + filepath.Join(paths.InputsDir, "glossaries", "party.yml"), + filepath.Join(paths.InputsDir, "glossaries", "campaign.yml"), + filepath.Join(paths.InputsDir, "glossaries", "setting.yml"), + filepath.Join(paths.InputsDir, "glossaries", "spells.yml"), filepath.Join(paths.InputsDir, "players.yml"), filepath.Join(paths.InputsDir, "party.yml"), filepath.Join(paths.AudioDir, "alice.flac"), @@ -54,8 +57,8 @@ func TestPrepareStageExplicitAudioFiles(t *testing.T) { } } - if len(m.Inputs) != 10 { - t.Fatalf("manifest inputs len = %d, want 10", len(m.Inputs)) + if len(m.Inputs) != 13 { + t.Fatalf("manifest inputs len = %d, want 13", len(m.Inputs)) } for _, in := range m.Inputs { if in.Checksum == "" { @@ -989,3 +992,59 @@ func projectedTestPlayers(t *testing.T, party *config.CanonicalParty) []byte { } return data } + +func TestPrepareWritesVerifiedGlossaryDocuments(t *testing.T) { + env, m := setupPrepareEnv(t) + root := filepath.Dir(env.Config.SessionPath) + writeFile(t, filepath.Join(root, "audio", "a.flac"), "a") + env.Config.Session.Inputs.AudioFiles = []string{"./audio/a.flac"} + if _, err := (prepareStage{}).Run(context.Background(), env, m); err != nil { + t.Fatal(err) + } + paths := sessionPathsForEnv(env, m.SessionID) + for _, source := range []string{"narratio.input.glossary.party", "narratio.input.glossary.campaign", "narratio.input.glossary.setting", "narratio.input.glossary.spells"} { + identity, err := artifacts.ResolvePreparedInput(paths, m, source) + if err != nil { + t.Fatal(err) + } + if identity.Size == 0 { + t.Fatalf("missing size: %+v", identity) + } + info, err := os.Stat(identity.Path) + if err != nil { + t.Fatal(err) + } + if info.Mode().Perm() != 0664 { + t.Fatalf("file mode %s: %v", identity.Path, info.Mode()) + } + } + info, err := os.Stat(filepath.Join(paths.InputsDir, "glossaries")) + if err != nil { + t.Fatal(err) + } + if info.Mode().Perm() != 0775 || info.Mode()&os.ModeSetgid == 0 { + t.Fatalf("directory mode: %v", info.Mode()) + } + for i := 1; i < len(m.Inputs); i++ { + if m.Inputs[i-1].Kind > m.Inputs[i].Kind { + t.Fatalf("unsorted inputs: %+v", m.Inputs) + } + } + for _, record := range m.Inputs { + if record.Kind == "glossary" { + t.Fatal("legacy glossary recorded") + } + } +} + +func TestPrepareGlossaryFailurePreservesManifest(t *testing.T) { + env, m := setupPrepareEnv(t) + env.Config.Session.Inputs.GlossaryFile = new(filepath.Join(t.TempDir(), "missing.yml")) + prior := append([]manifest.InputRecord(nil), m.Inputs...) + if _, err := (prepareStage{}).Run(context.Background(), env, m); err == nil || !strings.Contains(err.Error(), "vocabulary") { + t.Fatalf("missing vocabulary: %v", err) + } + if len(m.Inputs) != len(prior) { + t.Fatalf("manifest changed: %+v", m.Inputs) + } +}