8000 feat(jobs): rich sync-failure diagnostic report behind banner toggle by johannboehme · Pull Request #40 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
103 changes: 103 additions & 0 deletions frontend/src/local/diagnostics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { collectSyncFailureReport, formatReport } from "./diagnostics";
import { useOpsStore } from "./ops-store";

beforeEach(() => {
useOpsStore.setState({ ops: {} });
});

afterEach(() => {
vi.unstubAllGlobals();
useOpsStore.setState({ ops: {} });
});

describe("collectSyncFailureReport", () => {
it("captures stage, file-in-flight, and Error.name + message", async () => {
useOpsStore.getState().startSyncOp("job-1", { stage: "decoding-studio-audio" });
useOpsStore.getState().updateSyncOp("job-1", {
currentFile: {
name: "studio.wav",
size: 1024 * 1024,
type: "audio/wav",
sourceKind: "opfs",
},
});

const err = new TypeError("boom");
const { summary, report, data } = await collectSyncFailureReport("job-1", err);

expect(summary).toContain("[decoding-studio-audio]");
expect(summary).toContain("TypeError");
expect(summary).toContain("boom");

expect(data.error.name).toBe("TypeError");
expect(data.error.message).toBe("boom");
expect(data.file?.name).toBe("studio.wav");
expect(data.file?.sourceKind).toBe("opfs");
expect(data.stage).toBe("decoding-studio-audio");

expect(report).toContain("Job: job-1");
expect(report).toContain("Stage: decoding-studio-audio");
expect(report).toContain("Name: TypeError");
expect(report).toContain("Message: boom");
expect(report).toContain("Name: studio.wav");
expect(report).toContain("Source: opfs");
});

it("survives a non-Error throw and stack-less errors", async () => {
const err = new Error("oops");
err.stack = undefined as unknown as string; // simulate stripped stack
const { data } = await collectSyncFailureReport("job-x", err);
expect(data.error.message).toBe("oops");
expect(data.error.stack).toEqual([]);

const string = await collectSyncFailureReport("job-y", "raw string fail");
expect(string.data.error.name).toBe("string");
expect(string.data.error.message).toBe("raw string fail");
expect(string.summary).toContain("raw string fail");
});

it("falls back to 'unknown' stage when the op is absent", async () => {
const { data, summary } = await collectSyncFailureReport(
"missing",
new Error("no op for this jobId"),
);
expect(data.stage).toBe("unknown");
expect(summary).not.toContain("[unknown]"); // no stage prefix when unknown
expect(summary).toContain("no op for this jobId");
});

it("renders capability rows in the report (live snapshot)", async () => {
const { report } = await collectSyncFailureReport("job-c", new Error("x"));
expect(report).toContain("WebCodecs (audio):");
expect(report).toContain("OPFS:");
expect(report).toContain("WebGPU:");
});
});

describe("formatReport", () => {
it("omits the 'File in flight' section when no file is set", () => {
const out = formatReport({
jobId: "j",
stage: "loading",
whenIso: "2026-05-09T00:00:00.000Z",
error: { name: "Error", message: "x", stack: [] },
browser: { userAgent: "ua", platform: "p", language: "l" },
capabilities: {
webAssembly: true,
sharedArrayBuffer: true,
crossOriginIsolated: true,
opfs: true,
audioDecoder: true,
videoDecoder: true,
audioEncoder: true,
videoEncoder: true,
fileSystemAccess: true,
webgl2: true,
webgpu: false,
},
});
expect(out).not.toContain("File in flight");
expect(out).toContain("WebGPU: no");
});
});
215 changes: 215 additions & 0 deletions frontend/src/local/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/**
* Sync-failure diagnostic report.
*
* Sammelt zur Fail-Zeit alles was uns hilft, einen Bug-Report vom User
* zu triagen ohne zurückfragen zu müssen: originaler Error inkl. Name +
* Stack, welcher File grade in der Mache war, Browser-/OS-Fingerprint,
* Capability-Snapshot, OPFS-Quota.
*
* Output ist plaintext, copy-paste-tauglich. Bewusst kein JSON — der
* User soll's auch ohne Tools lesen können (Discord, Mail).
*
* Gegenstück zum generischen "File could not be read! Code=-1" Banner:
* der Banner zeigt nur die summary-Zeile, der full report wird per
* "Show details" aus-/eingeklappt.
*/

import { getCapabilities, type Capabilities } from "./capabilities";
import { useOpsStore, type SyncOpFileContext } from "./ops-store";

const STACK_FRAME_LIMIT = 5;

export interface SyncFailureReport {
/** One-liner suitable for the inline error banner. Carries stage +
* the first line of the error message (truncated). */
summary: string;
/** Multi-line plaintext report — what the user copies into a bug
* report. Includes everything in `data` formatted for humans. */
report: string;
/** Structured copy of the report, exposed for tests/telemetry. */
data: SyncFailureData;
}

export interface SyncFailureData {
jobId: string;
stage: string;
whenIso: string;
error: { name: string; message: string; stack: string[] };
file?: SyncOpFileContext;
browser: { userAgent: string; platform: string; language: string };
capabilities: Capabilities;
storage?: { usage?: number; quota?: number };
}

