Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions electron/ai-edition/document-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,21 @@ export class DocumentService {
}

/** Every project that parses, unrelinked and unannounced: a listing, not an open. */
private async readAllProjects(): Promise<AxcutDocument[]> {
async readAllProjects(): Promise<AxcutDocument[]> {
return (await this.readAllProjectsReport()).documents;
}

/**
* `readAllProjects`, plus the project files it skipped. A caller deciding whether no project
* uses a file needs those: a project that does not parse may still be using it.
*/
async readAllProjectsReport(): Promise<{ documents: AxcutDocument[]; unreadable: string[] }> {
await this.ensureProjectsDir();
const entries = await fs.readdir(this.projectsRoot);
// ensureProjectsDir (above) already migrated any legacy `.axcut` files.
const projectFiles = entries.filter((name) => name.endsWith(PROJECT_FILE_EXTENSION));
const documents: AxcutDocument[] = [];
const unreadable: string[] = [];
for (const name of projectFiles) {
const filePath = path.join(this.projectsRoot, name);
try {
Expand All @@ -399,9 +408,10 @@ export class DocumentService {
// ponytail: skip unreadable files rather than failing the whole list.
// A future migration pass can recover them.
console.warn(`[ai-edition] failed to read ${filePath}:`, error);
unreadable.push(filePath);
}
}
return documents;
return { documents, unreadable };
}

/**
Expand Down
140 changes: 140 additions & 0 deletions electron/ai-edition/project-recordings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { DocumentService } from "./document-service";
import { findProjectRecordings, removeRecordingSets } from "./project-recordings";

async function touch(filePath: string, content = "x"): Promise<string> {
await fs.writeFile(filePath, content);
return filePath;
}

describe("project recordings", () => {
let root: string;
let recordingsDir: string;
let elsewhere: string;
let documents: DocumentService;

beforeEach(async () => {
root = await fs.mkdtemp(path.join(os.tmpdir(), "openscreen-project-recordings-"));
recordingsDir = path.join(root, "recordings");
elsewhere = path.join(root, "Desktop");
await fs.mkdir(recordingsDir);
await fs.mkdir(elsewhere);
documents = new DocumentService(path.join(root, "projects"), root);
});

afterEach(async () => {
await fs.rm(root, { recursive: true, force: true });
});

async function projectWith(title: string, ...videoPaths: string[]): Promise<string> {
const doc = await documents.createProject(title);
for (const videoPath of videoPaths) {
await documents.addAsset(doc.project.id, {
path: videoPath,
label: path.basename(videoPath),
});
}
return doc.project.id;
}

it("owns every file recorded beside the screen video", async () => {
const video = await touch(path.join(recordingsDir, "recording-12.mp4"), "12345");
const companions = [
await touch(`${video}.cursor.json`),
await touch(path.join(recordingsDir, "recording-12.session.json")),
await touch(path.join(recordingsDir, "recording-12-webcam.webm")),
];
await touch(path.join(recordingsDir, "recording-123.mp4"));
const id = await projectWith("Mine", video);

const { owned, keptFor } = await findProjectRecordings(
id,
await documents.readAllProjectsReport(),
[recordingsDir],
);

expect(owned).toHaveLength(1);
expect(owned[0].files.sort()).toEqual([video, ...companions].sort());
expect(owned[0].sizeBytes).toBe(5 + 3);
expect(keptFor).toEqual([]);
});

it("keeps a recording another project uses, and names that project", async () => {
const shared = await touch(path.join(recordingsDir, "recording-1.mp4"));
const own = await touch(path.join(recordingsDir, "recording-2.mp4"));
const id = await projectWith("Mine", shared, own);
await projectWith("Demo v2", shared);

const { owned, keptFor } = await findProjectRecordings(
id,
await documents.readAllProjectsReport(),
[recordingsDir],
);

expect(owned.map((set) => set.stem)).toEqual(["recording-2"]);
expect(keptFor).toEqual(["Demo v2"]);
});

it("keeps every recording while a project file does not parse", async () => {
const video = await touch(path.join(recordingsDir, "recording-9.mp4"));
const id = await projectWith("Mine", video);
// Could be a project using recording-9: a newer schema, or a save cut short.
await touch(path.join(root, "projects", "broken.openscreen"), "{ not json");

const { owned, keptFor, unreadable } = await findProjectRecordings(
id,
await documents.readAllProjectsReport(),
[recordingsDir],
);

expect(owned).toEqual([]);
expect(keptFor).toEqual([]);
expect(unreadable).toBe(1);
});

it("reports no unreadable projects when it has no recordings to keep", async () => {
const imported = await touch(path.join(elsewhere, "clip.mp4"));
const id = await projectWith("Imported", imported);
await touch(path.join(root, "projects", "broken.openscreen"), "{ not json");

const { unreadable } = await findProjectRecordings(
id,
await documents.readAllProjectsReport(),
[recordingsDir],
);

expect(unreadable).toBe(0);
});

it("never owns a video outside the recordings folders", async () => {
const imported = await touch(path.join(elsewhere, "recording-5.mp4"));
const id = await projectWith("Imported", imported);

const { owned } = await findProjectRecordings(id, await documents.readAllProjectsReport(), [
recordingsDir,
]);

expect(owned).toEqual([]);
});

it("trashes each file, or removes it for good when asked", async () => {
const video = await touch(path.join(recordingsDir, "recording-7.mp4"));
const cursor = await touch(`${video}.cursor.json`);
const id = await projectWith("Mine", video);
const { owned } = await findProjectRecordings(id, await documents.readAllProjectsReport(), [
recordingsDir,
]);

const trashItem = vi.fn(async (_filePath: string) => {});
await removeRecordingSets(owned, "trash", trashItem);
expect(trashItem.mock.calls.map(([p]) => p).sort()).toEqual([video, cursor].sort());
await expect(fs.access(video)).resolves.toBeUndefined();

await removeRecordingSets(owned, "permanent", trashItem);
await expect(fs.access(video)).rejects.toThrow();
await expect(fs.access(cursor)).rejects.toThrow();
});
});
137 changes: 137 additions & 0 deletions electron/ai-edition/project-recordings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import fs from "node:fs/promises";
import path from "node:path";
import type { AxcutDocument } from "../../src/lib/ai-edition/schema";

const RECORDING_STEM = /^recording-\d+/;

/** One take: the screen video and every file recorded beside it (cursor, session, camera). */
export interface RecordingSet {
dir: string;
stem: string;
files: string[];
sizeBytes: number;
}

export interface ProjectRecordings {
owned: RecordingSet[];
/** Titles of the other projects that use some of this project's recordings. */
keptFor: string[];
/**
* Project files that did not parse. Any of them may use these recordings, so while there
* are some the project owns none: its recordings are all kept.
*/
unreadable: number;
}

export type RecordingsDisposal = "keep" | "trash" | "permanent";

function documentMediaPaths(doc: AxcutDocument): string[] {
return doc.assets.flatMap((asset) =>
[asset.originalPath, asset.proxyPath, asset.waveformPath, asset.cameraTrack?.sourcePath].filter(
(p): p is string => typeof p === "string" && p.length > 0,
),
);
}

// `path.relative` compares case-insensitively on Windows, which a string compare would not.
function sameDir(a: string, b: string): boolean {
return path.relative(path.resolve(a), path.resolve(b)) === "";
}

function recordingKey(filePath: string, recordingsDirs: readonly string[]): string | null {
const dir = path.dirname(path.resolve(filePath));
const managedDir = recordingsDirs.find((candidate) => sameDir(candidate, dir));
if (!managedDir) return null;
const stem = RECORDING_STEM.exec(path.basename(filePath))?.[0];
return stem ? path.join(path.resolve(managedDir), stem) : null;
}

function documentRecordingKeys(doc: AxcutDocument, recordingsDirs: readonly string[]): Set<string> {
const keys = new Set<string>();
for (const mediaPath of documentMediaPaths(doc)) {
const key = recordingKey(mediaPath, recordingsDirs);
if (key) keys.add(key);
}
return keys;
}

async function readRecordingSet(key: string): Promise<RecordingSet | null> {
const dir = path.dirname(key);
const stem = path.basename(key);
let names: string[];
try {
names = await fs.readdir(dir);
} catch {
return null;
}
const files: string[] = [];
let sizeBytes = 0;
for (const name of names) {
// `recording-12` must not pick up `recording-123`.
if (name !== stem && !name.startsWith(`${stem}.`) && !name.startsWith(`${stem}-`)) continue;
const filePath = path.join(dir, name);
try {
const stat = await fs.stat(filePath);
if (!stat.isFile()) continue;
files.push(filePath);
sizeBytes += stat.size;
} catch {
// Gone between the listing and the stat: nothing left to delete.
}
}
return files.length > 0 ? { dir, stem, files, sizeBytes } : null;
}

/**
* The recordings `projectId` owns: takes in one of OpenScreen's recordings folders that no
* other project uses. A video imported from anywhere else is never the project's to delete.
*/
export async function findProjectRecordings(
projectId: string,
projects: { documents: readonly AxcutDocument[]; unreadable: readonly string[] },
recordingsDirs: readonly string[],
): Promise<ProjectRecordings> {
const { documents } = projects;
const target = documents.find((doc) => doc.project.id === projectId);
if (!target) return { owned: [], keptFor: [], unreadable: 0 };

const keys = documentRecordingKeys(target, recordingsDirs);
if (keys.size > 0 && projects.unreadable.length > 0) {
return { owned: [], keptFor: [], unreadable: projects.unreadable.length };
}
const keptFor = new Set<string>();
for (const doc of documents) {
if (doc.project.id === projectId) continue;
const otherKeys = documentRecordingKeys(doc, recordingsDirs);
for (const key of keys) {
if (otherKeys.has(key)) {
keys.delete(key);
keptFor.add(doc.project.title);
}
}
}

const owned: RecordingSet[] = [];
for (const key of keys) {
const set = await readRecordingSet(key);
if (set) owned.push(set);
}
return { owned, keptFor: [...keptFor], unreadable: 0 };
}

/** Throws on the first file that cannot be removed, so a failed trash can be retried permanently. */
export async function removeRecordingSets(
sets: readonly RecordingSet[],
disposal: Exclude<RecordingsDisposal, "keep">,
trashItem: (filePath: string) => Promise<void>,
): Promise<void> {
for (const set of sets) {
for (const filePath of set.files) {
if (disposal === "trash") {
await trashItem(filePath);
} else {
await fs.rm(filePath, { force: true });
}
}
}
}
4 changes: 4 additions & 0 deletions electron/ipc/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5222,6 +5222,10 @@ export function registerIpcHandlers(
}
},
getAiEditionDocuments: () => aiEditionDocuments,
getRecordingsDirs: () => ({
dirs: getAllowedReadDirs(),
registryDir: DEFAULT_RECORDINGS_DIR,
}),
getStylePresets: () => stylePresets,
getAiEditionLlmConfig,
runAiEditionChat: (projectId, sessionId, message, document, sink, signal) =>
Expand Down
62 changes: 62 additions & 0 deletions electron/ipc/nativeBridge.deleteProject.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Deleting a project takes the recordings disposal from the renderer, and every value but
// "trash" would remove files for good, so the bridge accepts only the known ones.

import { beforeEach, describe, expect, it, vi } from "vitest";
import type { NativeBridgeResponse } from "../../src/native/contracts";
import { type NativeBridgeContext, registerNativeBridgeHandlers } from "./nativeBridge";

const electron = vi.hoisted(() => ({ handle: vi.fn() }));
vi.mock("electron", () => ({
app: { getAppPath: () => "", isPackaged: false },
ipcMain: { handle: electron.handle, removeHandler: vi.fn() },
shell: {},
}));
vi.mock("../native-bridge/services/compositorViewService", () => ({
CompositorViewService: class {},
}));

const service = vi.hoisted(() => ({
deleteProject: vi.fn(async () => ({ success: true })),
}));
vi.mock("../native-bridge/services/aiEditionService", () => ({
AiEditionService: class {
deleteProject = service.deleteProject;
},
}));

let invoke: (payload: unknown) => Promise<NativeBridgeResponse>;

beforeEach(() => {
electron.handle.mockClear();
service.deleteProject.mockClear();
registerNativeBridgeHandlers({
getPlatform: () => "linux",
getAiEditionDocuments: () => ({}),
getAiEditionLlmConfig: () => ({}),
} as unknown as NativeBridgeContext);
const handler = electron.handle.mock.calls[0]?.[1] as (
event: unknown,
request: unknown,
) => Promise<NativeBridgeResponse>;
invoke = (payload) =>
handler(
{ sender: {} },
{ domain: "aiEdition", action: "document.delete", payload, requestId: "r" },
);
});

describe("native bridge project delete", () => {
it.each(["Trash", "delete", null, 1])("rejects recordings=%s as a bad request", async (value) => {
const response = await invoke({ projectId: "p", recordings: value });

expect(response.ok ? undefined : response.error?.code).toBe("INVALID_REQUEST");
expect(service.deleteProject).not.toHaveBeenCalled();
});

it.each([undefined, "keep", "trash", "permanent"])("passes recordings=%s on", async (value) => {
const response = await invoke({ projectId: "p", recordings: value });

expect(response.ok).toBe(true);
expect(service.deleteProject).toHaveBeenCalledWith("p", value);
});
});
Loading
Loading