Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(tasks): reject incomplete attachment finalization (#81774)

Alessandro Pogliaghi4c6881c6e3b8
diff --git a/products/desktop/packages/core/src/sessions/cloudArtifactService.test.ts b/products/desktop/packages/core/src/sessions/cloudArtifactService.test.ts
index 7a5fab6e30d9..9d3815971bbb 100644
--- a/products/desktop/packages/core/src/sessions/cloudArtifactService.test.ts
+++ b/products/desktop/packages/core/src/sessions/cloudArtifactService.test.ts
@@ -134,6 +134,39 @@ describe("CloudArtifactService", () => {
fetchMock.mockRestore();
});
+ it("rejects an incomplete finalization response", async () => {
+ const fetchMock = vi
+ .spyOn(globalThis, "fetch")
+ .mockResolvedValue({ ok: true } as Response);
+ const service = new CloudArtifactService(
+ vi.fn().mockResolvedValue(btoa("hello")),
+ bundleLocalSkill,
+ passthroughDeps,
+ );
+ const client = makeClient();
+ (
+ client.prepareTaskRunArtifactUploads as ReturnType<typeof vi.fn>
+ ).mockResolvedValue([
+ {
+ id: "prep-1",
+ name: "a.txt",
+ type: "user_attachment",
+ size: 5,
+ presigned_post: { url: "https://s3/upload", fields: { key: "k" } },
+ },
+ ]);
+ (
+ client.finalizeTaskRunArtifactUploads as ReturnType<typeof vi.fn>
+ ).mockResolvedValue([]);
+
+ await expect(
+ service.uploadRunAttachments(client, "task-1", "run-1", ["/tmp/a.txt"]),
+ ).rejects.toThrow(
+ "Finalized uploads do not match the selected attachments",
+ );
+ fetchMock.mockRestore();
+ });
+
it("appends edited text as an output artifact with the same name and content type", async () => {
const fetchMock = vi
.spyOn(globalThis, "fetch")
diff --git a/products/desktop/packages/core/src/sessions/cloudArtifactService.ts b/products/desktop/packages/core/src/sessions/cloudArtifactService.ts
index ea619df5679a..ddc333ad78a9 100644
--- a/products/desktop/packages/core/src/sessions/cloudArtifactService.ts
+++ b/products/desktop/packages/core/src/sessions/cloudArtifactService.ts
@@ -155,7 +155,7 @@ export class CloudArtifactService {
preparedArtifacts,
);
- return finalizedArtifacts.map((artifact) => artifact.id);
+ return this.getFinalizedArtifactIds(preparedArtifacts, finalizedArtifacts);
}
async uploadRunOutput(
@@ -224,7 +224,24 @@ export class CloudArtifactService {
preparedArtifacts,
);
- return finalizedArtifacts.map((artifact) => artifact.id);
+ return this.getFinalizedArtifactIds(preparedArtifacts, finalizedArtifacts);
+ }
+
+ private getFinalizedArtifactIds(
+ preparedArtifacts: PreparedCloudArtifact[],
+ finalizedArtifacts: FinalizedCloudArtifact[],
+ ): string[] {
+ const artifactIds = finalizedArtifacts
+ .map((artifact) => artifact.id)
+ .filter((artifactId) => artifactId.trim().length > 0);
+
+ if (artifactIds.length !== preparedArtifacts.length) {
+ throw new Error(
+ "Finalized uploads do not match the selected attachments",
+ );
+ }
+
+ return artifactIds;
}
private async loadCloudAttachments(