Skip to content

Commit 4c7fbde

Browse files
vanceingallsclaude
andcommitted
fix(engine): emit pipelined frame diagnostics before the recoverable wrapper
Review feedback on #3805. captureFrameToBufferPipelined checked isRecoverableDrawElementError and threw DrawElementCaptureError before reaching captureFrameErrorDiagnostics, so the NCPR/canvas failures that now abort the whole attempt produced no frame-error PNG/HTML/JSON bundle — the exact case worth debugging, and the one the adjacent comment promised mirrored the serial path. The serial path was unaffected because its own DrawElementCaptureError throw propagates through captureFrameCore's outer diagnostics catch. Run diagnostics first, then the recoverable wrapper. Bounded to at most one bundle per attempt, since a recoverable error fails the whole attempt, and captureFrameErrorDiagnostics self-catches, so a dead page cannot mask the structural error the producer's fresh-page retry depends on. Covered by a new test asserting the bundle lands for a recoverable pipelined failure; verified by mutation (restoring the old order fails it). Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 6a206ff commit 4c7fbde

2 files changed

Lines changed: 37 additions & 10 deletions

File tree

‎packages/engine/src/services/frameCapture-freshFallback.test.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2-
import { mkdtempSync, rmSync } from "node:fs";
2+
import { existsSync, mkdtempSync, rmSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import {
@@ -102,6 +102,26 @@ describe("fresh screenshot fallback", () => {
102102
});
103103
}
104104

105+
// The recoverable-error wrapper used to throw before the diagnostics call,
106+
// so exactly the NCPR/canvas failures worth debugging produced no bundle.
107+
it("still writes per-frame diagnostics when a recoverable pipelined failure aborts the attempt", async () => {
108+
vi.mocked(produceDrawElementFrame).mockRejectedValueOnce(
109+
new Error("No cached paint record for element"),
110+
);
111+
const s = await session();
112+
// The shared mock page fails its diagnostic screenshot; give this one a
113+
// working page so the bundle can actually land on disk.
114+
Object.assign(s.page, {
115+
screenshot: async () => Buffer.alloc(0),
116+
content: async () => "<html></html>",
117+
});
118+
119+
await expect(captureFrameToBufferPipelined(s, 7, 7 / 30)).rejects.toBeInstanceOf(
120+
DrawElementCaptureError,
121+
);
122+
expect(existsSync(join(s.outputDir, "diagnostics", "frame-error-7.json"))).toBe(true);
123+
});
124+
105125
it("rejects a suspect tiny frame without substituting a stale screenshot", async () => {
106126
vi.mocked(captureDrawElementFrame).mockResolvedValueOnce(Buffer.alloc(100));
107127
await expect(captureFrameToBuffer(await session(), 7, 7 / 30)).rejects.toThrow(

‎packages/engine/src/services/frameCapture.ts‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3893,17 +3893,16 @@ export async function captureFrameToBufferPipelined(
38933893

38943894
return { encodeResult, captureTimeMs };
38953895
} catch (captureError) {
3896-
// The viewport can contain the last injected bitmap, not the sought frame.
3897-
if (isRecoverableDrawElementError(captureError)) {
3898-
session.deNcprFallbacks = (session.deNcprFallbacks ?? 0) + 1;
3899-
const reason = isCanvasNotInitializedError(captureError)
3900-
? "drawElement canvas not initialized"
3901-
: "No cached paint record";
3902-
throw new DrawElementCaptureError(frameIndex, reason, captureError);
3903-
}
39043896
// Mirror captureFrameCore: capture per-frame diagnostics (frame-error
39053897
// PNG/HTML/JSON + console tail) before rethrowing so pipelined-path
3906-
// failures are debuggable like the serial path.
3898+
// failures are debuggable like the serial path. Runs BEFORE the
3899+
// recoverable-error wrapper below because the serial path's diagnostics
3900+
// sit in an outer catch that its own DrawElementCaptureError throw
3901+
// propagates through — ordering it after the wrapper would silently skip
3902+
// the bundle for exactly the NCPR/canvas failures worth debugging.
3903+
// Bounded: a recoverable error aborts the whole attempt, so this fires at
3904+
// most once per attempt. captureFrameErrorDiagnostics self-catches, so a
3905+
// dead page cannot mask the structural error the producer retries on.
39073906
if (session.isInitialized) {
39083907
await captureFrameErrorDiagnostics(
39093908
session,
@@ -3912,6 +3911,14 @@ export async function captureFrameToBufferPipelined(
39123911
captureError instanceof Error ? captureError : new Error(String(captureError)),
39133912
);
39143913
}
3914+
// The viewport can contain the last injected bitmap, not the sought frame.
3915+
if (isRecoverableDrawElementError(captureError)) {
3916+
session.deNcprFallbacks = (session.deNcprFallbacks ?? 0) + 1;
3917+
const reason = isCanvasNotInitializedError(captureError)
3918+
? "drawElement canvas not initialized"
3919+
: "No cached paint record";
3920+
throw new DrawElementCaptureError(frameIndex, reason, captureError);
3921+
}
39153922
throw captureError;
39163923
}
39173924
}

0 commit comments

Comments
 (0)