Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(diagnostics): write the editor's document, cap Linux helper output in bytes

Etienne Lescotd8550b795b04
diff --git a/electron/diagnostics/project-state.test.ts b/electron/diagnostics/project-state.test.ts
new file mode 100644
index 000000000..68cfaeab7
--- /dev/null
+++ b/electron/diagnostics/project-state.test.ts
@@ -0,0 +1,30 @@
+import { describe, expect, it, vi } from "vitest";
+import {
+ DIAGNOSTIC_PROJECT_STATE_TIMEOUT_MS,
+ resolveDiagnosticProjectState,
+} from "./project-state";
+
+const editorWith = (snapshot: { document: unknown; revision: number } | null) => ({
+ snapshot: vi.fn(async (_timeoutMs?: number) => snapshot),
+});
+
+describe("resolveDiagnosticProjectState", () => {
+ it("writes the editor's document, not the snapshot wrapping it", async () => {
+ const document = { project: { id: "p1" }, timeline: [] };
+ const host = editorWith({ document, revision: 7 });
+ await expect(resolveDiagnosticProjectState(null, host)).resolves.toBe(document);
+ expect(host.snapshot).toHaveBeenCalledWith(DIAGNOSTIC_PROJECT_STATE_TIMEOUT_MS);
+ });
+
+ it("keeps a state the caller passed, without asking the editor", async () => {
+ const supplied = { project: { id: "from-renderer" } };
+ const host = editorWith({ document: { project: { id: "p1" } }, revision: 1 });
+ await expect(resolveDiagnosticProjectState(supplied, host)).resolves.toBe(supplied);
+ expect(host.snapshot).not.toHaveBeenCalled();
+ });
+
+ it("is null when no editor holds a document, or there is no host", async () => {
+ await expect(resolveDiagnosticProjectState(null, editorWith(null))).resolves.toBeNull();
+ await expect(resolveDiagnosticProjectState(undefined, null)).resolves.toBeNull();
+ });
+});
diff --git a/electron/diagnostics/project-state.ts b/electron/diagnostics/project-state.ts
new file mode 100644
index 000000000..a2cf1aa50
--- /dev/null
+++ b/electron/diagnostics/project-state.ts
@@ -0,0 +1,22 @@
+import type { EditorDocumentHost } from "../mcp/editor-document-host";
+
+/**
+ * How long Save Diagnostics waits for the editor's document. A hung editor is exactly
+ * when a bundle is wanted, so it must not hold the save for an MCP call's 30 s.
+ */
+export const DIAGNOSTIC_PROJECT_STATE_TIMEOUT_MS = 5_000;
+
+/**
+ * The `projectState` a diagnostic bundle carries: what the caller passed, else the open
+ * editor's live document — the document itself, not the `{ document, revision }`
+ * snapshot it arrives in — else `null`. The tray and the menus pass nothing, having no
+ * renderer to ask; `host` is the one the MCP server reads the editor through.
+ */
+export async function resolveDiagnosticProjectState(
+ supplied: unknown,
+ host: Pick<EditorDocumentHost, "snapshot"> | null,
+): Promise<unknown> {
+ if (supplied !== null && supplied !== undefined) return supplied;
+ const snapshot = await host?.snapshot(DIAGNOSTIC_PROJECT_STATE_TIMEOUT_MS);
+ return snapshot?.document ?? null;
+}
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index 8da84bc8c..a7600ec73 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -73,6 +73,7 @@ import { AppSettingsStore } from "../app-settings";
import { createRunnerHost } from "../cli/runnerHost";
import { hideCountdownOverlay } from "../countdownOverlay";
import { isDiagnosticModeEnabled, mainLogBuffer } from "../diagnostics/main-log-buffer";
+import { resolveDiagnosticProjectState } from "../diagnostics/project-state";
import { mainT } from "../i18n";
import { getInstallChannel } from "../install-channel";
import {
@@ -1929,11 +1930,6 @@ async function resolveMediaLinksForVideo(videoPath: string): Promise<{
* registerIpcHandlers.
*/
let editorDocumentHost: EditorDocumentHost | null = null;
-/**
- * How long Save Diagnostics waits for the editor's document. A hung editor is exactly
- * when a bundle is wanted, so it must not hold the save for an MCP call's 30 s.
- */
-const DIAGNOSTIC_PROJECT_STATE_TIMEOUT_MS = 5_000;
/**
* Writes the diagnostic bundle a bug report needs: app/OS facts, the native
@@ -1960,10 +1956,10 @@ export async function exportDiagnosticFile(payload: {
const HELPER_OUTPUT_MAX_BYTES = 64 * 1024;
const tail = (s: string, max: number) => (s.length <= max ? s : s.slice(s.length - max));
- const projectState =
- payload.projectState ??
- (await editorDocumentHost?.snapshot(DIAGNOSTIC_PROJECT_STATE_TIMEOUT_MS)) ??
- null;
+ const projectState = await resolveDiagnosticProjectState(
+ payload.projectState,
+ editorDocumentHost,
+ );
const diagnostic = {
timestamp: new Date().toISOString(),
diff --git a/electron/mcp/editor-document-host.ts b/electron/mcp/editor-document-host.ts
index 973fb04cb..9230d85b3 100644
--- a/electron/mcp/editor-document-host.ts
+++ b/electron/mcp/editor-document-host.ts
@@ -102,7 +102,7 @@ export class EditorDocumentHost implements McpDocumentHost {
});
}
- /** `timeoutMs`: Save Diagnostics waits less than an MCP call, see exportDiagnosticFile. */
+ /** `timeoutMs`: shorter for Save Diagnostics, see diagnostics/project-state.ts. */
async snapshot(timeoutMs = REQUEST_TIMEOUT_MS): Promise<AiEditionMcpHostSnapshot | null> {
const result = await this.request({ op: "snapshot" }, timeoutMs);
if (!result || result === TIMED_OUT || typeof result !== "object") return null;
diff --git a/electron/native-bridge/capture/linuxNativeCaptureSession.test.ts b/electron/native-bridge/capture/linuxNativeCaptureSession.test.ts
index e19dba5ff..7af0409be 100644
--- a/electron/native-bridge/capture/linuxNativeCaptureSession.test.ts
+++ b/electron/native-bridge/capture/linuxNativeCaptureSession.test.ts
@@ -634,5 +634,17 @@ describe("LinuxNativeCaptureSession", () => {
expect(session.helperOutput).toContain("warning 999 ");
expect(session.helperOutput).not.toContain('"event":"ready"');
});
+
+ it("counts the cap in UTF-8 bytes and cuts it between characters", async () => {
+ const session = newSession();
+ await startReady(session);
+ // 90 000 bytes of a 3-byte character: the 64 KiB cut lands 2 bytes into one,
+ // so the whole characters that fit are the 21 845 after it.
+ helper.stderr.write("€".repeat(30_000));
+ await flushStdout();
+
+ expect(Buffer.byteLength(session.helperOutput, "utf8")).toBeLessThanOrEqual(64 * 1024);
+ expect(session.helperOutput).toBe("€".repeat(21_845));
+ });
});
});
diff --git a/electron/native-bridge/capture/linuxNativeCaptureSession.ts b/electron/native-bridge/capture/linuxNativeCaptureSession.ts
index 0ccffd467..0c65c13d5 100644
--- a/electron/native-bridge/capture/linuxNativeCaptureSession.ts
+++ b/electron/native-bridge/capture/linuxNativeCaptureSession.ts
@@ -44,10 +44,11 @@ const STOP_TIMEOUT_MS = 15_000;
*/
const FIRST_FRAME_TIMEOUT_MS = 10_000;
/**
- * The most of the helper's output [`helperOutput`] keeps, the 64 KiB Save Diagnostics
- * writes of each helper. Only the newest is kept, as on Windows and macOS.
+ * The most of the helper's output [`helperOutput`] keeps, in UTF-8 bytes: the 64 KiB
+ * Save Diagnostics writes of each helper. Only the newest is kept, as on Windows and
+ * macOS.
*/
-const HELPER_OUTPUT_MAX_CHARS = 64 * 1024;
+const HELPER_OUTPUT_MAX_BYTES = 64 * 1024;
export interface LinuxCaptureConfig {
outputPath: string;
@@ -344,9 +345,17 @@ export class LinuxNativeCaptureSession {
private appendHelperOutput(text: string) {
this.helperOutputText += text;
- if (this.helperOutputText.length > HELPER_OUTPUT_MAX_CHARS) {
- this.helperOutputText = this.helperOutputText.slice(-HELPER_OUTPUT_MAX_CHARS);
+ if (Buffer.byteLength(this.helperOutputText, "utf8") <= HELPER_OUTPUT_MAX_BYTES) {
+ return;
+ }
+ const bytes = Buffer.from(this.helperOutputText, "utf8");
+ let start = bytes.length - HELPER_OUTPUT_MAX_BYTES;
+ // A continuation byte (0b10xxxxxx) is the middle of a character: start at the
+ // next one, so the kept text never opens on a replacement character.
+ while (start < bytes.length && (bytes[start] & 0xc0) === 0x80) {
+ start += 1;
}
+ this.helperOutputText = bytes.subarray(start).toString("utf8");
}
/**