Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(runtime): extract stageArchivedArtifact from RestoreMissingWorkspaceArtifacts (#472)

fx-kurumi06a2f01b56b1
diff --git a/internal/runtime/artifact_stage_test.go b/internal/runtime/artifact_stage_test.go
new file mode 100644
index 00000000..e99fbacc
--- /dev/null
+++ b/internal/runtime/artifact_stage_test.go
@@ -0,0 +1,57 @@
+package runtime
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func TestStageArchivedArtifactWritesPrivateTempFile(t *testing.T) {
+ dir := t.TempDir()
+ if err := os.MkdirAll(filepath.Join(dir, "out"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ base, err := os.OpenRoot(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer base.Close()
+ temp, err := stageArchivedArtifact(base, "out", "hello\n")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if filepath.Dir(temp) != "out" {
+ t.Fatalf("temp %q not staged under parent", temp)
+ }
+ data, err := os.ReadFile(filepath.Join(dir, temp))
+ if err != nil || string(data) != "hello\n" {
+ t.Fatalf("content = %q, %v", data, err)
+ }
+ st, err := os.Stat(filepath.Join(dir, temp))
+ if err != nil || st.Mode().Perm() != 0644 {
+ t.Fatalf("mode = %v, %v", st, err)
+ }
+}
+
+func TestStageArchivedArtifactGivesUpAfterCollisions(t *testing.T) {
+ dir := t.TempDir()
+ base, err := os.OpenRoot(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer base.Close()
+ next := artifactTempSequence.Load()
+ for i := uint64(1); i <= 10; i++ {
+ name := fmt.Sprintf(".agentflow-artifact-%d-%d", os.Getpid(), next+i)
+ if err := os.WriteFile(filepath.Join(dir, name), nil, 0600); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if _, err := stageArchivedArtifact(base, ".", "x"); err == nil || err.Error() != "cannot stage archived artifact" {
+ t.Fatalf("err = %v", err)
+ }
+ if _, err := stageArchivedArtifact(base, ".", "x"); err != nil {
+ t.Fatalf("staging after collisions: %v", err)
+ }
+}
diff --git a/internal/runtime/workspace_artifacts.go b/internal/runtime/workspace_artifacts.go
index ad9c68b1..ba503c55 100644
--- a/internal/runtime/workspace_artifacts.go
+++ b/internal/runtime/workspace_artifacts.go
@@ -70,38 +70,11 @@ func RestoreMissingWorkspaceArtifacts(root string, files []domain.WorkspaceArtif
}
// Stage privately, then link without replacing the destination. os.Root
// confines every operation even if a parent directory changes concurrently.
- var temp string
- for attempt := 0; attempt < 10; attempt++ {
- candidate := filepath.Join(parent, fmt.Sprintf(".agentflow-artifact-%d-%d", os.Getpid(), artifactTempSequence.Add(1)))
- out, err := base.OpenFile(candidate, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
- if os.IsExist(err) {
- continue
- }
- if err != nil {
- return restored, err
- }
- temp = candidate
- _, err = out.WriteString(f.Content)
- if err == nil {
- err = out.Chmod(0644)
- }
- if err == nil {
- err = out.Sync()
- }
- closeErr := out.Close()
- if err == nil {
- err = closeErr
- }
- if err != nil {
- _ = base.Remove(temp)
- return restored, err
- }
- break
- }
- if temp == "" {
- return restored, fmt.Errorf("cannot stage archived artifact")
+ temp, err := stageArchivedArtifact(base, parent, f.Content)
+ if err != nil {
+ return restored, err
}
- err := base.Link(temp, rel)
+ err = base.Link(temp, rel)
_ = base.Remove(temp)
if os.IsExist(err) {
continue
@@ -114,4 +87,37 @@ func RestoreMissingWorkspaceArtifacts(root string, files []domain.WorkspaceArtif
return restored, nil
}
+// stageArchivedArtifact writes content to a fresh private temp file under
+// parent (0644 after sync) and returns its root-relative path. It retries name
+// collisions up to 10 times; any write failure removes the partial temp file.
+func stageArchivedArtifact(base *os.Root, parent, content string) (string, error) {
+ for attempt := 0; attempt < 10; attempt++ {
+ candidate := filepath.Join(parent, fmt.Sprintf(".agentflow-artifact-%d-%d", os.Getpid(), artifactTempSequence.Add(1)))
+ out, err := base.OpenFile(candidate, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
+ if os.IsExist(err) {
+ continue
+ }
+ if err != nil {
+ return "", err
+ }
+ _, err = out.WriteString(content)
+ if err == nil {
+ err = out.Chmod(0644)
+ }
+ if err == nil {
+ err = out.Sync()
+ }
+ closeErr := out.Close()
+ if err == nil {
+ err = closeErr
+ }
+ if err != nil {
+ _ = base.Remove(candidate)
+ return "", err
+ }
+ return candidate, nil
+ }
+ return "", fmt.Errorf("cannot stage archived artifact")
+}
+
var artifactTempSequence atomic.Uint64