Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion internal/controlplane/execution/workspace_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions internal/controlplane/execution/workspace_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
6 changes: 4 additions & 2 deletions internal/provider/artifact_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions internal/provider/artifact_observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Loading