High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(scrape): count a 4xx/5xx page in failedPages and exclude it from contentTypes, reinstating PIN-330 (PIN-412)
Luigi Agostibb52d3702d67
diff --git a/docs/scrape.md b/docs/scrape.mdindex fbe37f8a..d5e75bb9 100644--- a/docs/scrape.md+++ b/docs/scrape.md@@ -137,6 +137,25 @@ summary: contentTypes, httpPages, browserPages, failedPages, recommendationswithout `--output-dir`): a single digest with the site overview, the page treeby URL pattern, and each page's content (or its snippet in preview mode).+### Summary counters++`httpPages`, `browserPages` and `failedPages` partition the pages: every page+falls in exactly one, and the three sum to the page total.++- `failedPages` counts the pages that did not return usable content — a page that+ failed in both engines (a transport `error`) **or** one whose `statusCode` is+ `>= 400` (a 4xx/5xx response). A `3xx` redirect and a page that never carried a+ status (`statusCode` `0`/unset) are **not** failures and stay in+ `httpPages`/`browserPages`. A non-2xx page the browser then rendered into content+ (`source: "browser"`) is **not** counted as failed either — the recovery cleared+ the failure, though the page keeps its original `statusCode`. When `failedPages`+ is non-zero, the recommendations include `"N of M pages returned errors or+ 4xx/5xx responses"`, so broken links surface in the summary rather than being+ counted as successful pages.+- `contentTypes` is the taxonomy of the **successful** pages only: a non-2xx page+ is excluded, so an error body is never reported as an ordinary `page`. The+ page's own `statusCode` and `markdown` are left untouched.+### Schema history`schemaVersion` is stamped into every report so a consumer can detect formatdiff --git a/internal/cli/actions/actions_scrape.go b/internal/cli/actions/actions_scrape.goindex 23be1117..6341c0eb 100644--- a/internal/cli/actions/actions_scrape.go+++ b/internal/cli/actions/actions_scrape.go@@ -157,6 +157,13 @@ func printScrapeSummary(report scrape.Report) {if p.BrowserError != "" {status += " · browser failed: " + p.BrowserError}+ // A non-2xx page carries no transport error, so without this it printed as an+ // ordinary "source: http" line and the failure was invisible next to the count.+ // scrape.PageFailed is the same predicate the summary partitions on, so the line+ // and the failedPages count agree — a browser-recovered non-2xx page is not marked.+ if p.StatusCode >= 400 && scrape.PageFailed(p) {+ status = fmt.Sprintf("failed: HTTP %d", p.StatusCode)+ }if p.Error != "" {status = "error: " + p.Error}diff --git a/internal/scrape/run.go b/internal/scrape/run.goindex bf7d17dd..c9092061 100644--- a/internal/scrape/run.go+++ b/internal/scrape/run.go@@ -377,11 +377,13 @@ var regeneratedRecommendations = []string{"extractable text", "returned errors"}func summarize(pages []Page, inherited []string) Summary {s := Summary{ContentTypes: map[string]int{}}for _, p := range pages {- if p.ContentType != "" {+ // A failed page is not an ordinary content sample: keep its error body out of+ // the contentTypes taxonomy so a consumer is not told a 404 is a `page`.+ if !PageFailed(p) && p.ContentType != "" {s.ContentTypes[p.ContentType]++}switch {- case p.Error != "":+ case PageFailed(p):s.FailedPages++case p.Source == SourceBrowser:s.BrowserPages++@@ -396,6 +398,22 @@ func summarize(pages []Page, inherited []string) Summary {return s}+// pageFailed reports whether a page is a failure rather than a content sample: a+// transport error, or a 4xx/5xx response the browser did not recover. A 3xx or an+// unset (zero) status is not a failure — the threshold is >= 400 — so a redirect+// artefact or a page that never carried a status stays an ordinary page. A page the+// browser rendered into content (Source browser, which enrichPage sets only on+// success and where it clears Error) is not failed even if its HTTP status was+// >= 400, mirroring enrichPage's rule that a recovered fetch failure no longer marks+// the page failed; its statusCode is left intact. This is the one owner of the rule,+// read by both the summary partition and the thin-content count.+func PageFailed(p Page) bool {+ if p.Error != "" {+ return true+ }+ return p.StatusCode >= 400 && p.Source != SourceBrowser+}+// recommend keeps the crawl-scope advice the HTTP phase produced and regenerates the// page-content advice from the pages as they finally stand.func recommend(pages []Page, s Summary, inherited []string) []string {@@ -427,7 +445,7 @@ func recommend(pages []Page, s Summary, inherited []string) []string {// the errors line already reports.func thinPages(pages []Page) (thin, unenriched int) {for _, p := range pages {- if p.Error != "" || p.StatusCode >= 400 {+ if PageFailed(p) {continue}if contentChars(p) >= ThinContentChars {diff --git a/internal/scrape/run_test.go b/internal/scrape/run_test.goindex b3694eec..6d0e0c81 100644--- a/internal/scrape/run_test.go+++ b/internal/scrape/run_test.go@@ -627,6 +627,73 @@ func TestAGenuinelyFailedPageIsStillReported(t *testing.T) {}}+// PIN-412: a 4xx page is a failure even though it carries no transport Error. The+// summary must count it in failedPages (not httpPages), keep it out of contentTypes,+// keep the buckets summing to the page total, and fire the errors recommendation. A+// transport-Error-only fixture cannot prove this, since that page never reaches the+// StatusCode >= 400 arm.+func TestA404PageCountsAsFailedNotHTTP(t *testing.T) {+ crawl := crawlAdvising(nil,+ seaportal.PageObject{URL: "https://example.com/good", Status: 200, Markdown: longMarkdown, ContentType: "page"},+ seaportal.PageObject{URL: "https://example.com/missing", Status: 404, ContentType: "page"},+ )++ report, err := Run(context.Background(), Input{URL: "https://example.com"}, RunOptions{NoBrowser: true}, crawl, nil)+ if err != nil {+ t.Fatalf("Run: %v", err)+ }++ // The fixture only proves the fix if the 404 stayed a non-Error 4xx page.+ missing := report.Pages[1]+ if missing.StatusCode != 404 || missing.Error != "" {+ t.Fatalf("fixture 404 page is not a bare 4xx: status=%d error=%q", missing.StatusCode, missing.Error)+ }++ s := report.Summary+ if s.FailedPages != 1 {+ t.Errorf("FailedPages = %d, want 1 (the 404)", s.FailedPages)+ }+ if s.HTTPPages != 1 {+ t.Errorf("HTTPPages = %d, want 1 (only the 200)", s.HTTPPages)+ }+ if s.BrowserPages != 0 {+ t.Errorf("BrowserPages = %d, want 0", s.BrowserPages)+ }+ if got := s.HTTPPages + s.BrowserPages + s.FailedPages; got != len(report.Pages) {+ t.Errorf("buckets sum to %d, want the page total %d", got, len(report.Pages))+ }+ if s.ContentTypes["page"] != 1 {+ t.Errorf("contentTypes[page] = %d, want 1 — the 404 must not be counted as a page", s.ContentTypes["page"])+ }+ if rec := recommendationNaming(t, report, "returned errors"); rec == "" {+ t.Errorf("the 4xx page produced no errors recommendation: %v", s.Recommendations)+ }+}++// AC #3: a 3xx redirect and an unset-status page are NOT failures — the arm is+// >= 400, so they stay in the ordinary buckets and change nothing.+func TestRedirectAndUnsetStatusAreNotFailures(t *testing.T) {+ crawl := crawlAdvising(nil,+ seaportal.PageObject{URL: "https://example.com/redir", Status: 301, Markdown: longMarkdown, ContentType: "page"},+ seaportal.PageObject{URL: "https://example.com/nostatus", Status: 0, Markdown: longMarkdown, ContentType: "page"},+ )++ report, err := Run(context.Background(), Input{URL: "https://example.com"}, RunOptions{NoBrowser: true}, crawl, nil)+ if err != nil {+ t.Fatalf("Run: %v", err)+ }++ if report.Summary.FailedPages != 0 {+ t.Errorf("FailedPages = %d, want 0 for a 3xx and an unset-status page", report.Summary.FailedPages)+ }+ if report.Summary.HTTPPages != 2 {+ t.Errorf("HTTPPages = %d, want 2", report.Summary.HTTPPages)+ }+ if report.Summary.ContentTypes["page"] != 2 {+ t.Errorf("contentTypes[page] = %d, want 2 — neither page is a failure", report.Summary.ContentTypes["page"])+ }+}+// Crawl-scope advice describes which URLs were SAMPLED, which no amount of browser// rendering changes, so it must survive verbatim. Dropping advice that is still true is// the failure mode on the other side of this fix.