Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): record why the desktop app cleaned up an Instant start

Richie McIlroy65bd607ff01d
diff --git a/apps/web/__tests__/unit/desktop-video-create.test.ts b/apps/web/__tests__/unit/desktop-video-create.test.ts
index 795c0aaeb..4ad18b656 100644
--- a/apps/web/__tests__/unit/desktop-video-create.test.ts
+++ b/apps/web/__tests__/unit/desktop-video-create.test.ts
@@ -124,6 +124,10 @@ vi.mock("@/lib/google-drive-storage-quota", () => ({
invalidateGoogleDriveStorageQuotaCache: vi.fn(),
}));
+vi.mock("@/lib/server-analytics", () => ({
+ trackServerEvent: vi.fn(),
+}));
+
// The live-transcription stack drags in the whole workflow graph
// (server-only modules included); these tests only care that create works.
vi.mock("@/lib/live-transcribe", () => ({
@@ -143,6 +147,7 @@ const { invalidateGoogleDriveStorageQuotaCache } = await import(
"@/lib/google-drive-storage-quota"
);
const { maybeStartLiveTranscription } = await import("@/lib/live-transcribe");
+const { trackServerEvent } = await import("@/lib/server-analytics");
function resetMockDb() {
for (const key of Object.keys(mockDb)) {
@@ -440,6 +445,74 @@ describe("DELETE /delete", () => {
expect(deletion.deleteVideo).not.toHaveBeenCalled();
expect(mockDb.delete).not.toHaveBeenCalled();
});
+
+ it("reports why the desktop app cleaned up an Instant start after deleting it", async () => {
+ mockDb.where.mockResolvedValue([
+ { video: { ...ownedVideo, source: { type: "desktopSegments" } } },
+ ]);
+ const response = await app.request(
+ "https://cap.test/delete?videoId=video-1&reason=start-failed&cause=microphone",
+ {
+ method: "DELETE",
+ headers: { "X-Cap-Desktop-Features": "googleDriveUpload,gpui" },
+ },
+ );
+ expect(response.status).toBe(200);
+ expect(await response.json()).toBe(true);
+ expect(trackServerEvent).toHaveBeenCalledExactlyOnceWith(
+ "user-1",
+ "desktop_instant_cleanup",
+ {
+ reason: "start-failed",
+ cause: "microphone",
+ app: "gpui",
+ sourceType: "desktopSegments",
+ },
+ );
+ });
+
+ it.each([
+ "https://cap.test/delete?videoId=video-1",
+ "https://cap.test/delete?videoId=video-1&reason=unknown&cause=microphone",
+ ])(
+ "deletes without reporting when no known reason is sent: %s",
+ async (url) => {
+ const response = await app.request(url, { method: "DELETE" });
+ expect(response.status).toBe(200);
+ expect(await response.json()).toBe(true);
+ expect(deletion.deleteVideo).toHaveBeenCalledExactlyOnceWith("video-1");
+ expect(trackServerEvent).not.toHaveBeenCalled();
+ },
+ );
+
+ it("drops an unknown cause and labels other desktop apps", async () => {
+ const response = await app.request(
+ "https://cap.test/delete?videoId=video-1&reason=interrupted&cause=../../etc",
+ { method: "DELETE" },
+ );
+ expect(response.status).toBe(200);
+ expect(trackServerEvent).toHaveBeenCalledExactlyOnceWith(
+ "user-1",
+ "desktop_instant_cleanup",
+ { reason: "interrupted", cause: null, app: "desktop", sourceType: null },
+ );
+ });
+
+ it("reports nothing when the deletion itself fails", async () => {
+ deletion.deleteVideo.mockReturnValue(
+ Effect.fail(
+ new StorageDomain.StorageError({
+ cause: new Error("Provider deletion failed"),
+ }),
+ ),
+ );
+ const response = await app.request(
+ "https://cap.test/delete?videoId=video-1&reason=start-failed",
+ { method: "DELETE" },
+ );
+ expect(response.status).toBe(500);
+ expect(trackServerEvent).not.toHaveBeenCalled();
+ });
});
describe("GET /create", () => {
diff --git a/apps/web/app/api/desktop/[...route]/video.ts b/apps/web/app/api/desktop/[...route]/video.ts
index 8565e429f..83ea3de6b 100644
--- a/apps/web/app/api/desktop/[...route]/video.ts
+++ b/apps/web/app/api/desktop/[...route]/video.ts
@@ -21,9 +21,11 @@ import { Effect, Option } from "effect";
import { Hono } from "hono";
import { after } from "next/server";
import { z } from "zod";
+import { readInstantCleanupReport } from "@/lib/desktop-instant-cleanup";
import { invalidateGoogleDriveStorageQuotaCache } from "@/lib/google-drive-storage-quota";
import { maybeStartLiveTranscription } from "@/lib/live-transcribe";
import { runPromise } from "@/lib/server";
+import { trackServerEvent } from "@/lib/server-analytics";
import {
GOOGLE_DRIVE_UPLOAD_FEATURE,
hasDesktopFeature,
@@ -443,6 +445,17 @@ app.delete(
result.video.storageIntegrationId,
);
+ const cleanup = readInstantCleanupReport(
+ c.req.query("reason"),
+ c.req.query("cause"),
+ hasDesktopFeature(c.req, "gpui"),
+ );
+ if (cleanup)
+ trackServerEvent(user.id, "desktop_instant_cleanup", {
+ ...cleanup,
+ sourceType: result.video.source?.type ?? null,
+ });
+
return c.json(true);
} catch (error) {
console.error("Error in video delete endpoint:", error);
diff --git a/apps/web/lib/desktop-instant-cleanup.ts b/apps/web/lib/desktop-instant-cleanup.ts
new file mode 100644
index 000000000..4591024e7
--- /dev/null
+++ b/apps/web/lib/desktop-instant-cleanup.ts
@@ -0,0 +1,35 @@
+const CLEANUP_REASONS = new Set([
+ "start-failed",
+ "start-cancelled",
+ "interrupted",
+]);
+
+const START_FAILURE_CAUSES = new Set([
+ "permission",
+ "microphone",
+ "camera",
+ "capture",
+ "storage",
+ "account",
+ "network",
+ "other",
+]);
+
+export type InstantCleanupReport = {
+ reason: string;
+ cause: string | null;
+ app: "gpui" | "desktop";
+};
+
+export function readInstantCleanupReport(
+ reason: string | undefined,
+ cause: string | undefined,
+ isGpui: boolean,
+): InstantCleanupReport | null {
+ if (!reason || !CLEANUP_REASONS.has(reason)) return null;
+ return {
+ reason,
+ cause: cause && START_FAILURE_CAUSES.has(cause) ? cause : null,
+ app: isGpui ? "gpui" : "desktop",
+ };
+}