/**
* Build a failure report for the given job + thrown error. Reads the
* current sync-op state for stage + the file-in-flight, plus the live
* capability snapshot and OPFS quota. Pure async — never throws (a
* broken diagnostic step is degraded to "unknown" so we don't lose the
* original error).
*/
export async function collectSyncFailureReport(
jobId: string,
err: unknown,
): Promise<SyncFailureReport> {
const op = useOpsStore.getState().ops[jobId]?.sync;
const stage = op?.stage ?? "unknown";
const file = op?.currentFile;

const error = normaliseError(err);
const browser = readBrowser();
const capabilities = getCapabilities();
const storage = await readStorage();

const data: SyncFailureData = {
jobId,
stage,
whenIso: new Date().toISOString(),
error,
file,
browser,
capabilities,
storage,
};

return {
summary: buildSummary(stage, error),
report: formatReport(data),
data,
};
}

function normaliseError(err: unknown): SyncFailureData["error"] {
if (err instanceof Error) {
return {
name: err.name || "Error",
message: err.message || String(err),
stack: parseStackFrames(err.stack),
};
}
return { name: typeof err, message: String(err), stack: [] };
}

function parseStackFrames(stack: string | undefined): string[] {
if (!stack) return [];
// V8 prefixes with "<Name>: <message>" line; non-V8 (Safari) starts
// straight with the frames. Drop any prefix that doesn't look like a
// frame (no leading "at " AND no "@" — Safari uses `fn@url:line:col`).
const lines = stack.split("\n").map((l) => l.trim()).filter(Boolean);
const frames = lines.filter((l) => l.startsWith("at ") || l.includes("@"));
return (frames.length > 0 ? frames : lines).slice(0, STACK_FRAME_LIMIT);
}

function readBrowser(): SyncFailureData["browser"] {
if (typeof navigator === "undefined") {
return { userAgent: "unknown", platform: "unknown", language: "unknown" };
}
const nav = navigator as Navigator & { platform?: string };
return {
userAgent: nav.userAgent ?? "unknown",
platform: nav.platform ?? "unknown",
language: nav.language ?? "unknown",
};
}

async function readStorage(): Promise<SyncFailureData["storage"]> {
if (typeof navigator === "undefined") return undefined;
const storage = navigator.storage as
| (StorageManager & { estimate?: () => Promise<StorageEstimate> })
| undefined;
if (!storage?.estimate) return undefined;
try {
const est = await storage.estimate();
return { usage: est.usage, quota: est.quota };
} catch {
return undefined;
}
}

function buildSummary(stage: string, error: SyncFailureData["error"]): string {
const firstLine = error.message.split("\n")[0].slice(0, 200);
const stagePart = stage && stage !== "unknown" ? `[${stage}] ` : "";
return `${stagePart}${error.name}: ${firstLine}`;
}

const SECTION_RULE = "----------------------------------------";

/** Plaintext formatter — one column, fixed-width labels, no markdown. */
export function formatReport(data: SyncFailureData): string {
const lines: string[] = [];
lines.push("videoaudiosync — sync failure report");
lines.push("====================================");
lines.push(`Job: ${data.jobId}`);
lines.push(`Stage: ${data.stage}`);
lines.push(`When: ${data.whenIso}`);
lines.push("");

lines.push("Error");
lines.push(SECTION_RULE);
lines.push(`Name: ${data.error.name}`);
lines.push(`Message: ${data.error.message}`);
if (data.error.stack.length > 0) {
lines.push("Stack:");
for (const frame of data.error.stack) lines.push(` ${frame}`);
}
lines.push("");

if (data.file) {
lines.push("File in flight");
lines.push(SECTION_RULE);
lines.push(`Name: ${data.file.name}`);
lines.push(`Size: ${formatBytes(data.file.size)}`);
lines.push(`Type: ${data.file.type || "unknown"}`);
lines.push(`Source: ${data.file.sourceKind}`);
lines.push("");
}

lines.push("Browser");
lines.push(SECTION_RULE);
lines.push(`UA: ${data.browser.userAgent}`);
lines.push(`Platform: ${data.browser.platform}`);
lines.push(`Language: ${data.browser.language}`);
lines.push("");

lines.push("Capabilities");
lines.push(SECTION_RULE);
const c = data.capabilities;
lines.push(`WebCodecs (audio): ${yn(c.audioDecoder)}`);
lines.push(`WebCodecs (video): ${yn(c.videoDecoder)}`);
lines.push(`WebCodecs encoders: audio=${yn(c.audioEncoder)} video=${yn(c.videoEncoder)}`);
lines.push(`OPFS: ${yn(c.opfs)}`);
lines.push(`WebAssembly: ${yn(c.webAssembly)}`);
lines.push(`SharedArrayBuffer: ${yn(c.sharedArrayBuffer)}`);
lines.push(`Cross-origin iso: ${yn(c.crossOriginIsolated)}`);
lines.push(`File System Access: ${yn(c.fileSystemAccess)}`);
lines.push(`WebGL2: ${yn(c.webgl2)}`);
lines.push(`WebGPU: ${yn(c.webgpu)}`);
lines.push("");

if (data.storage) {
lines.push("Storage");
lines.push(SECTION_RULE);
lines.push(`Used: ${formatBytes(data.storage.usage)}`);
lines.push(`Quota: ${formatBytes(data.storage.quota)}`);
lines.push("");
}

return lines.join("\n").trimEnd();
}

function yn(v: boolean): string {
return v ? "yes" : "no";
}

function formatBytes(n: number | undefined): string {
if (n === undefined || !Number.isFinite(n)) return "unknown";
if (n < 1024) return `${n} B`;
const units = ["KB", "MB", "GB", "TB"];
let val = n / 1024;
let i = 0;
while (val >= 1024 && i < units.length - 1) {
val /= 1024;
i++;
}
return `${val.toFixed(val < 10 ? 2 : 1)} ${units[i]}`;
}
Loading
Loading
0