diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 342cb54..cf1fce8 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -86,7 +86,7 @@ conditions under which preparation runs. ## Stage 2: Supply object storage to read-only session planning -Status: Pending +Status: Complete Read `internal/app/plan_command.go`, `plan.go`, `semantic_resume.go`, `object_store.go`, and the preparation branch of `runner_stage_execution.go`. diff --git a/internal/app/plan.go b/internal/app/plan.go index 738fd12..fce97a7 100644 --- a/internal/app/plan.go +++ b/internal/app/plan.go @@ -6,6 +6,7 @@ import ( "fmt" "time" + "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" "gitea.maximumdirect.net/eric/narratio/internal/config" "gitea.maximumdirect.net/eric/narratio/internal/manifest" @@ -72,7 +73,7 @@ type projectedStage struct { Analysis *stage.AnalyzeResumeSummary } -func projectSelectedWork(ctx context.Context, cfg *config.Config, plan BoundedPlan, force bool, selectedArtifacts []string, effective artifacts.EffectiveArtifactSet, store artifacts.Store, snapshot *manifest.Manifest) (*workProjection, error) { +func projectSelectedWork(ctx context.Context, cfg *config.Config, plan BoundedPlan, force bool, selectedArtifacts []string, effective artifacts.EffectiveArtifactSet, store artifacts.Store, objectStore storage.ObjectStore, snapshot *manifest.Manifest) (*workProjection, error) { model, err := cloneManifestForPlan(snapshot, cfg) if err != nil { return nil, fmt.Errorf("plan: clone session state: %w", err) @@ -81,7 +82,7 @@ func projectSelectedWork(ctx context.Context, cfg *config.Config, plan BoundedPl stages := plan.Stages() stageEnv := &stage.Env{ Config: cfg, SelectedArtifactKeys: append([]string(nil), selectedArtifacts...), - EffectiveArtifacts: effective, ArtifactStore: store, Force: force, + EffectiveArtifacts: effective, ArtifactStore: store, ObjectStore: objectStore, Force: force, } projection := &workProjection{Model: model} diff --git a/internal/app/plan_command.go b/internal/app/plan_command.go index dd66547..5dc567f 100644 --- a/internal/app/plan_command.go +++ b/internal/app/plan_command.go @@ -5,8 +5,10 @@ import ( "errors" "fmt" + "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" "gitea.maximumdirect.net/eric/narratio/internal/config" + "gitea.maximumdirect.net/eric/narratio/internal/manifest" "gitea.maximumdirect.net/eric/narratio/internal/stage" "go.maximumdirect.net/commandkit" ) @@ -87,10 +89,10 @@ func planSelectedPipeline(ctx context.Context, input *executionInput, deps comma if err != nil { return nil, &inputError{message: err.Error(), cause: err} } - return buildSessionPlan(ctx, cfg, input, selected, effective) + return buildSessionPlan(ctx, cfg, input, selected, effective, deps) } -func buildSessionPlan(ctx context.Context, cfg *config.Config, input *executionInput, selectedArtifacts []string, effective artifacts.EffectiveArtifactSet) (*sessionPlanReport, error) { +func buildSessionPlan(ctx context.Context, cfg *config.Config, input *executionInput, selectedArtifacts []string, effective artifacts.EffectiveArtifactSet, deps commandDependencies) (*sessionPlanReport, error) { if err := ctx.Err(); err != nil { return nil, err } @@ -101,8 +103,15 @@ func buildSessionPlan(ctx context.Context, cfg *config.Config, input *executionI if err := validateBoundedPrerequisites(input.plan, m); err != nil { return nil, fmt.Errorf("plan: %w", err) } + var objectStore storage.ObjectStore + if input.plan.Contains("prepare") && !input.Force.Value && cfg.Session.Inputs.AudioS3 != nil && recordedStageStatus(m, "prepare") == manifest.StatusSucceeded { + objectStore, err = newCommandObjectStoreWithDependencies(ctx, cfg, deps) + if err != nil { + return nil, fmt.Errorf("plan: %w", err) + } + } store := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root) - projection, err := projectSelectedWork(ctx, cfg, input.plan, input.Force.Value, selectedArtifacts, effective, store, m) + projection, err := projectSelectedWork(ctx, cfg, input.plan, input.Force.Value, selectedArtifacts, effective, store, objectStore, m) if projection == nil { return nil, err } diff --git a/internal/app/plan_s3_test.go b/internal/app/plan_s3_test.go new file mode 100644 index 0000000..afeb3bb --- /dev/null +++ b/internal/app/plan_s3_test.go @@ -0,0 +1,169 @@ +package app + +import ( + "bytes" + "context" + "errors" + "os" + "path/filepath" + "reflect" + "strings" + "testing" + "time" + + "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" + "gitea.maximumdirect.net/eric/narratio/internal/artifacts" + "gitea.maximumdirect.net/eric/narratio/internal/config" + "gitea.maximumdirect.net/eric/narratio/internal/manifest" + "gitea.maximumdirect.net/eric/narratio/internal/stage" +) + +func TestSessionPlanUsesS3EvidenceWithoutChangingSession(t *testing.T) { + pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir()) + replaceInFileOrFatal(t, session, "audio_dir: ./audio", "audio_s3:\n prefix: audio/") + cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{}) + if err != nil { + t.Fatal(err) + } + key := artifacts.S3SessionPrefix(cfg.Pipeline.Storage.S3.RootPrefix, cfg.Session.Campaign, cfg.Session.SessionID) + "audio/alice.flac" + remote := &storage.FakeBackend{} + remote.SeedObject(storage.FakeObject{Key: key, Data: []byte("original audio")}) + prepare, err := stage.Select("prepare") + if err != nil { + t.Fatal(err) + } + run := func() *RunSummary { + t.Helper() + summary, err := executeStages(context.Background(), cfg, []stage.Stage{prepare}, RunOptions{Env: &Env{ObjectStore: remote}}) + if err != nil { + t.Fatal(err) + } + return summary + } + first := run() + mstore := &manifest.LocalStore{} + m, err := mstore.Load(context.Background(), first.ManifestPath) + if err != nil { + t.Fatal(err) + } + m.MarkStageSucceeded("transcribe", time.Now().UTC(), nil) + seedCurrentSemanticEvidence(t, cfg, m, "transcribe") + if err := mstore.Save(context.Background(), first.ManifestPath, m); err != nil { + t.Fatal(err) + } + args := []string{"session", "plan", "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--through", "transcribe", "--format", "json"} + deps := fakeCommandDependencies(remote, nil) + plan := func(wantCode int, d commandDependencies, extra ...string) sessionPlanReport { + t.Helper() + before := snapshotSessionTree(t, filepath.Dir(first.ManifestPath)) + reads, uploads, deletes := len(remote.Reads), len(remote.Uploads), len(remote.Deletes) + var out, diagnostics bytes.Buffer + code := executeWithDependencies(append(append([]string{}, args...), extra...), &out, &diagnostics, d, nil) + if code != wantCode { + t.Fatalf("plan code=%d diagnostics=%s", code, diagnostics.String()) + } + if code != 0 && !strings.Contains(diagnostics.String(), "synthetic store initialization failure") { + t.Fatalf("missing initialization failure: %s", diagnostics.String()) + } + if !reflect.DeepEqual(before, snapshotSessionTree(t, filepath.Dir(first.ManifestPath))) { + t.Fatal("planning changed session files") + } + if reads != len(remote.Reads) || uploads != len(remote.Uploads) || deletes != len(remote.Deletes) { + t.Fatal("planning transferred or mutated remote objects") + } + if code != 0 { + return sessionPlanReport{} + } + return decodeNativeReport[sessionPlanReport](t, out.Bytes()) + } + report := plan(0, deps) + if report.Stages[0].Action != "skip" || report.Stages[1].Action != "skip" { + t.Fatalf("unchanged plan=%+v", report.Stages) + } + if result := run(); result.StageResults[0].Outcome != "reused" { + t.Fatalf("unchanged execution=%+v", result.StageResults) + } + remote.ListErr = errors.New("synthetic listing outage") + report = plan(0, deps) + if report.Stages[0].Action != "run" || !strings.Contains(report.Stages[0].Reason, "could not be verified") || !strings.Contains(report.Stages[0].Reason, "synthetic listing outage") || report.Stages[1].Action != "run" { + t.Fatalf("unverified plan=%+v", report.Stages) + } + remote.ListErr = nil + failure := errors.New("synthetic store initialization failure") + failing := deps + failing.newObjectStore = func(context.Context, *config.Config) (storage.ObjectStore, error) { return nil, failure } + plan(1, failing) + report = plan(0, failing, "--force") + if report.Stages[0].Action != "run" { + t.Fatalf("forced plan=%+v", report.Stages) + } + report = plan(0, failing, "--from", "transcribe") + if len(report.Stages) != 1 || report.Stages[0].Name != "transcribe" { + t.Fatalf("downstream plan=%+v", report.Stages) + } + remote.SeedObject(storage.FakeObject{Key: key, Data: []byte("changed audio")}) + report = plan(0, deps) + if report.Stages[0].Action != "run" || !strings.Contains(report.Stages[0].Reason, "changed") || report.Stages[1].Action != "run" { + t.Fatalf("changed plan=%+v", report.Stages) + } + if result := run(); result.StageResults[0].Outcome != "completed" { + t.Fatalf("changed execution=%+v", result.StageResults) + } + m, err = mstore.Load(context.Background(), first.ManifestPath) + if err != nil || m.Stages["transcribe"].Status != manifest.StatusStale { + t.Fatalf("dependent state=%+v error=%v", m.Stages["transcribe"], err) + } +} + +func snapshotSessionTree(t *testing.T, root string) map[string]string { + t.Helper() + files := map[string]string{} + err := filepath.WalkDir(root, func(path string, entry os.DirEntry, err error) error { + if err != nil { + return err + } + rel, err := filepath.Rel(root, path) + if err != nil { + return err + } + if entry.IsDir() { + files[rel] = "directory" + return nil + } + if entry.Type()&os.ModeSymlink != 0 { + target, err := os.Readlink(path) + files[rel] = "link:" + target + return err + } + data, err := os.ReadFile(path) + if err != nil { + return err + } + files[rel] = string(data) + return nil + }) + if err != nil { + t.Fatal(err) + } + return files +} + +func TestSessionPlanDoesNotInitializeStoreForNewOrLocalPreparation(t *testing.T) { + for _, source := range []string{"new S3", "local"} { + t.Run(source, func(t *testing.T) { + pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir()) + if source == "new S3" { + replaceInFileOrFatal(t, session, "audio_dir: ./audio", "audio_s3:\n prefix: audio/") + } + deps := fakeCommandDependencies(&storage.FakeBackend{}, nil) + deps.newObjectStore = func(context.Context, *config.Config) (storage.ObjectStore, error) { + return nil, errors.New("unexpected store initialization") + } + var out, diagnostic bytes.Buffer + code := executeWithDependencies([]string{"session", "plan", "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--through", "prepare"}, &out, &diagnostic, deps, nil) + if code != 0 { + t.Fatalf("code=%d diagnostic=%s", code, diagnostic.String()) + } + }) + } +} diff --git a/internal/app/plan_test.go b/internal/app/plan_test.go index 714c6ea..500ceb6 100644 --- a/internal/app/plan_test.go +++ b/internal/app/plan_test.go @@ -89,7 +89,7 @@ func TestProjectionUsesInjectedSnapshotAndSchedulesPrerequisites(t *testing.T) { t.Fatal(err) } store := &observingArtifactStore{Store: artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)} - projection, err := projectSelectedWork(context.Background(), cfg, plan, false, selection, effective, store, snapshot) + projection, err := projectSelectedWork(context.Background(), cfg, plan, false, selection, effective, store, nil, snapshot) if err != nil { t.Fatal(err) } diff --git a/internal/app/preflight_targets_test.go b/internal/app/preflight_targets_test.go index 41a32c5..9227fd0 100644 --- a/internal/app/preflight_targets_test.go +++ b/internal/app/preflight_targets_test.go @@ -31,7 +31,7 @@ func TestInspectionTargetsUseProjectedWorkAndDefinitionClosure(t *testing.T) { if err != nil { t.Fatal(err) } - projection, err := projectSelectedWork(context.Background(), cfg, plan, false, selection, effective, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil) + projection, err := projectSelectedWork(context.Background(), cfg, plan, false, selection, effective, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil, nil) if err != nil { t.Fatal(err) } @@ -106,7 +106,7 @@ func TestInspectionTargetsExpandSelectedFamilyAndIgnoreUnrelatedRange(t *testing if err != nil { t.Fatal(err) } - projection, err := projectSelectedWork(context.Background(), cfg, plan, false, selected, effective, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil) + projection, err := projectSelectedWork(context.Background(), cfg, plan, false, selected, effective, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil, nil) if err != nil { t.Fatal(err) } @@ -118,7 +118,7 @@ func TestInspectionTargetsExpandSelectedFamilyAndIgnoreUnrelatedRange(t *testing if err != nil { t.Fatal(err) } - projection, err = projectSelectedWork(context.Background(), cfg, plan, false, nil, artifacts.EffectiveArtifactSet{}, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil) + projection, err = projectSelectedWork(context.Background(), cfg, plan, false, nil, artifacts.EffectiveArtifactSet{}, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil, nil) if err != nil { t.Fatal(err) } diff --git a/internal/app/runner_preflight.go b/internal/app/runner_preflight.go index d944427..4fa3b34 100644 --- a/internal/app/runner_preflight.go +++ b/internal/app/runner_preflight.go @@ -13,7 +13,7 @@ func inspectInvocationRequirements(runtime *invocationRuntime, start int) error } remaining := runtime.plan remaining.stages = append([]stage.Stage(nil), runtime.stages[start:]...) - projection, err := projectSelectedWork(runtime.ctx, runtime.cfg, remaining, runtime.opts.Force, runtime.opts.SelectedArtifacts, runtime.effectiveArtifacts, runtime.artifactStore, runtime.manifest) + projection, err := projectSelectedWork(runtime.ctx, runtime.cfg, remaining, runtime.opts.Force, runtime.opts.SelectedArtifacts, runtime.effectiveArtifacts, runtime.artifactStore, runtime.env.ObjectStore, runtime.manifest) if err != nil { return fmt.Errorf("project downstream preflight work: %w", err) } diff --git a/internal/app/session_readiness.go b/internal/app/session_readiness.go index a2d634b..a580e8f 100644 --- a/internal/app/session_readiness.go +++ b/internal/app/session_readiness.go @@ -144,7 +144,7 @@ func assessSessionReadiness(runtime *invocationRuntime, report *sessionReadiness plan, planErr := BuildBoundedPlan("transcribe", "trim") if planErr == nil { var projection *workProjection - projection, planErr = projectSelectedWork(runtime.ctx, runtime.cfg, plan, false, nil, runtime.effectiveArtifacts, runtime.artifactStore, runtime.manifest) + projection, planErr = projectSelectedWork(runtime.ctx, runtime.cfg, plan, false, nil, runtime.effectiveArtifacts, runtime.artifactStore, runtime.env.ObjectStore, runtime.manifest) if planErr == nil { useCurrent = currentTranscriptUsableForNotarius(projection) }