diff --git a/internal/controlplane/execution/workspace_lifecycle.go b/internal/controlplane/execution/workspace_lifecycle.go index df3d84f2..8895b828 100644 --- a/internal/controlplane/execution/workspace_lifecycle.go +++ b/internal/controlplane/execution/workspace_lifecycle.go @@ -309,6 +309,12 @@ func applyArtifactManifest(workspace *domain.ActivityWorkspace, manifest *domain workspace.Manifest = domain.WorkspaceManifest{} initial := make(map[string]domain.WorkspaceEntry, len(manifest.InitialSnapshot)) final := make(map[string]domain.WorkspaceEntry, len(manifest.FinalSnapshot)) + observedOutputs := make(map[string]bool, len(manifest.Files)) + for _, file := range manifest.Files { + if file.Change == domain.ArtifactCreated || file.Change == domain.ArtifactModified { + observedOutputs[file.Path] = true + } + } for _, file := range manifest.InitialSnapshot { entry := domain.WorkspaceEntry{Path: file.Path, Digest: file.Checksum, SizeBytes: file.SizeBytes} workspace.Manifest.Initial = append(workspace.Manifest.Initial, entry) @@ -319,7 +325,9 @@ func applyArtifactManifest(workspace *domain.ActivityWorkspace, manifest *domain workspace.Manifest.Final = append(workspace.Manifest.Final, entry) final[file.Path] = entry before, existed := initial[file.Path] - if existed && before.Digest == entry.Digest && before.SizeBytes == entry.SizeBytes { + // An atomic replacement is an output even when it reproduces the + // exact bytes of a file left by an earlier attempt. + if existed && before.Digest == entry.Digest && before.SizeBytes == entry.SizeBytes && !observedOutputs[file.Path] { workspace.Manifest.Inputs = append(workspace.Manifest.Inputs, entry) workspace.InputBytes += entry.SizeBytes } else { diff --git a/internal/controlplane/execution/workspace_lifecycle_test.go b/internal/controlplane/execution/workspace_lifecycle_test.go index 719c45e8..1e4d54c7 100644 --- a/internal/controlplane/execution/workspace_lifecycle_test.go +++ b/internal/controlplane/execution/workspace_lifecycle_test.go @@ -52,3 +52,27 @@ func TestApplyArtifactManifestClassifiesWorkspaceSnapshots(t *testing.T) { t.Fatalf("workspace counters=%+v", workspace) } } + +func TestApplyArtifactManifestPreservesIdenticalReplacedOutput(t *testing.T) { + entries := []domain.ArtifactSnapshotEntry{ + {Path: "input.csv", SizeBytes: 10, Checksum: "sha256:input"}, + {Path: "result.csv", SizeBytes: 20, Checksum: "sha256:result"}, + } + manifest := &domain.ArtifactManifest{ + InitialSnapshot: entries, + FinalSnapshot: entries, + Files: []domain.ArtifactObservation{ + {Path: "result.csv", Change: domain.ArtifactModified, SizeBytes: 20, Checksum: "sha256:result"}, + }, + Summary: domain.ArtifactSummary{FinalFiles: 2, OutputBytes: 20}, + } + workspace := &domain.ActivityWorkspace{} + applyArtifactManifest(workspace, manifest) + if len(workspace.Manifest.Outputs) != 1 || workspace.Manifest.Outputs[0].Path != "result.csv" || + len(workspace.Manifest.Inputs) != 1 || workspace.Manifest.Inputs[0].Path != "input.csv" { + t.Fatalf("workspace manifest=%+v", workspace.Manifest) + } + if workspace.InputBytes != 10 || workspace.OutputBytes != 20 { + t.Fatalf("workspace byte counters=%+v", workspace) + } +} diff --git a/internal/provider/artifact_observer.go b/internal/provider/artifact_observer.go index b717e018..547f01a8 100644 --- a/internal/provider/artifact_observer.go +++ b/internal/provider/artifact_observer.go @@ -23,6 +23,7 @@ type artifactFile struct { size int64 checksum string modified int64 + identity os.FileInfo } // PrepareArtifactRoot returns the declared workspace or allocates an isolated @@ -60,7 +61,7 @@ func SnapshotArtifacts(root string) (ArtifactSnapshot, error) { if err != nil { return err } - snapshot.Files[filepath.ToSlash(relative)] = artifactFile{size: info.Size(), checksum: checksum, modified: info.ModTime().UnixNano()} + snapshot.Files[filepath.ToSlash(relative)] = artifactFile{size: info.Size(), checksum: checksum, modified: info.ModTime().UnixNano(), identity: info} return nil }) return snapshot, err @@ -94,7 +95,8 @@ func ArtifactManifestFor(runID, activityID, runtime string, startedAt, finishedA case !hasCurrent: change = domain.ArtifactDeleted manifest.Summary.DeletedFiles++ - case previous.size == current.size && previous.checksum == current.checksum: + case previous.size == current.size && previous.checksum == current.checksum && + os.SameFile(previous.identity, current.identity): continue default: change = domain.ArtifactModified diff --git a/internal/provider/artifact_observer_test.go b/internal/provider/artifact_observer_test.go index 82132966..7ea3d4ce 100644 --- a/internal/provider/artifact_observer_test.go +++ b/internal/provider/artifact_observer_test.go @@ -120,3 +120,36 @@ func writeFile(t *testing.T, path, content string) { t.Fatal(err) } } + +func TestArtifactSnapshotDetectsIdenticalAtomicReplacement(t *testing.T) { + root := t.TempDir() + result := filepath.Join(root, "result.csv") + input := filepath.Join(root, "input.csv") + writeFile(t, result, "deterministic output") + writeFile(t, input, "untouched content") + + before, err := SnapshotArtifacts(root) + if err != nil { + t.Fatal(err) + } + replacement := filepath.Join(root, "attempt-output") + writeFile(t, replacement, "deterministic output") + if err := os.Rename(replacement, result); err != nil { + t.Fatal(err) + } + now := time.Now().Add(time.Hour) + if err := os.Chtimes(input, now, now); err != nil { + t.Fatal(err) + } + after, err := SnapshotArtifacts(root) + if err != nil { + t.Fatal(err) + } + manifest := ArtifactManifestFor("run", "activity", "local", 1, 2, 0, before, after) + if len(manifest.Files) != 1 || manifest.Files[0].Path != "result.csv" || manifest.Files[0].Change != domain.ArtifactModified { + t.Fatalf("expected only the atomically replaced output, got %#v", manifest.Files) + } + if before.Files["result.csv"].checksum != after.Files["result.csv"].checksum { + t.Fatal("test fixture must reproduce identical bytes") + } +}