Skip to content

Commit 9ae0007

Browse files
committed
fix(cli): preserve capture failure diagnostics
1 parent ac94588 commit 9ae0007

6 files changed

Lines changed: 64 additions & 8 deletions

File tree

‎packages/cli/src/capture/contentExtractor.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,35 @@ describe("captionImagesWithGemini — OpenRouter provider", () => {
309309
expect(fetchMock).not.toHaveBeenCalled();
310310
});
311311

312+
it("classifies non-provider pipeline failures without leaking the local path", async () => {
313+
const dir = mkdtempSync(join(tmpdir(), "hf-caption-no-assets-"));
314+
dirs.push(dir);
315+
vi.stubEnv("OPENROUTER_API_KEY", "or-unused-key");
316+
317+
const warnings: string[] = [];
318+
let outcome: VisionCaptionOutcome | undefined;
319+
const captions = await captionImagesWithGemini(dir, () => {}, warnings, {
320+
onOutcome: (value) => {
321+
outcome = value;
322+
},
323+
});
324+
325+
expect(captions).toEqual({});
326+
expect(warnings).toEqual(["OpenRouter captioning failed internally; captions omitted."]);
327+
expect(warnings.join(" ")).not.toContain(dir);
328+
expect(outcome).toEqual({
329+
timedOutRequests: 0,
330+
failedRequests: 0,
331+
budgetExhausted: false,
332+
internalError: true,
333+
});
334+
if (!outcome) throw new Error("Expected vision caption outcome");
335+
expect(resolveVisionPhaseCompletion(outcome, 10_000)).toEqual({
336+
status: "degraded",
337+
reason: "internal-error",
338+
});
339+
});
340+
312341
it("skips captioning entirely when no provider key is present", async () => {
313342
const dir = makeProjectWithImages();
314343
dirs.push(dir);

‎packages/cli/src/capture/contentExtractor.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export interface VisionCaptionOutcome {
2121
timedOutRequests: number;
2222
failedRequests: number;
2323
budgetExhausted: boolean;
24+
/** Failure outside provider request handling (filesystem, module, or programming error). */
25+
internalError?: boolean;
2426
}
2527

2628
interface VisionCaptionOptions {
@@ -36,11 +38,14 @@ export function resolveVisionPhaseCompletion(
3638
| { status: "completed" }
3739
| {
3840
status: "degraded";
39-
reason: "budget-exhausted" | "request-timeout" | "provider-error";
41+
reason: "budget-exhausted" | "request-timeout" | "provider-error" | "internal-error";
4042
} {
4143
if (outcome.budgetExhausted || remainingMs <= 0) {
4244
return { status: "degraded", reason: "budget-exhausted" };
4345
}
46+
if (outcome.internalError) {
47+
return { status: "degraded", reason: "internal-error" };
48+
}
4449
if (outcome.timedOutRequests > 0) {
4550
return { status: "degraded", reason: "request-timeout" };
4651
}
@@ -245,12 +250,15 @@ export async function captionImagesWithGemini(
245250
let timedOutCount = 0;
246251
let failedRequestCount = 0;
247252
let budgetExhausted = false;
253+
let internalError = false;
248254
const reportOutcome = (): void => {
249-
options.onOutcome?.({
255+
const outcome: VisionCaptionOutcome = {
250256
timedOutRequests: timedOutCount,
251257
failedRequests: failedRequestCount,
252258
budgetExhausted,
253-
});
259+
};
260+
if (internalError) outcome.internalError = true;
261+
options.onOutcome?.(outcome);
254262
};
255263
if (options.skipVision) {
256264
reportOutcome();
@@ -546,8 +554,8 @@ export async function captionImagesWithGemini(
546554
);
547555
}
548556
} catch {
549-
failedRequestCount = Math.max(1, failedRequestCount);
550-
warnings.push(`${providerName} captioning failed; captions omitted.`);
557+
internalError = true;
558+
warnings.push(`${providerName} captioning failed internally; captions omitted.`);
551559
}
552560

553561
reportOutcome();

‎packages/cli/src/capture/index.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@ export async function captureWebsite(
267267
httpStatus: navigationResponse?.status() ?? null,
268268
...pageContentCheck,
269269
});
270-
if (blockedReason) throw new Error(blockedReason);
270+
if (blockedReason) {
271+
phase("navigation", "degraded", "blocked");
272+
throw new Error(blockedReason);
273+
}
271274

272275
phase("navigation", "completed");
273276
phase("core-extraction", "started");

‎packages/cli/src/capture/pageBlockDetection.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ describe("detectBlockedPage", () => {
2323
hasChallengeElement: false,
2424
},
2525
},
26+
{
27+
label: "classic Cloudflare interstitial",
28+
evidence: {
29+
httpStatus: 503,
30+
title: "Attention Required! | Cloudflare",
31+
textLength: 120,
32+
bodyChildCount: 3,
33+
hasChallengeElement: false,
34+
},
35+
},
2636
])("rejects a minimal protection page: $label", ({ evidence }) => {
2737
expect(detectBlockedPage(evidence)).toMatch(/capture blocked/i);
2838
});

‎packages/cli/src/capture/pageBlockDetection.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function detectBlockedPage(evidence: PageLoadEvidence): string | undefine
1616
const hasBlockedStatus =
1717
evidence.httpStatus === 401 || evidence.httpStatus === 403 || evidence.httpStatus === 429;
1818
const hasBlockedTitle =
19-
/^(?:(?:error\s*)?(?:401|403|429)(?:\s*(?:[-:—]\s*)?(?:forbidden|unauthorized|access denied|too many requests))?|forbidden|access denied|attention required|just a moment(?:\.{3})?)(?:\s*[|—-]\s*(?:cloudflare|sucuri website firewall))?$/i.test(
19+
/^(?:(?:error\s*)?(?:401|403|429)(?:\s*(?:[-:—]\s*)?(?:forbidden|unauthorized|access denied|too many requests))?|forbidden|access denied|attention required!?|just a moment(?:\.{3})?)(?:\s*[|—-]\s*(?:cloudflare|sucuri website firewall))?$/i.test(
2020
evidence.title.trim(),
2121
);
2222

‎packages/cli/src/capture/types.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ export interface CapturePhaseProgress {
2626
status: "started" | "completed" | "degraded";
2727
/** Null before the post-navigation budget begins. */
2828
remainingMs: number | null;
29-
reason?: "budget-exhausted" | "disabled" | "request-timeout" | "provider-error";
29+
reason?:
30+
| "budget-exhausted"
31+
| "disabled"
32+
| "request-timeout"
33+
| "provider-error"
34+
| "internal-error"
35+
| "blocked";
3036
}
3137

3238
export interface CaptureOptions {

0 commit comments

Comments
 (0)