Skip to content

The open-source app everyone uses to manage agents at work

fix: return empty read instead of past-EOF range when log reader is caught up (#13592)

Devin Foleyde9414dd8b08
diff --git a/server/src/services/run-log-store.test.ts b/server/src/services/run-log-store.test.ts
index 276321c7db..ab12ae17b3 100644
--- a/server/src/services/run-log-store.test.ts
+++ b/server/src/services/run-log-store.test.ts
@@ -33,6 +33,14 @@ function createMemoryProvider() {
err.name = "NoSuchKey";
throw err;
}
+ // Real S3 answers 416 when the range starts at or past EOF — the mock
+ // must too, or caught-up-reader regressions (PAPERCLIP-BACKEND-9) pass
+ // silently with an empty slice.
+ if (input.range && (input.range.start >= buf.length || input.range.start > input.range.end)) {
+ const err = new Error("Invalid range") as Error & { name: string };
+ err.name = "InvalidRange";
+ throw err;
+ }
const slice = input.range ? buf.subarray(input.range.start, input.range.end + 1) : buf;
return { stream: Readable.from(slice), contentLength: slice.length };
},
@@ -122,6 +130,39 @@ describe("createDurableRunLogStore", () => {
expect(tail.nextOffset).toBeUndefined();
});
+ it("S3 fallback returns an empty read (no InvalidRange) when the reader is fully caught up", async () => {
+ // Regression test for PAPERCLIP-BACKEND-9: a poller that had consumed the
+ // whole log kept polling at offset === total after the pod rolled; the S3
+ // path clamped end up to start and requested `bytes=total-total`, which
+ // S3 rejects with 416 InvalidRange -> 500.
+ const { provider } = createMemoryProvider();
+ const store = createDurableRunLogStore({ basePath: baseDir, s3: { provider, keyPrefix: "p" } });
+ const handle = await store.begin(begin);
+ await store.append(handle, { stream: "stdout", chunk: "0123456789", ts: "t" });
+ await store.finalize(handle);
+ const full = await store.read(handle);
+ const total = Buffer.byteLength(full.content, "utf8");
+ await fs.rm(baseDir, { recursive: true, force: true }); // force S3 path
+ const caughtUp = await store.read(handle, { offset: total, limitBytes: 100 });
+ expect(caughtUp.content).toBe("");
+ expect(caughtUp.nextOffset).toBeUndefined();
+ // Past-EOF offsets clamp to the same empty read.
+ const pastEof = await store.read(handle, { offset: total + 50, limitBytes: 100 });
+ expect(pastEof.content).toBe("");
+ expect(pastEof.nextOffset).toBeUndefined();
+ });
+
+ it("local read at offset === size returns an empty read with undefined nextOffset", async () => {
+ const store = createDurableRunLogStore({ basePath: baseDir });
+ const handle = await store.begin(begin);
+ await store.append(handle, { stream: "stdout", chunk: "abc", ts: "t" });
+ const full = await store.read(handle);
+ const total = Buffer.byteLength(full.content, "utf8");
+ const caughtUp = await store.read(handle, { offset: total, limitBytes: 100 });
+ expect(caughtUp.content).toBe("");
+ expect(caughtUp.nextOffset).toBeUndefined();
+ });
+
it("falls back to S3 when the local file vanishes between stat() and open (TOCTOU race)", async () => {
const { provider } = createMemoryProvider();
const store = createDurableRunLogStore({ basePath: baseDir, s3: { provider, keyPrefix: "run-logs" } });
diff --git a/server/src/services/run-log-store.ts b/server/src/services/run-log-store.ts
index 90f7f975fe..f07ede9abd 100644
--- a/server/src/services/run-log-store.ts
+++ b/server/src/services/run-log-store.ts
@@ -209,8 +209,11 @@ export function createDurableRunLogStore(options: DurableRunLogStoreOptions): Ru
const stat = await fs.stat(filePath).catch(() => null);
if (!stat) return null;
const start = Math.max(0, Math.min(offset, stat.size));
- const end = Math.max(start, Math.min(start + limitBytes - 1, stat.size - 1));
- if (start > end) return { content: "", nextOffset: start };
+ // No lower clamp to `start`: when the reader is fully caught up
+ // (offset === size) that clamp made end === start and produced a
+ // 1-byte-past-EOF range instead of an empty read.
+ const end = Math.min(start + limitBytes - 1, stat.size - 1);
+ if (start > end) return { content: "", nextOffset: start < stat.size ? start : undefined };
const chunks: Buffer[] = [];
try {
@@ -243,8 +246,12 @@ export function createDurableRunLogStore(options: DurableRunLogStoreOptions): Ru
if (!head.exists) throw notFound("Run log not found");
const total = head.contentLength ?? 0;
const start = Math.max(0, Math.min(offset, total));
- const end = Math.max(start, Math.min(start + limitBytes - 1, total - 1));
- if (start > end || total === 0) return { content: "", nextOffset: start < total ? start : undefined };
+ // Unlike local file streams, S3 rejects a range that starts at or past
+ // EOF with 416 InvalidRange, so a caught-up reader (offset === total)
+ // must short-circuit to an empty read instead of clamping end up to
+ // start and requesting `bytes=total-total`.
+ const end = Math.min(start + limitBytes - 1, total - 1);
+ if (total === 0 || start > end) return { content: "", nextOffset: start < total ? start : undefined };
const result = await s3.provider.getObject({ objectKey: key, range: { start, end } });
const chunks: Buffer[] = [];
diff --git a/server/src/services/workspace-operation-log-store.ts b/server/src/services/workspace-operation-log-store.ts
index 379dab62d2..a285ce39f7 100644
--- a/server/src/services/workspace-operation-log-store.ts
+++ b/server/src/services/workspace-operation-log-store.ts
@@ -61,10 +61,13 @@ function createLocalFileWorkspaceOperationLogStore(basePath: string): WorkspaceO
if (!stat) throw notFound("Workspace operation log not found");
const start = Math.max(0, Math.min(offset, stat.size));
- const end = Math.max(start, Math.min(start + limitBytes - 1, stat.size - 1));
+ // No lower clamp to `start`: when the reader is fully caught up
+ // (offset === size) that clamp made end === start and produced a
+ // 1-byte-past-EOF range instead of an empty read.
+ const end = Math.min(start + limitBytes - 1, stat.size - 1);
if (start > end) {
- return { content: "", nextOffset: start };
+ return { content: "", nextOffset: start < stat.size ? start : undefined };
}
const chunks: Buffer[] = [];