Backtestshugorun-20260930-140149#15206
ReadingTime and FuzzyWordCount have off-by-one errors in their ceiling calculations
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches make identical changes to hugolib/page__content.go. FuzzyWordCount now uses (wc+99)/100*100, and ReadingTime uses (wc+211)/212 and (wc+499)/500. This is the correct ceiling-division fix at the root cause, and it matches the issue's proposal exactly. Both also add the same integration test, TestReadingTimeAndFuzzyWordCountBoundaries. That test fails before the change (p100, p213, p500, p501) and passes after it. Correctness and test coverage are therefore equal. The difference is scope: Patch B also reformats about a dozen unrelated AssertFileContent/Assert call sites in page_test.go, splitting the first argument onto its own line. This is pure formatting churn with no connection to the issue. It inflates the diff, removes 16 lines from existing tests, and obscures the actual fix in review. Patch A is minimal and focused, so it wins on scope and craft.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
5m 16s11 turns
Wall-clock time in the container
Full test suite
No new failures
3,696 passed · 99 skipped · 3,804 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
80vs 128 merged
Changed lines, ours vs merged
The issue
View on GitHubSummary
Two related integer-arithmetic bugs in hugolib/page__content.go (in the plain-content calculation), causing behavior that deviates from documented intent at boundary values.
1. ReadingTime divides by 213/501 instead of 212/500
if isCJKLanguage {
result.readingTime = (result.wordCount + 500) / 501
} else {
result.readingTime = (result.wordCount + 212) / 213
}
The intent is a reading speed of 212 words per minute (500 for CJK), rounded up. But the correct ceiling-division idiom is (n + d - 1) / d. As written, the expression computes ceil(wordCount / 213) and ceil(wordCount / 501), so the effective reading speeds are 213 and 501 wpm.
| wordCount | current: (wc+212)/213 | expected at 212 wpm: ceil(wc/212) |
|---|---|---|
| 212 | 1 | 1 |
| 213 | **1** | **2** |
| 424 | 2 | 2 |
| 425 | **2** | **3** |
Proposed fix:
result.readingTime = (result.wordCount + 211) / 212 // non-CJK
result.readingTime = (result.wordCount + 499) / 500 // CJK
2. FuzzyWordCount overshoots on exact multiples of 100
result.fuzzyWordCount = (result.wordCount + 100) / 100 * 100
Documented as the word count "rounded up to the nearest multiple of 100", but adding 100 unconditionally bumps exact multiples to the next multiple:
| wordCount | current | expected |
|---|---|---|
| 99 | 100 | 100 |
| 100 | 200 | 100 |
| 101 | 200 | 200 |
| 0 | 100 | **0** |
Proposed fix:
result.fuzzyWordCount = (result.wordCount + 99) / 100 * 100
Both changes only affect output at exact boundary values, but they do change rendered output for existing sites (e.g., a 100-word page's FuzzyWordCount drops from 200 to 100), so this may warrant a changelog note.
Integration Test
<details>
<summary>hugolib/page_test.go</summary>
// See issue 15206.
func TestReadingTimeAndFuzzyWordCountBoundaries(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap"]
-- content/p99.md --
---
title: p99
---
` + strings.Repeat("word ", 99) + `
-- content/p100.md --
---
title: p100
---
` + strings.Repeat("word ", 100) + `
-- content/p101.md --
---
title: p101
---
` + strings.Repeat("word ", 101) + `
-- content/p211.md --
---
title: p211
---
` + strings.Repeat("word ", 211) + `
-- content/p212.md --
---
title: p212
---
` + strings.Repeat("word ", 212) + `
-- content/p213.md --
---
title: p213
---
` + strings.Repeat("word ", 213) + `
-- content/p499.md --
---
title: p499
isCJKLanguage: true
---
` + strings.Repeat("你", 499) + `
-- content/p500.md --
---
title: p500
isCJKLanguage: true
---
` + strings.Repeat("你", 500) + `
-- content/p501.md --
---
title: p501
isCJKLanguage: true
---
` + strings.Repeat("你", 501) + `
-- layouts/page.html --
{{ .WordCount }}|{{ .FuzzyWordCount }}|{{ .ReadingTime }}
`
b := Test(t, files)
b.AssertFileContent("public/p99/index.html", "99|100|1")
b.AssertFileContent("public/p100/index.html", "100|100|1") // fails
b.AssertFileContent("public/p101/index.html", "101|200|1")
b.AssertFileContent("public/p211/index.html", "211|300|1")
b.AssertFileContent("public/p212/index.html", "212|300|1")
b.AssertFileContent("public/p213/index.html", "213|300|2") // fails
b.AssertFileContent("public/p499/index.html", "499|500|1")
b.AssertFileContent("public/p500/index.html", "500|500|1") // fails
b.AssertFileContent("public/p501/index.html", "501|600|2") // fails
}
</details>
The two patches
hugolib/page__content.goindex c89d6b614..725576f38 100644@@ -807,13 +807,13 @@ func (c *cachedContentScope) contentPlain(ctx context.Context) (contentPlainPlai // TODO(bep) is set in a test. Fix that. if result.fuzzyWordCount == 0 {- result.fuzzyWordCount = (result.wordCount + 100) / 100 * 100+ result.fuzzyWordCount = (result.wordCount + 99) / 100 * 100 } if isCJKLanguage {- result.readingTime = (result.wordCount + 500) / 501+ result.readingTime = (result.wordCount + 499) / 500 } else {- result.readingTime = (result.wordCount + 212) / 213+ result.readingTime = (result.wordCount + 211) / 212 } rs.Value = resulthugolib/page_test.goindex cad453d5b..401636c5c 100644@@ -2368,3 +2368,77 @@ All. myothersection, _ := s.GetPage("myothersection") // backed by a content file. check(myothersection, true) }++// See issue 15206.+func TestReadingTimeAndFuzzyWordCountBoundaries(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap"]+-- content/p99.md --+---+title: p99+---+` + strings.Repeat("word ", 99) + `+-- content/p100.md --+---+title: p100+---+` + strings.Repeat("word ", 100) + `+-- content/p101.md --+---+title: p101+---+` + strings.Repeat("word ", 101) + `+-- content/p211.md --+---+title: p211+---+` + strings.Repeat("word ", 211) + `+-- content/p212.md --+---+title: p212+---+` + strings.Repeat("word ", 212) + `+-- content/p213.md --+---+title: p213+---+` + strings.Repeat("word ", 213) + `+-- content/p499.md --+---+title: p499+isCJKLanguage: true+---+` + strings.Repeat("你", 499) + `+-- content/p500.md --+---+title: p500+isCJKLanguage: true+---+` + strings.Repeat("你", 500) + `+-- content/p501.md --+---+title: p501+isCJKLanguage: true+---+` + strings.Repeat("你", 501) + `+-- layouts/page.html --+{{ .WordCount }}|{{ .FuzzyWordCount }}|{{ .ReadingTime }}+`++ b := Test(t, files)++ b.AssertFileContent("public/p99/index.html", "99|100|1")+ b.AssertFileContent("public/p100/index.html", "100|100|1")+ b.AssertFileContent("public/p101/index.html", "101|200|1")++ b.AssertFileContent("public/p211/index.html", "211|300|1")+ b.AssertFileContent("public/p212/index.html", "212|300|1")+ b.AssertFileContent("public/p213/index.html", "213|300|2")++ b.AssertFileContent("public/p499/index.html", "499|500|1")+ b.AssertFileContent("public/p500/index.html", "500|500|1")+ b.AssertFileContent("public/p501/index.html", "501|600|2")+}
hugolib/page__content.goindex c89d6b614..725576f38 100644@@ -807,13 +807,13 @@ func (c *cachedContentScope) contentPlain(ctx context.Context) (contentPlainPlai // TODO(bep) is set in a test. Fix that. if result.fuzzyWordCount == 0 {- result.fuzzyWordCount = (result.wordCount + 100) / 100 * 100+ result.fuzzyWordCount = (result.wordCount + 99) / 100 * 100 } if isCJKLanguage {- result.readingTime = (result.wordCount + 500) / 501+ result.readingTime = (result.wordCount + 499) / 500 } else {- result.readingTime = (result.wordCount + 212) / 213+ result.readingTime = (result.wordCount + 211) / 212 } rs.Value = resulthugolib/page_test.goindex cad453d5b..635358590 100644@@ -412,7 +412,8 @@ baseURL = "http://example.com/" p := b.H.Sites[0].RegularPages()[0] b.Assert(p.Summary(context.Background()), qt.Equals, template.HTML(- "<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup></p>"))+ "<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup></p>",+ )) cnt := content(p) b.Assert(cnt, qt.Equals, "<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup></p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"fn:1\">\n<p>Many people say so. <a href=\"#fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">↩︎</a></p>\n</li>\n</ol>\n</div>")@@ -730,7 +731,8 @@ This is **content**. Summary: {{ .Summary }}|Truncated: {{ .Truncated }}| Content: {{ .Content }}| -`).AssertFileContent("public/simple/index.html",+`).AssertFileContent(+ "public/simple/index.html", "Summary: <p>This is <strong>summary</strong>.</p>|", "Truncated: true|", "Content: <p>This is <strong>summary</strong>.</p>\n<p>This is <strong>content</strong>.</p>|",@@ -1286,13 +1288,15 @@ AllTranslations: {{ range .AllTranslations }}{{ .Language.Lang }}|{{ end }}| ` b := Test(t, files)- b.AssertFileContent("public/en/sect/p1/index.html",+ b.AssertFileContent(+ "public/en/sect/p1/index.html", "TranslationKey: adfasdf|", "AllTranslations: en|nn||", "Translations: nn||", ) - b.AssertFileContent("public/nn/sect/p1/index.html",+ b.AssertFileContent(+ "public/nn/sect/p1/index.html", "TranslationKey: adfasdf|", "Translations: en||", "AllTranslations: en|nn||",@@ -1378,12 +1382,14 @@ Resources: {{ range .Resources }}{{ .RelPermalink }}|{{ .Content }}|{{ end }}| ` b := Test(t, files)- b.AssertFileContent("public/en/sect/mybundle_en/index.html",+ b.AssertFileContent(+ "public/en/sect/mybundle_en/index.html", "TranslationKey: adfasdf|", "Resources: /en/sect/mybundle_en/f1.txt|f1.en|/en/sect/mybundle_en/f2.txt|f2.en||", ) - b.AssertFileContent("public/nn/sect/mybundle_nn/index.html",+ b.AssertFileContent(+ "public/nn/sect/mybundle_nn/index.html", "TranslationKey: adfasdf|", "Title: mybundle nn|TranslationKey: adfasdf|\nResources: /en/sect/mybundle_en/f1.txt|f1.en|/nn/sect/mybundle_nn/f2.nn.txt|f2.nn||", )@@ -1497,31 +1503,37 @@ CONTENT:{{ .Content }} ` b := Test(t, files) - b.AssertFileContent("public/page-md-shortcode/index.html",+ b.AssertFileContent(+ "public/page-md-shortcode/index.html", "SUMMARY:<p>This is a a shortcode.</p>:END", "CONTENT:<p>This is a a shortcode.</p>\n\n<p>Content.</p>\n", ) - b.AssertFileContent("public/page-md-shortcode-same-line/index.html",+ b.AssertFileContent(+ "public/page-md-shortcode-same-line/index.html", "SUMMARY:<p>This is a a shortcode</p>:END", "CONTENT:<p>This is a a shortcode</p>\n\n<p>Same line.</p>\n", ) - b.AssertFileContent("public/page-md-shortcode-same-line-after/index.html",+ b.AssertFileContent(+ "public/page-md-shortcode-same-line-after/index.html", "SUMMARY:<p>Summary</p>:END", "CONTENT:<p>Summary</p>\n\na shortcode", ) - b.AssertFileContent("public/page-org-shortcode/index.html",+ b.AssertFileContent(+ "public/page-org-shortcode/index.html", "SUMMARY:<p>\nThis is a a shortcode.\n</p>:END", "CONTENT:<p>\nThis is a a shortcode.\n</p>\n<p>\nContent.\t\n</p>\n", )- b.AssertFileContent("public/page-org-variant1/index.html",+ b.AssertFileContent(+ "public/page-org-variant1/index.html", "SUMMARY:<p>\nSummary.\n</p>:END", "CONTENT:<p>\nSummary.\n</p>\n<p>\nContent.\t\n</p>\n", ) - b.AssertFileContent("public/page-md-only-shortcode/index.html",+ b.AssertFileContent(+ "public/page-md-only-shortcode/index.html", "SUMMARY:a shortcode:END", "CONTENT:a shortcode\n\na shortcode\n", )@@ -1731,7 +1743,8 @@ c: {{ .Scratch.Get "c" }} b := Test(t, files) - b.AssertFileContent("public/index.html",+ b.AssertFileContent(+ "public/index.html", ".Scratch eq .Store: true", "a: b", "c: d",@@ -1928,7 +1941,8 @@ Site: {{ site.Store.Get "Site" }}| b := TestRunning(t, files) - b.AssertFileContent("public/index.html",+ b.AssertFileContent(+ "public/index.html", ` Shortcode: sh-Home| Page: p-Home|@@ -1939,7 +1953,8 @@ Hugo: h-Home| b.EditFileReplaceAll("content/_index.md", "Home", "Homer").Build() - b.AssertFileContent("public/index.html",+ b.AssertFileContent(+ "public/index.html", ` Shortcode: sh-Homer| Page: p-Homer|@@ -2149,7 +2164,8 @@ EF b := Test(t, files) - b.AssertFileContent("public/s1/p1/index.html",+ b.AssertFileContent(+ "public/s1/p1/index.html", "ab|ab|ab", "cD|cD|cD", "EF|EF|EF",@@ -2368,3 +2384,77 @@ All. myothersection, _ := s.GetPage("myothersection") // backed by a content file. check(myothersection, true) }++// See issue 15206.+func TestReadingTimeAndFuzzyWordCountBoundaries(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap"]+-- content/p99.md --+---+title: p99+---+` + strings.Repeat("word ", 99) + `+-- content/p100.md --+---+title: p100+---+` + strings.Repeat("word ", 100) + `+-- content/p101.md --+---+title: p101+---+` + strings.Repeat("word ", 101) + `+-- content/p211.md --+---+title: p211+---+` + strings.Repeat("word ", 211) + `+-- content/p212.md --+---+title: p212+---+` + strings.Repeat("word ", 212) + `+-- content/p213.md --+---+title: p213+---+` + strings.Repeat("word ", 213) + `+-- content/p499.md --+---+title: p499+isCJKLanguage: true+---+` + strings.Repeat("你", 499) + `+-- content/p500.md --+---+title: p500+isCJKLanguage: true+---+` + strings.Repeat("你", 500) + `+-- content/p501.md --+---+title: p501+isCJKLanguage: true+---+` + strings.Repeat("你", 501) + `+-- layouts/page.html --+{{ .WordCount }}|{{ .FuzzyWordCount }}|{{ .ReadingTime }}+`++ b := Test(t, files)++ b.AssertFileContent("public/p99/index.html", "99|100|1")+ b.AssertFileContent("public/p100/index.html", "100|100|1")+ b.AssertFileContent("public/p101/index.html", "101|200|1")++ b.AssertFileContent("public/p211/index.html", "211|300|1")+ b.AssertFileContent("public/p212/index.html", "212|300|1")+ b.AssertFileContent("public/p213/index.html", "213|300|2")++ b.AssertFileContent("public/p499/index.html", "499|500|1")+ b.AssertFileContent("public/p500/index.html", "500|500|1")+ b.AssertFileContent("public/p501/index.html", "501|600|2")+}
What the agent reported
I fixed both rounding bugs and added the report's regression test. However, I could not run any Hugo tests: the container has no Go dependencies cached and can't download them, so every ./bt-test ./hugolib ... run stopped at FAIL github.com/gohugoio/hugo/hugolib [setup failed]. Neither the new test nor the existing ones have been seen to pass.
What was wrong: in hugolib/page__content.go, the rounding-up formulas used the wrong numbers.
ReadingTimedivided by 213 (and 501 for CJK), so a 213-word page counted as 1 minute instead of 2.FuzzyWordCountalways added 100 before rounding, so a 100-word page showed 200 and an empty page showed 100.
What I changed:
- Fix (
hugolib/page__content.go:810-816): the report's three formulas —(wc+99)/100*100,(wc+499)/500for CJK and(wc+211)/212otherwise. - Test:
TestReadingTimeAndFuzzyWordCountBoundaries, copied word for word from the report onto the end ofhugolib/page_test.go.gofmtfinds nothing to fix. - Nothing else changed. The deleted AGENTS.md and CLAUDE.md were already deleted before I started.
Test evidence:
- Arithmetic check: I ran the old and new formulas in a small standalone Go program outside the repo, then deleted it.
- The new formulas give every value the test expects: 99→100|1, 100→100|1, 101→200|1, 211/212→300|1, 213→300|2, CJK 499→500|1, 500→500|1, 501→600|2, and 0 words→0|0.
- The old formulas get p100, p213, p500 and p501 wrong, the same four the report flags. So the test should fail before the fix and pass after it.
- Existing tests: the reviewer went through every existing test that checks these values, and I spot-checked several. None should change:
TestWordCount(483 words) stays 500 and 3.TestPassPageToI18n(about 490 words, counted by hand) stays 3.- The rest use small, non-zero word counts that stay 100 and 1, or set
fuzzyWordCountdirectly on mock pages.
Requirements:
- Reading time uses 212 words per minute (500 for CJK), rounded up: done. The docs (
docs/content/en/methods/page/ReadingTime.md) already say 212 and 500, so they needed no change. - Fuzzy word count rounds up to the nearest multiple of 100, so exact multiples stay put and 0 words gives 0: done, and it matches
docs/content/en/methods/page/FuzzyWordCount.md. - Integration test from the report: added word for word, but not run, for the reason above.
- Changelog note: skipped. The repo has no changelog file; Hugo builds release notes from commit messages. Since I was told not to commit, whoever commits this should mention the visible change: a 100-word page's
FuzzyWordCountgoes from 200 to 100, and an empty page's from 100 to 0.
Once the Go modules can be downloaded, run ./bt-test ./hugolib -run 'TestReadingTimeAndFuzzyWordCountBoundaries|TestWordCount' to get a real result.