diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 2f61347..6d693dd 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -180,7 +180,7 @@ missing prepared history to bypass the collection/hydration path. ## Stage 4: Verify restore and operator readiness use the same requirements -Status: Pending +Status: Complete Read `internal/app/restore_plan.go`, `restore_plan_test.go`, `restore_execution_test.go`, `restore_workflow_test.go`, diff --git a/internal/app/restore_execution_test.go b/internal/app/restore_execution_test.go index bcbeea1..1527b41 100644 --- a/internal/app/restore_execution_test.go +++ b/internal/app/restore_execution_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "io" + "maps" "os" "path/filepath" "strings" @@ -348,6 +349,9 @@ func TestExecuteRestoreDryRunReportsPreviousCacheWithoutWriting(t *testing.T) { cfg, _, _, _ := seedRestoreCommittedState(t, fake, pipelinePath, campaignPath, sessionPath) seedRestorePreviousCurrent(t, fake, cfg, "# previous recap\n") + sessionRoot := artifacts.SessionWorkDirForCampaign(workspaceRoot, cfg.Session.Campaign, cfg.Session.SessionID) + mustWriteTestFile(t, filepath.Join(sessionRoot, "inputs", "existing.txt"), "durable input") + before := snapshotSessionTree(t, sessionRoot) executeRestore := restoreCommandExecutor(fake) var stdout bytes.Buffer @@ -363,7 +367,23 @@ func TestExecuteRestoreDryRunReportsPreviousCacheWithoutWriting(t *testing.T) { t.Fatalf("stdout = %q, want dry-run remote-read notice", stdout.String()) } - sessionRoot := artifacts.SessionWorkDirForCampaign(workspaceRoot, cfg.Session.Campaign, cfg.Session.SessionID) + after := snapshotSessionTree(t, sessionRoot) + for path, value := range before { + if value == "directory" { + delete(before, path) + } + } + for path, value := range after { + if value == "directory" { + delete(after, path) + } + } + if !maps.Equal(before, after) { + t.Fatalf("dry-run changed durable files: before=%v after=%v", before, after) + } + if strings.Contains(stdout.String(), "previous/artifacts/unrelated.md") { + t.Fatalf("unrelated history included: %s", stdout.String()) + } if _, err := os.Stat(filepath.Join(sessionRoot, "inputs", "previous", "artifacts", "session_recap.md")); !os.IsNotExist(err) { t.Fatalf("previous artifact should not be written during dry-run; stat err=%v", err) } @@ -741,13 +761,26 @@ scriptorium: binary: scriptorium artifacts: session_recap: - enabled: true + enabled: false prompt_id: dnd.session_recap output_path: artifacts/session_recap.md inputs: previous_recap: source: narratio.previous_session.artifact.session_recap required: true + child: + enabled: true + prompt_id: dnd.child + output_path: artifacts/child.md + depends_on: [session_recap] + unrelated: + enabled: false + prompt_id: dnd.other + output_path: artifacts/other.md + inputs: + history: + source: narratio.previous_session.artifact.unrelated + required: true `) appendRestoreWorkflowScriptoriumConfig(t, sessionPath, ` previous_session_id: 2026-04-26 @@ -812,3 +845,27 @@ func fakeDownloadCount(fake *storage.FakeBackend, key string) int { } return count } + +func TestExecuteRestoreInstallsOptionalPrerequisiteHistory(t *testing.T) { + workspaceRoot := t.TempDir() + pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot) + appendRestoreWorkflowPreviousInputConfig(t, pipelinePath, sessionPath) + data, err := os.ReadFile(pipelinePath) + if err != nil { + t.Fatal(err) + } + data = bytes.Replace(data, []byte(" required: true\n child:"), []byte(" required: false\n child:"), 1) + if err := os.WriteFile(pipelinePath, data, 0600); err != nil { + t.Fatal(err) + } + fake := &storage.FakeBackend{} + cfg, _, _, _ := seedRestoreCommittedState(t, fake, pipelinePath, campaignPath, sessionPath) + seedRestorePreviousCurrent(t, fake, cfg, "optional history\n") + var stdout, stderr bytes.Buffer + code := restoreCommandExecutor(fake)([]string{"session", "restore", cfg.Session.SessionID, "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &stdout, &stderr) + if code != 0 { + t.Fatalf("restore exit = %d; stderr = %s", code, stderr.String()) + } + root := artifacts.SessionWorkDirForCampaign(workspaceRoot, cfg.Session.Campaign, cfg.Session.SessionID) + mustReadEquals(t, filepath.Join(root, "inputs", "previous", "artifacts", "session_recap.md"), "optional history\n") +} diff --git a/internal/app/restore_plan_test.go b/internal/app/restore_plan_test.go index 11280a8..41c1efe 100644 --- a/internal/app/restore_plan_test.go +++ b/internal/app/restore_plan_test.go @@ -512,7 +512,7 @@ func configureRestorePlanPreviousRequirement(cfg *config.Config, required bool) cfg.Pipeline.Scriptorium = &config.ScriptoriumConfig{ Artifacts: map[string]config.ScriptoriumArtifactConfig{ "session_recap": { - Enabled: true, + Enabled: false, PromptID: "dnd.session_recap", OutputPath: "artifacts/session_recap.md", Inputs: map[string]config.ScriptoriumInputConfig{ @@ -522,6 +522,8 @@ func configureRestorePlanPreviousRequirement(cfg *config.Config, required bool) }, }, }, + "child": {Enabled: true, PromptID: "child", OutputPath: "artifacts/child.md", DependsOn: []string{"session_recap"}}, + "unrelated": {PromptID: "other", OutputPath: "artifacts/other.md", Inputs: map[string]config.ScriptoriumInputConfig{"history": {Source: "narratio.previous_session.artifact.unrelated", Required: true}}}, }, } cfg.ArtifactCatalog = &config.ArtifactCatalog{Artifacts: cfg.Pipeline.Scriptorium.Artifacts} @@ -545,3 +547,20 @@ func restorePlanCurrentState(t *testing.T, cfg *config.Config) *RemoteCurrentSta }, } } + +func TestRestorePlanIncludesAvailableOptionalPrerequisiteHistory(t *testing.T) { + cfg := restorePlanConfig(t) + configureRestorePlanPreviousRequirement(cfg, false) + current := restorePlanCurrentState(t, cfg) + store := &storage.FakeBackend{} + seedCurrentRestoreObject(t, store, current, current.CurrentManifestKey, []byte(`{"session_id":"2026-05-03"}`)) + seedRestorePreviousCurrent(t, store, cfg, "optional history\n") + plan, err := buildRestorePlan(context.Background(), cfg, current, store, RestorePlanOptions{}) + if err != nil { + t.Fatal(err) + } + want := []string{"inputs/previous/artifacts/session_recap.md", "inputs/previous/manifest.json", "manifest.json"} + if got := actionRelPaths(plan.Actions); !slices.Equal(got, want) { + t.Fatalf("actions = %v, want %v", got, want) + } +} diff --git a/internal/app/restore_workflow_test.go b/internal/app/restore_workflow_test.go index b6af720..84e7c2f 100644 --- a/internal/app/restore_workflow_test.go +++ b/internal/app/restore_workflow_test.go @@ -143,7 +143,7 @@ scriptorium: binary: scriptorium artifacts: session_recap: - enabled: true + enabled: false prompt_id: dnd.session_recap output_path: artifacts/session_recap.md inputs: @@ -153,6 +153,11 @@ scriptorium: previous_recap: source: narratio.previous_session.artifact.session_recap required: true + child: + enabled: true + prompt_id: dnd.child + output_path: artifacts/child.md + depends_on: [session_recap] `) appendRestoreWorkflowScriptoriumConfig(t, sessionPath, ` previous_session_id: 2026-04-26 @@ -161,7 +166,18 @@ previous_session_id: 2026-04-26 fakeStore := &storage.FakeBackend{} cfg, sessionPrefix, manifestKey, runIDKey := seedRestoreCommittedState(t, fakeStore, pipelinePath, campaignPath, sessionPath) seedRestoreObject(fakeStore, runIDKey, []byte("20260519T010203Z-a1b2c3d4\n")) - seedRestoreObject(fakeStore, manifestKey, restoreWorkflowManifestJSON(t, cfg.Session.SessionID, cfg.Session.Campaign)) + sessionRoot := artifacts.SessionWorkDirForCampaign(workspaceRoot, cfg.Session.Campaign, cfg.Session.SessionID) + var committed manifest.Manifest + if err := json.Unmarshal(restoreWorkflowManifestJSON(t, cfg.Session.SessionID, cfg.Session.Campaign), &committed); err != nil { + t.Fatal(err) + } + delete(committed.Stages, "analyze") + committed.Inputs = append(committed.Inputs, manifest.InputRecord{Kind: "previous_artifact", Source: "narratio.previous_session.artifact.session_recap", Path: "inputs/previous/artifacts/session_recap.md", Checksum: restoreCommitSHA256([]byte("# previous recap\n"))}) + committedData, err := json.Marshal(&committed) + if err != nil { + t.Fatal(err) + } + seedRestoreObject(fakeStore, manifestKey, committedData) seedRestoreObject(fakeStore, sessionPrefix+"transcripts/final.trimmed.json", []byte(`{"segments":[]}`+"\n")) seedRestorePreviousCurrent(t, fakeStore, cfg, "# previous recap\n") @@ -185,17 +201,7 @@ previous_session_id: 2026-04-26 t.Fatalf("restore stderr = %q, want empty", stderr.String()) } - sessionRoot := artifacts.SessionWorkDirForCampaign(workspaceRoot, cfg.Session.Campaign, cfg.Session.SessionID) mustReadEquals(t, filepath.Join(sessionRoot, "transcripts", "final.trimmed.json"), `{"segments":[]}`+"\n") - localManifest, err := (&manifest.LocalStore{}).Load(context.Background(), filepath.Join(sessionRoot, "manifest.json")) - if err != nil { - t.Fatalf("load restored manifest: %v", err) - } - setCurrentBuiltInEvidence(t, localManifest, "trim", artifacts.TranscriptOutputKindFinalTrimmed, artifacts.ArtifactTranscriptFinalTrimmed, filepath.Join(sessionRoot, "transcripts", "final.trimmed.json")) - setCurrentPreviousArtifactInput(t, localManifest, filepath.Join(sessionRoot, "inputs", "previous", "artifacts", "session_recap.md")) - if err := (&manifest.LocalStore{}).Save(context.Background(), filepath.Join(sessionRoot, "manifest.json"), localManifest); err != nil { - t.Fatalf("save manifest evidence: %v", err) - } previousManifestBytes, err := os.ReadFile(filepath.Join(sessionRoot, "inputs", "previous", "manifest.json")) if err != nil { t.Fatalf("read restored previous manifest: %v", err) @@ -228,7 +234,7 @@ previous_session_id: 2026-04-26 "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--force", - "--artifacts", "session_recap", + "--artifacts", "child", }, &stdout, &stderr, @@ -243,8 +249,8 @@ previous_session_id: 2026-04-26 if objectStoreConstructed { t.Fatal("single-stage analysis should not construct object store for previous-session input resolution") } - if len(scriptoriumFake.RunRequests) != 1 { - t.Fatalf("scriptorium run requests = %d, want 1", len(scriptoriumFake.RunRequests)) + if len(scriptoriumFake.RunRequests) != 2 { + t.Fatalf("scriptorium run requests = %d, want 2", len(scriptoriumFake.RunRequests)) } req := scriptoriumFake.RunRequests[0] if got := req.InputPaths["transcript"]; got != filepath.Join(sessionRoot, "transcripts", "final.trimmed.json") { diff --git a/internal/app/session_readiness_test.go b/internal/app/session_readiness_test.go index ebee146..8cfc165 100644 --- a/internal/app/session_readiness_test.go +++ b/internal/app/session_readiness_test.go @@ -1,8 +1,10 @@ package app import ( + "bytes" "context" "errors" + "fmt" "os" "path/filepath" "strings" @@ -318,3 +320,42 @@ func TestSessionReadinessChecksS3PreparationEvidence(t *testing.T) { t.Fatalf("access failure=%+v error=%v", failed, err) } } + +func TestReadinessHydratesPrerequisiteHistory(t *testing.T) { + for _, required := range []bool{true, false} { + t.Run(fmt.Sprintf("required=%v", required), func(t *testing.T) { + pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir()) + appendRestoreWorkflowPreviousInputConfig(t, pipeline, session) + if !required { + data, err := os.ReadFile(pipeline) + if err != nil { + t.Fatal(err) + } + data = bytes.Replace(data, []byte(" required: true\n child:"), []byte(" required: false\n child:"), 1) + if err := os.WriteFile(pipeline, data, 0600); err != nil { + t.Fatal(err) + } + } + cfg, err := config.LoadWithSessionOptions(pipeline, campaign, session, config.SessionLoadOptions{}) + if err != nil { + t.Fatal(err) + } + store := &storage.FakeBackend{} + seedRestorePreviousCurrent(t, store, cfg, "# previous recap\n") + deps := fakeCommandDependencies(store, nil) + status, err := inspectSessionState(context.Background(), cfg, true, true, deps) + if err != nil { + t.Fatal(err) + } + if len(status.Previous) != 1 || status.Previous[0].Name != "session_recap" || status.Previous[0].Required != required || status.Previous[0].State != "ready" { + t.Fatalf("previous readiness = %#v", status.Previous) + } + report, err := preflightSelectedSession(context.Background(), readinessInput(pipeline, campaign, session), deps) + if err != nil || !report.Ready || report.Preparation.Outcome != "completed" { + t.Fatalf("preflight = %+v, error = %v", report, err) + } + paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID) + mustReadEquals(t, filepath.Join(paths.PreviousArtifactsDir, "session_recap.md"), "# previous recap\n") + }) + } +}