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): split checkpoint entry header and file copy out of CreateWorkspaceCheckpointTo (#520)

fx-kurumif44369861cfc
diff --git a/internal/runtime/checkpoint_create_entry_test.go b/internal/runtime/checkpoint_create_entry_test.go
new file mode 100644
index 00000000..6f189e16
--- /dev/null
+++ b/internal/runtime/checkpoint_create_entry_test.go
@@ -0,0 +1,97 @@
+package runtime
+
+import (
+ "bytes"
+ "net"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+func TestCheckpointEntryHeaderNormalizesEntries(t *testing.T) {
+ root := t.TempDir()
+ if err := os.MkdirAll(filepath.Join(root, "dir"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(root, "dir", "file.txt"), []byte("hello"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Symlink("/outside/target", filepath.Join(root, "link")); err != nil {
+ t.Fatal(err)
+ }
+ base, err := os.OpenRoot(root)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer base.Close()
+
+ for _, tc := range []struct {
+ path string
+ linkname string
+ size int64
+ }{
+ {path: "dir"},
+ {path: filepath.Join("dir", "file.txt"), size: 5},
+ {path: "link", linkname: "/outside/target"},
+ } {
+ info, err := os.Lstat(filepath.Join(root, tc.path))
+ if err != nil {
+ t.Fatal(err)
+ }
+ header, err := checkpointEntryHeader(base, tc.path, info)
+ if err != nil {
+ t.Fatalf("%s: %v", tc.path, err)
+ }
+ if header.Name != filepath.ToSlash(tc.path) || header.Linkname != tc.linkname || header.Size != tc.size {
+ t.Fatalf("%s: unexpected header %+v", tc.path, header)
+ }
+ if header.Uid != 0 || header.Gid != 0 || header.Uname != "" || header.Gname != "" {
+ t.Fatalf("%s: ownership not cleared: %+v", tc.path, header)
+ }
+ }
+}
+
+func TestCheckpointEntryHeaderRejectsSpecialFiles(t *testing.T) {
+ root := t.TempDir()
+ listener, err := net.Listen("unix", filepath.Join(root, "sock"))
+ if err != nil {
+ t.Skipf("unix socket unavailable: %v", err)
+ }
+ defer listener.Close()
+ base, err := os.OpenRoot(root)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer base.Close()
+ info, err := os.Lstat(filepath.Join(root, "sock"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err = checkpointEntryHeader(base, "sock", info); err == nil || !strings.Contains(err.Error(), "unsupported checkpoint file: sock") {
+ t.Fatalf("expected unsupported file error, got %v", err)
+ }
+}
+
+func TestCopyCheckpointFileCopiesDeclaredSize(t *testing.T) {
+ root := t.TempDir()
+ if err := os.WriteFile(filepath.Join(root, "a.txt"), []byte("payload"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ base, err := os.OpenRoot(root)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer base.Close()
+ var out bytes.Buffer
+ if err = copyCheckpointFile(base, &out, "a.txt", 7); err != nil || out.String() != "payload" {
+ t.Fatalf("copy = %q, %v", out.String(), err)
+ }
+ if err = copyCheckpointFile(base, &out, "missing.txt", 1); err == nil {
+ t.Fatal("expected error for missing file")
+ }
+ out.Reset()
+ if err = copyCheckpointFile(base, &out, "a.txt", 100); err == nil {
+ t.Fatal("expected short-file error when declared size exceeds content")
+ }
+}
diff --git a/internal/runtime/workspace_checkpoint.go b/internal/runtime/workspace_checkpoint.go
index 4b5d4c3d..408eb6aa 100644
--- a/internal/runtime/workspace_checkpoint.go
+++ b/internal/runtime/workspace_checkpoint.go
@@ -106,27 +106,10 @@ func CreateWorkspaceCheckpointTo(root string, destination io.Writer) error {
if err != nil {
return err
}
- link := ""
- if info.Mode()&os.ModeSymlink != 0 {
- // Links are archived verbatim, including absolute or external targets
- // that a repository checkout can legitimately contain. Rejecting them
- // left such workspaces unsaved and therefore never reclaimed.
- link, err = base.Readlink(path)
- if err != nil {
- return err
- }
- } else if !info.IsDir() && !info.Mode().IsRegular() {
- return fmt.Errorf("unsupported checkpoint file: %s", path)
- }
- header, err := tar.FileInfoHeader(info, link)
+ header, err := checkpointEntryHeader(base, path, info)
if err != nil {
return err
}
- header.Name = filepath.ToSlash(path)
- header.Uid = 0
- header.Gid = 0
- header.Uname = ""
- header.Gname = ""
expanded += header.Size
if expanded > workspaceCheckpointExpandedMax {
return fmt.Errorf("workspace checkpoint exceeds expanded size limit")
@@ -138,18 +121,7 @@ func CreateWorkspaceCheckpointTo(root string, destination io.Writer) error {
return fmt.Errorf("external Git worktree metadata cannot be checkpointed: %s", path)
}
if info.Mode().IsRegular() {
- file, err := base.Open(path)
- if err != nil {
- return err
- }
- _, copyErr := io.CopyN(writer, file, header.Size)
- closeErr := file.Close()
- if copyErr != nil {
- return copyErr
- }
- if closeErr != nil {
- return closeErr
- }
+ return copyCheckpointFile(base, writer, path, header.Size)
}
return nil
})
@@ -166,6 +138,52 @@ func CreateWorkspaceCheckpointTo(root string, destination io.Writer) error {
}
return nil
}
+
+// checkpointEntryHeader builds the tar header for one workspace entry: symlink
+// targets are recorded verbatim, entries other than directories, regular files
+// and symlinks are rejected, and ownership is cleared so archives do not depend
+// on the host's users.
+func checkpointEntryHeader(base *os.Root, path string, info fs.FileInfo) (*tar.Header, error) {
+ link := ""
+ if info.Mode()&os.ModeSymlink != 0 {
+ // Links are archived verbatim, including absolute or external targets
+ // that a repository checkout can legitimately contain. Rejecting them
+ // left such workspaces unsaved and therefore never reclaimed.
+ var err error
+ link, err = base.Readlink(path)
+ if err != nil {
+ return nil, err
+ }
+ } else if !info.IsDir() && !info.Mode().IsRegular() {
+ return nil, fmt.Errorf("unsupported checkpoint file: %s", path)
+ }
+ header, err := tar.FileInfoHeader(info, link)
+ if err != nil {
+ return nil, err
+ }
+ header.Name = filepath.ToSlash(path)
+ header.Uid = 0
+ header.Gid = 0
+ header.Uname = ""
+ header.Gname = ""
+ return header, nil
+}
+
+// copyCheckpointFile streams size bytes of the regular file at path into
+// writer, reporting a copy failure before a close failure.
+func copyCheckpointFile(base *os.Root, writer io.Writer, path string, size int64) error {
+ file, err := base.Open(path)
+ if err != nil {
+ return err
+ }
+ _, copyErr := io.CopyN(writer, file, size)
+ closeErr := file.Close()
+ if copyErr != nil {
+ return copyErr
+ }
+ return closeErr
+}
+
func RestoreWorkspaceCheckpoint(root string, content []byte) error {
if len(content) == 0 || len(content) > WorkspaceCheckpointMaxBytes {
return fmt.Errorf("invalid workspace checkpoint size")