Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(sandbox): a failed artifact copy no longer leaves a truncated file

shukieshahb15e6c7b5824
diff --git a/beacon-sandbox/sandbox/localexec.go b/beacon-sandbox/sandbox/localexec.go
index e12acf48..77000f72 100644
--- a/beacon-sandbox/sandbox/localexec.go
+++ b/beacon-sandbox/sandbox/localexec.go
@@ -300,8 +300,24 @@ func copyFile(src, dst string) error {
return err
}
if _, err := io.Copy(out, in); err != nil {
- out.Close()
+ // Both errors are reported, and the partial file is removed. A close failure can itself
+ // explain the copy failure -- a full disk surfaces on either call depending on buffering --
+ // which is the same reasoning image/artifacts.go already applies to its download. Leaving
+ // the truncated destination behind is the part that actually matters: for Get() that is a
+ // short runtime.jsonl in the run directory, which the check layer would judge as a corrupt
+ // log and report as a Beacon defect caused by a harness copy failure.
+ closeErr := out.Close()
+ _ = os.Remove(dst)
+ if closeErr != nil {
+ return fmt.Errorf("%w (and closing %s failed: %v)", err, dst, closeErr)
+ }
return err
}
- return out.Close()
+ // Checked, not discarded: this handle was written through, so a close failure can be the first
+ // report of data that never reached disk.
+ if err := out.Close(); err != nil {
+ _ = os.Remove(dst)
+ return fmt.Errorf("close %s: %w", dst, err)
+ }
+ return nil
}
diff --git a/beacon-sandbox/sandbox/localexec_test.go b/beacon-sandbox/sandbox/localexec_test.go
index 40029b0f..91728cd0 100644
--- a/beacon-sandbox/sandbox/localexec_test.go
+++ b/beacon-sandbox/sandbox/localexec_test.go
@@ -135,6 +135,31 @@ func TestExecCapturesBothStreams(t *testing.T) {
}
}
+// A failed copy must not leave a truncated file behind. For Get() the destination is the run
+// directory's runtime.jsonl, and a short log there is judged by the check layer as a corrupt
+// log -- reporting a Beacon defect that was really a harness copy failure.
+//
+// The failure is provoked by copying from a directory: opening one for reading succeeds on Unix,
+// and the read then fails, which is a genuine mid-copy error rather than a mocked one.
+func TestAFailedCopyLeavesNoPartialFile(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("opening a directory for reading fails outright on Windows, so the copy never starts")
+ }
+ dir := t.TempDir()
+ srcDir := filepath.Join(dir, "a-directory")
+ if err := os.Mkdir(srcDir, 0o700); err != nil {
+ t.Fatal(err)
+ }
+ dst := filepath.Join(dir, "out.jsonl")
+
+ if err := copyFile(srcDir, dst); err == nil {
+ t.Fatal("copying from a directory must fail")
+ }
+ if _, err := os.Stat(dst); !os.IsNotExist(err) {
+ t.Errorf("a failed copy must not leave %s behind (stat err: %v)", dst, err)
+ }
+}
+
// Snapshot must fail rather than quietly succeed. A caller that believes it snapshotted, and did
// not, would reuse a stale environment and attribute the result to the wrong build.
func TestSnapshotIsRefusedRatherThanFaked(t *testing.T) {