Skip to content

Commit 61d5aac

Browse files
fix(desktop): prevent preview annotation upload collisions
1 parent 7f3628e commit 61d5aac

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,7 +3937,7 @@ describe("PreviewManager", () => {
39373937
),
39383938
);
39393939

3940-
effectIt.effect("keeps annotation send grants isolated and updates an active pick", () =>
3940+
effectIt.effect("keeps annotation IDs and send grants isolated across tabs", () =>
39413941
withManager((manager) =>
39423942
Effect.gen(function* () {
39433943
const picked = new Map<number, (event: unknown, ...args: unknown[]) => void>();
@@ -3985,6 +3985,8 @@ describe("PreviewManager", () => {
39853985
screenshot: null,
39863986
createdAt: "2026-09-05T00:00:00.000Z",
39873987
};
3988+
// Guest counters restart after navigation and collide across tabs.
3989+
const annotationIds = new Set<string>();
39883990
for (const [tabId, id, nextGrant, expected] of [
39893991
["tab_b", 43, undefined, "attach"],
39903992
["tab_a", 42, false, "attach"],
@@ -3997,21 +3999,29 @@ describe("PreviewManager", () => {
39973999
}
39984000
picked.get(id)?.({}, annotation, null, "send");
39994001
const result = yield* Fiber.join(pick);
4000-
expect(result?.annotation.id).toBe(annotation.id);
4002+
expect(result).not.toBeNull();
4003+
expect(annotationIds.has(result!.annotation.id)).toBe(false);
4004+
annotationIds.add(result!.annotation.id);
4005+
expect(result?.annotation.comment).toBe(annotation.comment);
40014006
expect(result?.submission).toBe(expected);
40024007
}
40034008
yield* manager.registerWebview("tab_a", 44);
40044009
const replacementPick = yield* manager.pickElement("tab_a").pipe(Effect.forkChild);
40054010
yield* Effect.yieldNow;
40064011
picked.get(44)?.({}, annotation, null, "send");
4007-
expect((yield* Fiber.join(replacementPick))?.submission).toBe("send");
4012+
const replacementResult = yield* Fiber.join(replacementPick);
4013+
expect(replacementResult?.submission).toBe("send");
4014+
expect(annotationIds.has(replacementResult!.annotation.id)).toBe(false);
4015+
annotationIds.add(replacementResult!.annotation.id);
40084016
yield* manager.closeTab("tab_a");
40094017
yield* manager.createTab("tab_a");
40104018
yield* manager.registerWebview("tab_a", 44);
40114019
const reopenedPick = yield* manager.pickElement("tab_a").pipe(Effect.forkChild);
40124020
yield* Effect.yieldNow;
40134021
picked.get(44)?.({}, annotation, null, "send");
4014-
expect((yield* Fiber.join(reopenedPick))?.submission).toBe("attach");
4022+
const reopenedResult = yield* Fiber.join(reopenedPick);
4023+
expect(reopenedResult?.submission).toBe("attach");
4024+
expect(annotationIds.has(reopenedResult!.annotation.id)).toBe(false);
40154025
}),
40164026
),
40174027
);
@@ -4168,10 +4178,15 @@ describe("PreviewManager", () => {
41684178
expect(webviewSend).not.toHaveBeenCalledWith("preview:annotation-captured");
41694179
expect(secondPick.pollUnsafe()).toBeUndefined();
41704180

4171-
onPicked?.({}, { ...annotation, id: "annotation_2" }, null, "attach");
4181+
onPicked?.(
4182+
{},
4183+
{ ...annotation, id: "annotation_2", comment: "Second pick" },
4184+
null,
4185+
"attach",
4186+
);
41724187
yield* TestClock.adjust("6 seconds");
41734188
const result = yield* Fiber.join(secondPick);
4174-
expect(result?.annotation.id).toBe("annotation_2");
4189+
expect(result?.annotation.comment).toBe("Second pick");
41754190
expect(result?.submission).toBe("attach");
41764191
}),
41774192
),

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,6 +2159,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
21592159
const wc = yield* requireWebContents(tabId);
21602160
yield* cancelPickElement(tabId);
21612161
const annotationTheme = yield* Ref.get(annotationThemeRef);
2162+
// Guest counters restart on navigation and collide across tabs. Annotation
2163+
// IDs also key draft screenshots and their cached uploads in the renderer.
2164+
const annotationId = yield* crypto.randomUUIDv4.pipe(Effect.orDie);
21622165
return yield* Effect.callback<PreviewAnnotationSubmissionResult | null, PreviewManagerError>(
21632166
(resume) => {
21642167
// Declared first so cleanup can check slot ownership by identity
@@ -2231,6 +2234,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
22312234
settle(null);
22322235
return;
22332236
}
2237+
const annotation = { ...payload, id: annotationId };
22342238
const cropRect = normalizeCaptureRect(args[1]);
22352239
const submission =
22362240
args[2] === "send" && annotationSendEnabled.get(tabId) === true ? "send" : "attach";
@@ -2241,14 +2245,14 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
22412245
// flagged on the result.
22422246
Effect.match({
22432247
onFailure: (): PreviewAnnotationSubmissionResult => ({
2244-
annotation: payload,
2248+
annotation,
22452249
submission,
22462250
screenshotFailed: true,
22472251
}),
22482252
onSuccess: (screenshot): PreviewAnnotationSubmissionResult =>
22492253
screenshot === null
2250-
? { annotation: payload, submission, screenshotFailed: true }
2251-
: { annotation: { ...payload, screenshot }, submission },
2254+
? { annotation, submission, screenshotFailed: true }
2255+
: { annotation: { ...annotation, screenshot }, submission },
22522256
}),
22532257
Effect.flatMap((result) => {
22542258
// A capture that outlives its session must not touch the

0 commit comments

Comments
 (0)