Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(blobstorage): suppress sub-second remainder chunk to prevent silent key collision (#16592)

Niklas Semmler9b955df9c289
diff --git a/worker/src/__tests__/blobStorageIntegrationProcessing.test.ts b/worker/src/__tests__/blobStorageIntegrationProcessing.test.ts
index 66391cbace..04db1da74a 100644
--- a/worker/src/__tests__/blobStorageIntegrationProcessing.test.ts
+++ b/worker/src/__tests__/blobStorageIntegrationProcessing.test.ts
@@ -60,6 +60,7 @@ import { Job } from "bullmq";
import {
handleBlobStorageIntegrationProjectJob,
BLOB_STORAGE_LAG_BUFFER_MS,
+ BLOB_STORAGE_REMAINDER_COALESCE_MS,
} from "../features/blobstorage/handleBlobStorageIntegrationProjectJob";
import { BLOB_INTEGRATION_DISABLED_METRIC } from "../features/blobstorage/isCustomerFaultError";
import {
@@ -1791,6 +1792,86 @@ describe("BlobStorageIntegrationProcessingJob", () => {
expect(timeDiff).toBeLessThan(5000); // Within 5 seconds
});
+ it("should coalesce a sub-second remainder instead of emitting a colliding chunk", async () => {
+ // Regression for the silent object-key collision: a caught-up run whose
+ // full-interval chunk ends only a sub-second before the frontier used to
+ // re-enqueue a tiny remainder chunk. Both keys truncate to the same
+ // wall-clock second, so the remainder overwrote the full window. Position
+ // lastSyncAt so the interval-capped maxTimestamp lands just below the
+ // frontier (remainder < BLOB_STORAGE_REMAINDER_COALESCE_MS): the run must
+ // be treated as caught up (schedule one interval out), not re-enqueued.
+ const { projectId } = await createOrgProjectAndApiKey();
+ s3Prefix = projectId;
+ const now = new Date();
+ const frequencyIntervalMs = 60 * 60 * 1000; // hourly
+ // gap = frontier - (minTimestamp + interval). Kept tiny (well under the
+ // coalesce threshold) so it stays below threshold even after the handler's
+ // own `now` advances a few ms past the test's.
+ const gapMs = 50;
+ const lastSyncAt = new Date(
+ now.getTime() -
+ BLOB_STORAGE_LAG_BUFFER_MS -
+ frequencyIntervalMs -
+ gapMs,
+ );
+
+ // A trace inside the full window so a real chunk is exported.
+ const trace = createTrace({
+ project_id: projectId,
+ timestamp: lastSyncAt.getTime() + frequencyIntervalMs / 2,
+ name: "Windowed Trace",
+ });
+ await createTracesCh([trace]);
+
+ await prisma.blobStorageIntegration.create({
+ data: {
+ projectId,
+ type: BlobStorageIntegrationType.S3,
+ bucketName,
+ prefix: s3Prefix,
+ accessKeyId: minioAccessKeyId,
+ secretAccessKey: encrypt(minioAccessKeySecret),
+ region: region ? region : "auto",
+ endpoint: minioEndpoint,
+ forcePathStyle:
+ env.LANGFUSE_S3_EVENT_UPLOAD_FORCE_PATH_STYLE === "true",
+ enabled: true,
+ exportFrequency: "hourly",
+ lastSyncAt,
+ compressed: false,
+ },
+ });
+
+ await handleBlobStorageIntegrationProjectJob({
+ data: { payload: { projectId } },
+ } as Job);
+
+ const updatedIntegration = await prisma.blobStorageIntegration.findUnique(
+ {
+ where: { projectId },
+ },
+ );
+
+ expect(updatedIntegration).toBeDefined();
+ if (!updatedIntegration?.nextSyncAt || !updatedIntegration?.lastSyncAt) {
+ expect.fail("nextSyncAt and lastSyncAt should be set");
+ }
+
+ // Caught up: nextSyncAt is one interval past the exported boundary, far in
+ // the future — not the near-`now` value a catch-up re-enqueue would set.
+ expect(
+ updatedIntegration.nextSyncAt.getTime() - now.getTime(),
+ ).toBeGreaterThan(frequencyIntervalMs / 2);
+
+ // lastSyncAt advances only to the interval-capped boundary; the sub-second
+ // tail up to the frontier is deferred to the next scheduled run.
+ const frontier = now.getTime() - BLOB_STORAGE_LAG_BUFFER_MS;
+ expect(updatedIntegration.lastSyncAt.getTime()).toBeLessThan(frontier);
+ expect(frontier - updatedIntegration.lastSyncAt.getTime()).toBeLessThan(
+ BLOB_STORAGE_REMAINDER_COALESCE_MS + 2000, // + handler-clock drift tolerance
+ );
+ });
+
it("should schedule normally when caught up", async () => {
const { projectId } = await createOrgProjectAndApiKey();
s3Prefix = projectId;
diff --git a/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts b/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
index 26b9060456..bab1b99f39 100644
--- a/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
+++ b/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
@@ -134,6 +134,17 @@ function resolveBlobExportFormat(
export const BLOB_STORAGE_LAG_BUFFER_MS = 20 * 60 * 1000; // 20-minute lag buffer
+// When a catch-up run lands within this distance of the frontier, treat it as
+// caught up rather than emitting a tiny trailing chunk. Object keys are truncated
+// to whole-second precision (formatBlobExportTimestamp), so a sub-second remainder
+// chunk can share a wall-clock second with the full-interval chunk it follows and
+// silently overwrite that window's files and manifest. Suppressing the remainder
+// removes the collision at its source; the deferred tail is picked up by the next
+// scheduled run (its minTimestamp = lastSyncAt already covers it). Must be >= 1000ms
+// (the key granularity) and well below any frequencyInterval so no meaningful data
+// is deferred.
+export const BLOB_STORAGE_REMAINDER_COALESCE_MS = 1000;
+
export async function* enrichObservationStream(
stream: AsyncGenerator<Record<string, unknown>>,
projectId: string,
@@ -1451,8 +1462,14 @@ export const handleBlobStorageIntegrationProjectJob = async (
}
}
- // Determine if we've caught up with present-day data
- const caughtUp = maxTimestamp.getTime() >= uncappedMaxTimestamp.getTime();
+ // Determine if we've caught up with present-day data. A remainder below
+ // BLOB_STORAGE_REMAINDER_COALESCE_MS is treated as caught up so we never emit
+ // a sub-second trailing chunk that would collide with the full-interval chunk
+ // on the same second-precision object key (see the constant's doc comment).
+ const remainderMs = uncappedMaxTimestamp.getTime() - maxTimestamp.getTime();
+ const caughtUp =
+ maxTimestamp.getTime() >= uncappedMaxTimestamp.getTime() ||
+ remainderMs < BLOB_STORAGE_REMAINDER_COALESCE_MS;
let nextSyncAt: Date;
if (caughtUp) {