diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 1ce94b6..d879a59 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -76,7 +76,7 @@ filesystem/concurrency guarantees are part of this work. ## Stage 1: Make Run-Bundle Disposal Explicit -Status: Pending. +Status: Complete. Read `internal/stage/extract.go`, `extract_resume.go`, `extract_references.go`, `extract_native_test.go`, `internal/artifacts/extraction_evidence.go`, diff --git a/internal/artifacts/paths.go b/internal/artifacts/paths.go index 883596c..de0c810 100644 --- a/internal/artifacts/paths.go +++ b/internal/artifacts/paths.go @@ -147,11 +147,6 @@ func SessionRunNotariusOutputRootForCampaign(rootDir, campaign, sessionID, runID return filepath.Join(SessionRunExtractDirForCampaign(rootDir, campaign, sessionID, runID), "notarius-output") } -// SessionRunNotariusRetentionPath records the session-durable role of a bundle. -func SessionRunNotariusRetentionPath(paths SessionPaths, runID string) string { - return filepath.Join(paths.RunsDir, runID, "extract", "notarius-retention.json") -} - // SessionNotariusReferenceCacheDir returns the stable cache root for verified // reference snapshots supplied to Notarius. func SessionNotariusReferenceCacheDir(paths SessionPaths) string { diff --git a/internal/stage/extract.go b/internal/stage/extract.go index bf63373..7d4620b 100644 --- a/internal/stage/extract.go +++ b/internal/stage/extract.go @@ -217,14 +217,6 @@ func (extractStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (re ExternalProvenance: &artifactmodel.ExternalProvenance{System: "notarius", PipelineID: report.Pipeline, ArtifactID: lane.LaneID}, }) } - retentionPath := artifacts.SessionRunNotariusRetentionPath(paths, runID) - retention, err := json.Marshal(map[string]any{"role": "session_durable", "bundle_root": bundle}) - if err != nil { - return nil, err - } - if err := fileops.WriteFileAtomic(retentionPath, append(retention, '\n'), fileops.WorkspaceFileMode); err != nil { - return nil, fmt.Errorf("extract: record bundle retention: %w", err) - } completion := map[string]any{"pipeline": report.Pipeline, "artifact_count": report.ArtifactCount, "output_directory": bundle} if report.DebugDirectory != "" { completion["debug_directory"] = report.DebugDirectory @@ -235,7 +227,6 @@ func (extractStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (re } metadata := map[string]any{ "stage": "extract", "notarius_enabled": true, "bundle_root": bundle, - "bundle_retention_role": "session_durable", "retention_path": retentionPath, "diagnostic_path": logPath, "narratio_run_id": runID, "configuration_fingerprint": fingerprint, "direct_input": input.Metadata(), "reference_count": len(references.Identities), "references": extractReferenceMetadata(references.Identities), diff --git a/internal/stage/extract_native_test.go b/internal/stage/extract_native_test.go index 18b5732..8661638 100644 --- a/internal/stage/extract_native_test.go +++ b/internal/stage/extract_native_test.go @@ -106,10 +106,9 @@ func TestNativeExtractionPromotesRequiredLanesAndReusesCanonicalProducts(t *test t.Fatalf("output = %#v", output) } } - marker := result.Metadata["retention_path"].(string) - content, err := os.ReadFile(marker) - if err != nil || !strings.Contains(string(content), "session_durable") { - t.Fatalf("retention = %s, %v", content, err) + marker := filepath.Join(artifacts.SessionRunRootForCampaign(env.Config.Pipeline.Workspace.Root, m.Campaign, m.SessionID, m.RunID), "extract", "notarius-retention.json") + if _, err := os.Lstat(marker); !os.IsNotExist(err) { + t.Fatalf("unexpected retention marker: %v", err) } nativeRecordResult(m, result) encoded, err := json.Marshal(m) @@ -122,7 +121,7 @@ func TestNativeExtractionPromotesRequiredLanesAndReusesCanonicalProducts(t *test } *m = restored // Reuse is based on canonical files even after the original bundle disappears. - if err := os.RemoveAll(fake.Result.BundleRoot); err != nil { + if err := os.RemoveAll(artifacts.SessionRunRootForCampaign(env.Config.Pipeline.Workspace.Root, m.Campaign, m.SessionID, m.RunID)); err != nil { t.Fatal(err) } validation, err := (extractStage{}).ValidateResume(context.Background(), env, m) @@ -155,12 +154,37 @@ func TestNativeExtractionPromotesRequiredLanesAndReusesCanonicalProducts(t *test if err != nil || len(published) != 1 || published[0].LocalPath != entry.Path { t.Fatalf("publication source = %#v, %v", published, err) } - if err := os.WriteFile(entry.Path, []byte(`{"tampered":true}`), 0o644); err != nil { + original, err := os.ReadFile(entry.Path) + if err != nil { t.Fatal(err) } - validation, err = (extractStage{}).ValidateResume(context.Background(), env, m) - if err != nil || validation.Resumable { - t.Fatalf("tampered resume = %#v, %v", validation, err) + for _, change := range []string{"altered", "missing"} { + t.Run(change, func(t *testing.T) { + if change == "altered" { + err = os.WriteFile(entry.Path, []byte(`{"tampered":true}`), 0o644) + } else { + err = os.Remove(entry.Path) + } + if err != nil { + t.Fatal(err) + } + validation, err := (extractStage{}).ValidateResume(context.Background(), env, m) + if err != nil || validation.Resumable { + t.Fatalf("invalid product resume = %#v, %v", validation, err) + } + invalidCatalog := artifacts.NewArtifactCatalog() + if err := invalidCatalog.RegisterExtractionArtifacts(definitions); err != nil { + t.Fatal(err) + } + invalidCatalog.HydrateExtractionArtifacts(paths, m, definitions) + invalidEntry, ok := invalidCatalog.Lookup(artifacts.ExtractionArtifactSourceID("spells")) + if !ok || invalidEntry.Available { + t.Fatalf("invalid catalog entry = %#v", invalidEntry) + } + if err := os.WriteFile(entry.Path, original, 0o644); err != nil { + t.Fatal(err) + } + }) } } @@ -281,37 +305,6 @@ func TestNativeExtractionRejectsMappedLaneCollisions(t *testing.T) { } } -func TestNativeExtractionRetentionSurvivesRunCleanup(t *testing.T) { - env, m, fake := nativeExtractFixture(t) - result, err := (extractStage{}).Run(context.Background(), env, m) - if err != nil { - t.Fatal(err) - } - runRoot := artifacts.SessionRunRootForCampaign(env.Config.Pipeline.Workspace.Root, m.Campaign, m.SessionID, m.RunID) - if err := os.Mkdir(filepath.Join(runRoot, "other"), 0o755); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(filepath.Join(runRoot, "other", "temporary"), []byte("x"), 0o644); err != nil { - t.Fatal(err) - } - if _, err := cleanRunWithRetainedBundle(env.Config.Pipeline.Workspace.Root, runRoot, true); err != nil { - t.Fatal(err) - } - if _, err := os.Stat(filepath.Join(fake.Result.BundleRoot, "chunk-map.json")); err != nil { - t.Fatalf("retained auxiliary: %v", err) - } - if _, err := os.Stat(result.Metadata["retention_path"].(string)); err != nil { - t.Fatalf("retention marker: %v", err) - } - if _, err := os.Stat(filepath.Join(runRoot, "other")); !os.IsNotExist(err) { - t.Fatalf("ordinary run data remains: %v", err) - } - pending, err := cleanRunWithRetainedBundle(env.Config.Pipeline.Workspace.Root, runRoot, false) - if err != nil || pending { - t.Fatalf("cleanup resume = %v, %v", pending, err) - } -} - func TestNativeExtractionReportsPartialPromotionFailure(t *testing.T) { env, m, fake := nativeExtractFixture(t) env.Config.Pipeline.Notarius.Outputs = map[string]config.NotariusOutputConfig{"alpha": {LaneID: "alpha"}, "zeta": {LaneID: "zeta"}} @@ -360,29 +353,3 @@ func TestNativeExtractionThroughSubprocess(t *testing.T) { t.Fatalf("resume = %#v, %v", validation, err) } } - -func TestRetainedBundleCleanupRejectsUnsafeMarker(t *testing.T) { - env, m, _ := nativeExtractFixture(t) - result, err := (extractStage{}).Run(context.Background(), env, m) - if err != nil { - t.Fatal(err) - } - marker := result.Metadata["retention_path"].(string) - if err := os.Remove(marker); err != nil { - t.Fatal(err) - } - outside := filepath.Join(t.TempDir(), "outside") - if err := os.WriteFile(outside, []byte(`{"role":"session_durable"}`), 0o644); err != nil { - t.Fatal(err) - } - if err := os.Symlink(outside, marker); err != nil { - t.Fatal(err) - } - runRoot := artifacts.SessionRunRootForCampaign(env.Config.Pipeline.Workspace.Root, m.Campaign, m.SessionID, m.RunID) - if _, err := cleanRunWithRetainedBundle(env.Config.Pipeline.Workspace.Root, runRoot, true); err == nil { - t.Fatal("unsafe retention marker accepted") - } - if _, err := os.Stat(result.Metadata["bundle_root"].(string)); err != nil { - t.Fatalf("bundle removed after unsafe marker: %v", err) - } -}