-
Notifications
You must be signed in to change notification settings - Fork 750
Protect recordings from empty captures and stale deletions #7444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { beforeEach, expect, it, vi } from "vitest"; | ||
|
|
||
| import { discardEmptyAutomaticCapture } from "./empty-automatic-capture"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| speech: vi.fn(), | ||
| remove: vi.fn(), | ||
| execute: vi.fn(), | ||
| empty: vi.fn(), | ||
| flush: vi.fn(), | ||
| })); | ||
| vi.mock("@anlg/plugin-fs-sync", () => ({ | ||
| commands: { | ||
| audioHasSpeech: mocks.speech, | ||
| audioDelete: mocks.remove, | ||
| }, | ||
| })); | ||
| vi.mock("~/db", () => ({ liveQueryClient: { execute: mocks.execute } })); | ||
| vi.mock("~/session/queries", () => ({ isSessionEmpty: mocks.empty })); | ||
| vi.mock("~/session-sharing/editor-activity", () => ({ | ||
| flushCanonicalSessionEditorChanges: mocks.flush, | ||
| })); | ||
| vi.mock("~/session/audio-operations", () => ({ | ||
| enqueueSessionAudioOperation: (_: string, operation: () => unknown) => | ||
| operation(), | ||
| })); | ||
|
|
||
| const input = { | ||
| sessionId: "meeting", | ||
| automatic: true, | ||
| preserveExistingAudio: false, | ||
| preserveExistingTranscript: false, | ||
| initialTitle: "Standup", | ||
| transcriptTouched: false, | ||
| transcriptionComplete: true, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetAllMocks(); | ||
| mocks.speech.mockResolvedValue({ status: "ok", data: false }); | ||
| mocks.remove.mockResolvedValue({ status: "ok", data: true }); | ||
| mocks.execute.mockResolvedValue([{ title: "Standup", has_attachments: 0 }]); | ||
| mocks.empty.mockResolvedValue(true); | ||
| mocks.flush.mockResolvedValue(undefined); | ||
| }); | ||
|
|
||
| it.each([0, false])( | ||
| "discards silent automatic audio when the attachment flag is %j", | ||
| async (hasAttachments) => { | ||
| mocks.execute.mockResolvedValue([ | ||
| { title: "Standup", has_attachments: hasAttachments }, | ||
| ]); | ||
| expect(await discardEmptyAutomaticCapture(input)).toBe(true); | ||
| expect(mocks.remove).toHaveBeenCalledWith("meeting"); | ||
| expect(mocks.execute).toHaveBeenCalledTimes(1); | ||
| expect(mocks.execute.mock.calls[0][0]).toMatch(/^SELECT /); | ||
| }, | ||
| ); | ||
|
|
||
| it.each([ | ||
| { automatic: false }, | ||
| { preserveExistingAudio: true }, | ||
| { preserveExistingTranscript: true }, | ||
| { transcriptTouched: true }, | ||
| { transcriptionComplete: false }, | ||
| { initialTitle: undefined }, | ||
| ])("keeps captures that cannot safely be discarded: %j", async (override) => { | ||
| expect(await discardEmptyAutomaticCapture({ ...input, ...override })).toBe( | ||
| false, | ||
| ); | ||
| expect(mocks.speech).not.toHaveBeenCalled(); | ||
| expect(mocks.remove).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { status: "ok", data: true }, | ||
| { status: "error", error: "decoder failed" }, | ||
| ])("keeps speech and uncertain analysis: %j", async (result) => { | ||
| mocks.speech.mockResolvedValue(result); | ||
| expect(await discardEmptyAutomaticCapture(input)).toBe(false); | ||
| expect(mocks.remove).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("keeps recordings when the user adds notes", async () => { | ||
| mocks.empty.mockResolvedValue(false); | ||
| expect(await discardEmptyAutomaticCapture(input)).toBe(false); | ||
| expect(mocks.remove).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each([1, true, null, undefined, "0"])( | ||
| "keeps audio when the attachment flag is present or uncertain: %j", | ||
| async (hasAttachments) => { | ||
| mocks.execute.mockResolvedValue([ | ||
| { title: "Standup", has_attachments: hasAttachments }, | ||
| ]); | ||
| expect(await discardEmptyAutomaticCapture(input)).toBe(false); | ||
| expect(mocks.remove).not.toHaveBeenCalled(); | ||
| }, | ||
| ); | ||
|
|
||
| it("flushes edits made during audio analysis before deciding whether to discard", async () => { | ||
| mocks.speech.mockImplementation(async () => { | ||
| mocks.flush.mockImplementation(async () => { | ||
| mocks.empty.mockResolvedValue(false); | ||
| }); | ||
| return { status: "ok", data: false }; | ||
| }); | ||
| expect(await discardEmptyAutomaticCapture(input)).toBe(false); | ||
| expect(mocks.remove).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each([{ rows: [{ title: "Renamed by the user" }] }, { rows: [] }])( | ||
| "keeps renamed or deleted sessions", | ||
| async ({ rows }) => { | ||
| mocks.execute.mockResolvedValue(rows); | ||
| expect(await discardEmptyAutomaticCapture(input)).toBe(false); | ||
| expect(mocks.remove).not.toHaveBeenCalled(); | ||
| }, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { commands as fsSyncCommands } from "@anlg/plugin-fs-sync"; | ||
|
|
||
| import { liveQueryClient } from "~/db"; | ||
| import { flushCanonicalSessionEditorChanges } from "~/session-sharing/editor-activity"; | ||
| import { enqueueSessionAudioOperation } from "~/session/audio-operations"; | ||
| import { isSessionEmpty } from "~/session/queries"; | ||
|
|
||
| export async function discardEmptyAutomaticCapture({ | ||
| sessionId, | ||
| automatic, | ||
| preserveExistingAudio, | ||
| preserveExistingTranscript, | ||
| initialTitle, | ||
| transcriptTouched, | ||
| transcriptionComplete, | ||
| }: { | ||
| sessionId: string; | ||
| automatic: boolean; | ||
| preserveExistingAudio: boolean; | ||
| preserveExistingTranscript: boolean; | ||
| initialTitle: string | undefined; | ||
| transcriptTouched: boolean; | ||
| transcriptionComplete: boolean; | ||
| }): Promise<boolean> { | ||
| if ( | ||
| !automatic || | ||
| preserveExistingAudio || | ||
| preserveExistingTranscript || | ||
| initialTitle === undefined || | ||
| transcriptTouched || | ||
| !transcriptionComplete | ||
| ) { | ||
| return false; | ||
| } | ||
| try { | ||
| await flushCanonicalSessionEditorChanges(sessionId); | ||
| return await enqueueSessionAudioOperation(sessionId, async () => { | ||
| const speech = await fsSyncCommands.audioHasSpeech(sessionId); | ||
| if (speech.status !== "ok" || speech.data) return false; | ||
| await flushCanonicalSessionEditorChanges(sessionId); | ||
| const [session] = await liveQueryClient.execute<{ | ||
| title: string; | ||
| has_attachments: boolean | number; | ||
| }>( | ||
| `SELECT title, EXISTS ( | ||
| SELECT 1 FROM session_attachments | ||
| WHERE session_id = sessions.id AND deleted_at IS NULL | ||
| ) AS has_attachments | ||
| FROM sessions WHERE id = ? AND deleted_at IS NULL`, | ||
| [sessionId], | ||
| ); | ||
| if ( | ||
| !session || | ||
| session.title !== initialTitle || | ||
| (session.has_attachments !== 0 && session.has_attachments !== false) || | ||
| !(await isSessionEmpty(sessionId)) | ||
| ) { | ||
| return false; | ||
| } | ||
| // Only remove this device's un-catalogued audio. The calendar note and | ||
| // anything another device has contributed stay in the shared database. | ||
| const result = await fsSyncCommands.audioDelete(sessionId); | ||
| return result.status === "ok" && result.data; | ||
| }); | ||
| } catch (error) { | ||
| console.warn( | ||
| "[listener] keeping automatic capture after an inconclusive activity check", | ||
| error, | ||
| ); | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.