Skip to content

Open source alternative to Semrush and Ahrefs

Only report site audit failures that are recorded as failed (#972)

Carter Smithbf9f2d54c800
diff --git a/src/server/workflows/SiteAuditWorkflow.test.ts b/src/server/workflows/SiteAuditWorkflow.test.ts
index ba3397dc..e285771a 100644
--- a/src/server/workflows/SiteAuditWorkflow.test.ts
+++ b/src/server/workflows/SiteAuditWorkflow.test.ts
@@ -26,9 +26,11 @@ vi.mock("@/server/lib/audit/rendering-billing", () => ({
}));
vi.mock("@/server/features/audit/repositories/AuditRepository", () => ({
AuditRepository: {
- getAuditForWorkflow: vi
- .fn()
- .mockResolvedValue({ projectId: "project", currentPhase: "crawling" }),
+ getAuditForWorkflow: vi.fn().mockResolvedValue({
+ projectId: "project",
+ status: "running",
+ currentPhase: "crawling",
+ }),
failAudit: mocks.failAudit,
},
}));
diff --git a/src/server/workflows/SiteAuditWorkflow.ts b/src/server/workflows/SiteAuditWorkflow.ts
index d26d8c63..479f94cf 100644
--- a/src/server/workflows/SiteAuditWorkflow.ts
+++ b/src/server/workflows/SiteAuditWorkflow.ts
@@ -100,27 +100,12 @@ export class SiteAuditWorkflow extends WorkflowEntrypoint<Env, AuditParams> {
});
} catch (error) {
console.error(`Audit ${auditId} failed:`, error);
- // Workflow entrypoints run outside the server-function middleware, so
- // nothing else forwards this throw to PostHog as a $exception. Capture it
- // here (awaited — Workflows have no ctx.waitUntil) before re-throwing.
// Deploy-time resets are expected churn, not actionable errors.
const isDeployReset =
error instanceof Error &&
error.message.includes(
"Durable Object reset because its code was updated",
);
- if (!isDeployReset) {
- await captureServerError(
- error,
- {
- source: "site_audit_workflow",
- audit_id: auditId,
- organization_id: billingCustomer.organizationId,
- project_id: projectId,
- },
- billingCustomer.userId,
- );
- }
const errorInfo = classifyAuditError(error);
await pgStep(step, "mark-failed", DB_STEP, async () => {
// Read the phase before failAudit stamps currentPhase = "failed".
@@ -128,11 +113,32 @@ export class SiteAuditWorkflow extends WorkflowEntrypoint<Env, AuditParams> {
auditId,
event.instanceId,
);
+ // A completed audit (e.g. cleanup failed after finalize) stays
+ // completed, so it gets no failure telemetry either.
+ if (runningAudit?.status !== "running") return;
await AuditRepository.failAudit(auditId, event.instanceId, {
...errorInfo,
failedPhase: runningAudit?.currentPhase ?? null,
});
+ // Workflow entrypoints run outside the server-function middleware, so
+ // nothing else forwards this throw to PostHog as a $exception. Captured
+ // inside this step: when Cloudflare loses the invocation (internal
+ // workflow errors, evicted Durable Objects), the step never runs and the
+ // engine replays the audit, which usually completes.
+ if (!isDeployReset) {
+ await captureServerError(
+ error,
+ {
+ source: "site_audit_workflow",
+ audit_id: auditId,
+ organization_id: billingCustomer.organizationId,
+ project_id: projectId,
+ },
+ billingCustomer.userId,
+ );
+ }
+
await captureServerEvent({
distinctId: billingCustomer.userId,
event: "site_audit:complete",