Skip to content

Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.

TypeScript

fix(fuse): make a path truncate durable, and an absurd size an error

zhuangxialiec3ab86472a36
diff --git a/agent-fuse/main.go b/agent-fuse/main.go
index 5ae6ff1..8f4a43a 100644
--- a/agent-fuse/main.go
+++ b/agent-fuse/main.go
@@ -318,6 +318,20 @@ type fileNode struct {
generation uint64
}
+// The server stores a whole-file body in a single JSON PUT bounded at 34 MB
+// (`json({ limit: '34mb' })`, server/src/agents/runtime/fs-endpoints.ts), so a
+// body above that can never be written durably. Refusing it here turns a
+// guaranteed EIO at flush -- long after the agent believed the write landed --
+// into an immediate EFBIG at the syscall that asked for it.
+//
+// It also, and more urgently, bounds the allocation. The previous guards only
+// rejected sizes above maxInt, so `truncate -s 1000000000000000000` reached
+// `make([]byte, 1e18)` and panicked with "makeslice: len out of range". go-fuse
+// v2.5.1 has no recover() in its request path, so that panic killed the daemon:
+// /workspace then returned ENOTCONN for the life of the pod and every unflushed
+// dirty file in the process died with it.
+const maxFileBytes = 32 * 1024 * 1024
+
func (f *fileNode) fillAttr(a *fuse.Attr, size int64) {
a.Mode = fuse.S_IFREG | 0o644
a.Mtime = uint64(time.Now().Unix())
@@ -394,10 +408,14 @@ func (f *fileNode) Write(ctx context.Context, fh fs.FileHandle, data []byte, off
if off < 0 {
return 0, syscall.EINVAL
}
- maxInt := int(^uint(0) >> 1)
- if uint64(off) > uint64(maxInt-len(data)) {
+ // Both halves are needed: testing only the offset would compute
+ // `maxFileBytes-len(data)` negative for an oversized payload and wrap it to
+ // a huge uint64, letting the very allocation this guards slip through.
+ if len(data) > maxFileBytes || uint64(off) > uint64(maxFileBytes-len(data)) {
return 0, syscall.EFBIG
}
+ // Bound the request before hydrating: a doomed write should not also cost
+ // a remote read.
if errno := f.ensureCurrentLocked(); errno != 0 {
return 0, errno
}
@@ -422,17 +440,19 @@ func (f *fileNode) Fsync(ctx context.Context, fh fs.FileHandle, flags uint32) sy
}
func (f *fileNode) Setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut) syscall.Errno {
+ size, resizing := in.GetSize()
+ // Bound before hydrating, so an impossible size costs neither a remote read
+ // nor an allocation.
+ if resizing && size > maxFileBytes {
+ return syscall.EFBIG
+ }
+
f.mu.Lock()
if errno := f.ensureCurrentLocked(); errno != 0 {
f.mu.Unlock()
return errno
}
- if size, ok := in.GetSize(); ok {
- maxInt := uint64(^uint(0) >> 1)
- if size > maxInt {
- f.mu.Unlock()
- return syscall.EFBIG
- }
+ if resizing {
if int(size) < len(f.cachedBody) {
f.cachedBody = f.cachedBody[:int(size)]
} else if int(size) > len(f.cachedBody) {
@@ -445,6 +465,25 @@ func (f *fileNode) Setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAt
}
body := bytes.Clone(f.cachedBody)
f.mu.Unlock()
+
+ // A resize that arrives with NO file handle is a path-based truncate(2):
+ // the kernel opened nothing, so it will send neither FLUSH nor RELEASE for
+ // it and persist() would never run -- `truncate -s 5 f` reported success,
+ // reported the new size, and left the stored file untouched forever.
+ //
+ // An ftruncate(2) through an fd does carry a handle, and the kernel emits
+ // FLUSH on every close(2) of that fd -- including the closes it performs
+ // when a process is killed -- so that path keeps batching into the existing
+ // flush rather than paying a second upload. This is the only resize the
+ // kernel will never come back for, so it is the only one we push eagerly.
+ if resizing && fh == nil {
+ if errno := f.persist(); errno != 0 {
+ // persist() leaves the node dirty, so a later open+flush can still
+ // retry; the caller learns now rather than believing a lost write.
+ return errno
+ }
+ }
+
f.fillAttr(&out.Attr, int64(len(body)))
return 0
}
diff --git a/agent-fuse/main_test.go b/agent-fuse/main_test.go
index 97259be..9f41377 100644
--- a/agent-fuse/main_test.go
+++ b/agent-fuse/main_test.go
@@ -360,3 +360,156 @@ func TestFlushFailureKeepsDirtyCacheForRetry(t *testing.T) {
t.Fatalf("backend body after retry = %q, want %q", got, "local")
}
}
+
+// ─── Setattr durability and bounds ─────────────────────────────────
+//
+// A path-based truncate(2) never opens the file: the kernel sends SETATTR with
+// no file handle and issues no OPEN/FLUSH/RELEASE, so persist() -- reachable
+// only from Flush and Fsync -- never ran for it. `truncate -s 5 f` reported
+// success and the new size while the stored file kept its old contents.
+//
+// TestSetattrTruncateHydratesExistingContent looks like it covers this, but it
+// calls f.Flush() by hand immediately after Setattr. That is a step the kernel
+// performs only for an fd-based ftruncate, so the test asserts a durability the
+// real path does not have.
+
+func TestSetattrTruncateWithoutFileHandleIsDurable(t *testing.T) {
+ backend := newFuseTestBackend(map[string]string{"notes.txt": "hello world"})
+ w, closeServer := newFuseTestWorkspace(t, backend)
+ defer closeServer()
+ f := &fileNode{w: w, relPath: "notes.txt"}
+
+ var in fuse.SetAttrIn
+ in.Valid = fuse.FATTR_SIZE
+ in.Size = 5
+ var out fuse.AttrOut
+ // fh is nil: exactly what the kernel sends for `truncate -s 5 notes.txt`.
+ if errno := f.Setattr(nil, nil, &in, &out); errno != 0 {
+ t.Fatalf("Setattr() errno = %d", errno)
+ }
+ if out.Size != 5 {
+ t.Fatalf("Setattr() size = %d, want 5", out.Size)
+ }
+ // No Flush: the kernel will never send one for this operation.
+ if got := backend.body("notes.txt"); got != "hello" {
+ t.Fatalf("stored body = %q, want %q — the truncate was reported as succeeding but never uploaded", got, "hello")
+ }
+}
+
+func TestSetattrTruncateWithFileHandleStillBatchesIntoFlush(t *testing.T) {
+ // The guard rail: an ftruncate(2) does carry a handle, and the kernel emits
+ // FLUSH on close(2) of that fd, so it must keep batching. Pushing every
+ // resize eagerly would add an upload to `> file` and every open(..., 'w').
+ backend := newFuseTestBackend(map[string]string{"notes.txt": "hello world"})
+ w, closeServer := newFuseTestWorkspace(t, backend)
+ defer closeServer()
+ f := newFileNodeForTest(w, "notes.txt", "hello world")
+
+ var in fuse.SetAttrIn
+ in.Valid = fuse.FATTR_SIZE
+ in.Size = 0
+ var out fuse.AttrOut
+ if errno := f.Setattr(nil, &fileHandle{f: f}, &in, &out); errno != 0 {
+ t.Fatalf("Setattr() errno = %d", errno)
+ }
+ if got := len(backend.writes()); got != 0 {
+ t.Fatalf("uploads after ftruncate = %d, want 0 — the fd path must wait for FLUSH", got)
+ }
+ if _, errno := f.Write(nil, nil, []byte("hi"), 0); errno != 0 {
+ t.Fatalf("Write() errno = %d", errno)
+ }
+ if errno := f.Flush(nil, nil); errno != 0 {
+ t.Fatalf("Flush() errno = %d", errno)
+ }
+ if got := len(backend.writes()); got != 1 {
+ t.Fatalf("uploads after flush = %d, want exactly 1", got)
+ }
+ if got := backend.body("notes.txt"); got != "hi" {
+ t.Fatalf("stored body = %q, want %q", got, "hi")
+ }
+}
+
+func TestSetattrTruncateReportsAFailedUpload(t *testing.T) {
+ // The caller has no later flush to retry with, so it has to learn now
+ // rather than believe a write that never landed.
+ backend := newFuseTestBackend(map[string]string{"notes.txt": "hello world"})
+ backend.failWrites = true
+ w, closeServer := newFuseTestWorkspace(t, backend)
+ defer closeServer()
+ f := &fileNode{w: w, relPath: "notes.txt"}
+
+ var in fuse.SetAttrIn
+ in.Valid = fuse.FATTR_SIZE
+ in.Size = 5
+ var out fuse.AttrOut
+ if errno := f.Setattr(nil, nil, &in, &out); errno != syscall.EIO {
+ t.Fatalf("Setattr() errno = %d, want EIO when the upload fails", errno)
+ }
+ f.mu.Lock()
+ dirty := f.dirty
+ f.mu.Unlock()
+ if !dirty {
+ t.Fatalf("node was left clean after a failed upload; a later flush can no longer retry it")
+ }
+}
+
+func TestSetattrOversizedTruncateIsAnErrorNotAPanic(t *testing.T) {
+ // The old guard only rejected sizes above maxInt, so this reached
+ // make([]byte, 1e18) and panicked. go-fuse has no recover() in its request
+ // path, so the panic killed the daemon and took /workspace with it.
+ backend := newFuseTestBackend(map[string]string{"f": "x"})
+ w, closeServer := newFuseTestWorkspace(t, backend)
+ defer closeServer()
+ f := newFileNodeForTest(w, "f", "x")
+
+ var in fuse.SetAttrIn
+ in.Valid = fuse.FATTR_SIZE
+ in.Size = 1000000000000000000
+ var out fuse.AttrOut
+ if errno := f.Setattr(nil, nil, &in, &out); errno != syscall.EFBIG {
+ t.Fatalf("Setattr() errno = %d, want EFBIG", errno)
+ }
+ if got := backend.reads(); got != 0 {
+ t.Fatalf("backend reads = %d, want 0 — an impossible size must not cost a hydration", got)
+ }
+}
+
+func TestWriteAtAnAbsurdOffsetIsAnErrorNotAPanic(t *testing.T) {
+ backend := newFuseTestBackend(map[string]string{"f": "x"})
+ w, closeServer := newFuseTestWorkspace(t, backend)
+ defer closeServer()
+ f := newFileNodeForTest(w, "f", "x")
+
+ if _, errno := f.Write(nil, nil, []byte("hi"), 1000000000000000000); errno != syscall.EFBIG {
+ t.Fatalf("Write() errno = %d, want EFBIG", errno)
+ }
+}
+
+func TestWriteBeyondWhatTheServerCanStoreIsRefusedAtTheSyscall(t *testing.T) {
+ // The server caps a whole-file body at 34 MB, so a larger body could only
+ // ever fail at flush -- after the agent was told the write succeeded.
+ backend := newFuseTestBackend(map[string]string{"f": "x"})
+ w, closeServer := newFuseTestWorkspace(t, backend)
+ defer closeServer()
+ f := newFileNodeForTest(w, "f", "x")
+
+ if _, errno := f.Write(nil, nil, []byte("hi"), maxFileBytes); errno != syscall.EFBIG {
+ t.Fatalf("Write() at the ceiling errno = %d, want EFBIG", errno)
+ }
+ if n, errno := f.Write(nil, nil, []byte("hi"), maxFileBytes-2); errno != 0 || n != 2 {
+ t.Fatalf("Write() just inside the ceiling = (%d, %d), want (2, 0)", n, errno)
+ }
+}
+
+func TestWriteWithAnOversizedPayloadIsRefused(t *testing.T) {
+ // The offset half of the guard alone would wrap: maxFileBytes-len(data) is
+ // negative here, and uint64 of that is enormous.
+ backend := newFuseTestBackend(map[string]string{"f": "x"})
+ w, closeServer := newFuseTestWorkspace(t, backend)
+ defer closeServer()
+ f := newFileNodeForTest(w, "f", "x")
+
+ if _, errno := f.Write(nil, nil, make([]byte, maxFileBytes+1), 0); errno != syscall.EFBIG {
+ t.Fatalf("Write() with an oversized payload errno = %d, want EFBIG", errno)
+ }
+}