FAILURE MAP
← Case archive

FA-79886 / Typography line breaking / Open access

Total-fit paragraph demerits: adjacent fitness penalty · case 01

Neighbouring lines one class apart are penalised as visually incompatible.

Verified by executionVariant 1 · 8 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

The adjacent-demerit test uses >= 1 so any class change costs 3000.

THE FAILURE

The adjacent-demerit test uses >= 1 so any class change costs 3000.

Unsuccessful approach: Checking only the upward direction misses tight-after-loose jumps.

Case contract

Input [word widths, [space, stretch, shrink] per gap, line width, line penalty]. A line i..j has gaps=j-i-1. The last line has zero badness when not overfull. Other lines use ratio shortfall/(stretch*gaps) or /(shrink*gaps); zero stretch with slack or compression past shrink is infeasible; badness=min(10000, floor(100|r|^3+1/2)) and must be <=1000. Fitness 0 tight (r<-1/2), 1 decent (r<=1/2), 2 loose (r<=1), 3 very loose; last line decent. Demerits (lp+b)^2 plus 3000 when adjacent fitness classes differ by more than 1, starting from decent. Return [min total demerits, break list] (ties: first found) or ["infeasible"].

Why this case matters

Line breaking decides where paragraphs wrap on screen and in print; a wrong decision point shifts every following line.

1 / The failure

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    words, glue, width, lp = x
    sp, st, sh = glue
    n = len(words)
    def rate(i, j):
        gaps = j - i - 1
        nat = sum(words[i:j]) + sp * gaps
        short = width - nat
        if j == n and short >= 0:
            return 0, 1
        if short > 0:
            if st * gaps == 0:
                return None
            r = Fraction(short, st * gaps)
        elif short < 0:
            if -short > sh * gaps:
                return None
            r = Fraction(short, sh * gaps)
        else:
            r = Fraction(0)
        b = min(10000, math.floor(100 * abs(r) ** 3 + Fraction(1, 2)))
        if b > 1000:
            return None
        fit = 0 if r < Fraction(-1, 2) else 1 if r <= Fraction(1, 2) else 2 if r <= 1 else 3
        return b, fit
    best = {(0, 1): (0, [])}
    for j in range(1, n + 1):
        for i in range(j):
            rated = rate(i, j)
            if rated is None:
                continue
            b, fit = rated
            for (pos, pfit), (dem, brk) in sorted(best.items()):
                if pos != i:
                    continue
                d = dem + (lp + b) ** 2
                if abs(fit - pfit) >= 1:
                    d += 3000
                key = (j, fit)
                if key not in best or d < best[key][0]:
                    best[key] = (d, brk + [j])
    finals = [v for (pos, f), v in best.items() if pos == n]
    if not finals:
        return ['infeasible']
    d, brk = min(finals)
    return [d, brk]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('regression: adjacent fitness penalty', [[3, 6, 2, 2, 3, 1, 6], [3, 2, 1], 14, 1], [10211, [2, 5, 7]]), ('partial-repair probe', [[6, 6, 7, 3, 4, 4], [1, 1, 2], 15, 1], [647611, [2, 5, 6]]), ('partial-repair probe', [[1, 3, 7], [3, 2, 2], 11, 1], [647602, [2, 3]]), ('last line overfull', [[5, 5, 5], [1, 1, 0], 9, 10], ['infeasible']), ('tight then loose lines', [[3, 3, 3, 3, 3, 3, 3], [2, 2, 1], 11, 1], [20403, [3, 6, 7]]), ('control layout', [[4, 5, 6], [1, 1, 1], 10, 10], [200, [2, 3]]), ('control layout', [[4, 5, 6, 3, 3, 6, 2, 5], [3, 3, 0], 24, 50], [8969, [3, 7, 8]])], [('regression: adjacent fitness penalty', [[1, 7, 7, 2, 4, 1, 3, 4], [1, 1, 2], 13, 50], [27500, [3, 7, 8]]), ('regression: adjacent fitness penalty', [[4, 2, 3, 4], [2, 1, 0], 15, 10], [12200, [3, 4]]), ('partial-repair probe', [[7, 5, 2, 3, 3], [3, 1, 0], 17, 10], [662200, [2, 5]]), ('regression: adjacent fitness penalty', [[4, 7, 6, 7, 2, 7, 6, 4], [1, 1, 1], 22, 10], [139304, [3, 7, 8]]), ('single word paragraph', [[7], [2, 2, 1], 10, 10], [100, [1]]), ('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('control layout', [[3, 3, 5], [3, 1, 2], 21, 10], [100, [3]]), ('control layout', [[5, 5, 3, 6], [1, 1, 1], 22, 1], [1, [4]])], [('regression: adjacent fitness penalty', [[3, 6, 2, 2, 3, 1, 6], [3, 2, 1], 14, 1], [10211, [2, 5, 7]]), ('regression: adjacent fitness penalty', [[4, 2, 7, 3, 4], [2, 1, 1], 15, 50], [25000, [3, 5]]), ('partial-repair probe', [[5, 1, 6, 4, 7, 7, 1, 4], [1, 1, 1], 14, 1], [647603, [3, 5, 8]]), ('regression: adjacent fitness penalty', [[5, 7, 4, 7, 6, 4, 4, 1], [2, 1, 1], 24, 10], [674300, [3, 7, 8]]), ('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('single word paragraph', [[7], [2, 2, 1], 10, 10], [100, [1]]), ('control layout', [[1, 2, 5], [3, 3, 2], 13, 1], [9, [3]]), ('control layout', [[2, 3, 2, 2, 2, 4, 2, 7], [3, 3, 1], 9, 10], ['infeasible'])], [('regression: adjacent fitness penalty', [[2, 7, 2, 3, 3, 4], [3, 2, 0], 21, 50], [25000, [3, 6]]), ('regression: adjacent fitness penalty', [[7, 4, 3, 3, 6, 3, 7], [1, 1, 1], 13, 10], [12729, [2, 5, 7]]), ('partial-repair probe', [[2, 1, 4, 6, 1, 5], [3, 1, 1], 16, 50], [179044, [3, 6]]), ('partial-repair probe', [[5, 1, 5, 4], [2, 2, 1], 12, 1], [647602, [2, 4]]), ('single word paragraph', [[7], [2, 2, 1], 10, 10], [100, [1]]), ('last line overfull', [[5, 5, 5], [1, 1, 0], 9, 10], ['infeasible']), ('control layout', [[4, 5, 4, 4], [1, 3, 2], 23, 1], [1, [4]]), ('control layout', [[7, 2, 2, 5, 7, 5], [3, 1, 0], 24, 10], ['infeasible'])], [('regression: adjacent fitness penalty', [[4, 4, 5, 4, 5], [1, 1, 1], 11, 10], [671300, [2, 4, 5]]), ('regression: adjacent fitness penalty', [[4, 2, 3, 4], [2, 1, 0], 15, 10], [12200, [3, 4]]), ('partial-repair probe', [[4, 1, 2, 3, 1, 7, 5, 1], [3, 2, 2], 11, 1], [235844, [2, 4, 6, 8]]), ('partial-repair probe', [[7, 6, 6, 6, 5, 5, 2, 4], [2, 2, 0], 15, 1], [121119, [2, 4, 6, 8]]), ('tight then loose lines', [[3, 3, 3, 3, 3, 3, 3], [2, 2, 1], 11, 1], [20403, [3, 6, 7]]), ('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('control layout', [[7, 4, 6, 5, 6, 4], [2, 2, 2], 23, 50], [6469, [3, 6]]), ('control layout', [[6, 1, 2], [2, 2, 2], 14, 10], [100, [3]])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
two-line paragraph with loose first line[18200, [3, 4]][12200, [3, 4]]Failed
regression: adjacent fitness penalty[16211, [2, 5, 7]][10211, [2, 5, 7]]Failed
partial-repair probe[647611, [2, 5, 6]][647611, [2, 5, 6]]Passed
partial-repair probe[647602, [2, 3]][647602, [2, 3]]Passed
last line overfull['infeasible']['infeasible']Passed
tight then loose lines[26403, [3, 6, 7]][20403, [3, 6, 7]]Failed
control layout[200, [2, 3]][200, [2, 3]]Passed
control layout[8969, [3, 7, 8]][8969, [3, 7, 8]]Passed

SHA-256 / 1e83117599a4a24457674495465ffa67cd3467df1f508c9c715b8a599561647c

2 / The unsuccessful fix

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    words, glue, width, lp = x
    sp, st, sh = glue
    n = len(words)
    def rate(i, j):
        gaps = j - i - 1
        nat = sum(words[i:j]) + sp * gaps
        short = width - nat
        if j == n and short >= 0:
            return 0, 1
        if short > 0:
            if st * gaps == 0:
                return None
            r = Fraction(short, st * gaps)
        elif short < 0:
            if -short > sh * gaps:
                return None
            r = Fraction(short, sh * gaps)
        else:
            r = Fraction(0)
        b = min(10000, math.floor(100 * abs(r) ** 3 + Fraction(1, 2)))
        if b > 1000:
            return None
        fit = 0 if r < Fraction(-1, 2) else 1 if r <= Fraction(1, 2) else 2 if r <= 1 else 3
        return b, fit
    best = {(0, 1): (0, [])}
    for j in range(1, n + 1):
        for i in range(j):
            rated = rate(i, j)
            if rated is None:
                continue
            b, fit = rated
            for (pos, pfit), (dem, brk) in sorted(best.items()):
                if pos != i:
                    continue
                d = dem + (lp + b) ** 2
                if fit - pfit > 1:
                    d += 3000
                key = (j, fit)
                if key not in best or d < best[key][0]:
                    best[key] = (d, brk + [j])
    finals = [v for (pos, f), v in best.items() if pos == n]
    if not finals:
        return ['infeasible']
    d, brk = min(finals)
    return [d, brk]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('regression: adjacent fitness penalty', [[3, 6, 2, 2, 3, 1, 6], [3, 2, 1], 14, 1], [10211, [2, 5, 7]]), ('partial-repair probe', [[6, 6, 7, 3, 4, 4], [1, 1, 2], 15, 1], [647611, [2, 5, 6]]), ('partial-repair probe', [[1, 3, 7], [3, 2, 2], 11, 1], [647602, [2, 3]]), ('last line overfull', [[5, 5, 5], [1, 1, 0], 9, 10], ['infeasible']), ('tight then loose lines', [[3, 3, 3, 3, 3, 3, 3], [2, 2, 1], 11, 1], [20403, [3, 6, 7]]), ('control layout', [[4, 5, 6], [1, 1, 1], 10, 10], [200, [2, 3]]), ('control layout', [[4, 5, 6, 3, 3, 6, 2, 5], [3, 3, 0], 24, 50], [8969, [3, 7, 8]])], [('regression: adjacent fitness penalty', [[1, 7, 7, 2, 4, 1, 3, 4], [1, 1, 2], 13, 50], [27500, [3, 7, 8]]), ('regression: adjacent fitness penalty', [[4, 2, 3, 4], [2, 1, 0], 15, 10], [12200, [3, 4]]), ('partial-repair probe', [[7, 5, 2, 3, 3], [3, 1, 0], 17, 10], [662200, [2, 5]]), ('regression: adjacent fitness penalty', [[4, 7, 6, 7, 2, 7, 6, 4], [1, 1, 1], 22, 10], [139304, [3, 7, 8]]), ('single word paragraph', [[7], [2, 2, 1], 10, 10], [100, [1]]), ('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('control layout', [[3, 3, 5], [3, 1, 2], 21, 10], [100, [3]]), ('control layout', [[5, 5, 3, 6], [1, 1, 1], 22, 1], [1, [4]])], [('regression: adjacent fitness penalty', [[3, 6, 2, 2, 3, 1, 6], [3, 2, 1], 14, 1], [10211, [2, 5, 7]]), ('regression: adjacent fitness penalty', [[4, 2, 7, 3, 4], [2, 1, 1], 15, 50], [25000, [3, 5]]), ('partial-repair probe', [[5, 1, 6, 4, 7, 7, 1, 4], [1, 1, 1], 14, 1], [647603, [3, 5, 8]]), ('regression: adjacent fitness penalty', [[5, 7, 4, 7, 6, 4, 4, 1], [2, 1, 1], 24, 10], [674300, [3, 7, 8]]), ('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('single word paragraph', [[7], [2, 2, 1], 10, 10], [100, [1]]), ('control layout', [[1, 2, 5], [3, 3, 2], 13, 1], [9, [3]]), ('control layout', [[2, 3, 2, 2, 2, 4, 2, 7], [3, 3, 1], 9, 10], ['infeasible'])], [('regression: adjacent fitness penalty', [[2, 7, 2, 3, 3, 4], [3, 2, 0], 21, 50], [25000, [3, 6]]), ('regression: adjacent fitness penalty', [[7, 4, 3, 3, 6, 3, 7], [1, 1, 1], 13, 10], [12729, [2, 5, 7]]), ('partial-repair probe', [[2, 1, 4, 6, 1, 5], [3, 1, 1], 16, 50], [179044, [3, 6]]), ('partial-repair probe', [[5, 1, 5, 4], [2, 2, 1], 12, 1], [647602, [2, 4]]), ('single word paragraph', [[7], [2, 2, 1], 10, 10], [100, [1]]), ('last line overfull', [[5, 5, 5], [1, 1, 0], 9, 10], ['infeasible']), ('control layout', [[4, 5, 4, 4], [1, 3, 2], 23, 1], [1, [4]]), ('control layout', [[7, 2, 2, 5, 7, 5], [3, 1, 0], 24, 10], ['infeasible'])], [('regression: adjacent fitness penalty', [[4, 4, 5, 4, 5], [1, 1, 1], 11, 10], [671300, [2, 4, 5]]), ('regression: adjacent fitness penalty', [[4, 2, 3, 4], [2, 1, 0], 15, 10], [12200, [3, 4]]), ('partial-repair probe', [[4, 1, 2, 3, 1, 7, 5, 1], [3, 2, 2], 11, 1], [235844, [2, 4, 6, 8]]), ('partial-repair probe', [[7, 6, 6, 6, 5, 5, 2, 4], [2, 2, 0], 15, 1], [121119, [2, 4, 6, 8]]), ('tight then loose lines', [[3, 3, 3, 3, 3, 3, 3], [2, 2, 1], 11, 1], [20403, [3, 6, 7]]), ('two-line paragraph with loose first line', [[4, 5, 3, 6], [2, 2, 1], 14, 10], [12200, [3, 4]]), ('control layout', [[7, 4, 6, 5, 6, 4], [2, 2, 2], 23, 50], [6469, [3, 6]]), ('control layout', [[6, 1, 2], [2, 2, 2], 14, 10], [100, [3]])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
two-line paragraph with loose first line[12200, [3, 4]][12200, [3, 4]]Passed
regression: adjacent fitness penalty[10211, [2, 5, 7]][10211, [2, 5, 7]]Passed
partial-repair probe[644611, [2, 5, 6]][647611, [2, 5, 6]]Failed
partial-repair probe[644602, [2, 3]][647602, [2, 3]]Failed
last line overfull['infeasible']['infeasible']Passed
tight then loose lines[20403, [3, 6, 7]][20403, [3, 6, 7]]Passed
control layout[200, [2, 3]][200, [2, 3]]Passed
control layout[8969, [3, 7, 8]][8969, [3, 7, 8]]Passed

SHA-256 / 8c80ff31375d4ce972da4a6a3d6e0db4a474db6ee6079c31b6401293972e3b1b

HELD IN THE MEMBER ARCHIVE

The verified repair and its recorded checks are member-only.

This mechanism has 8 recorded checks per implementation. The open-access tier publishes the failure and the unsuccessful fix; the repaired source that passes every check, and the observations that prove it, are available to members.

Every case sharing this mechanism uses the same contract and the same repair, so this one record is held back for all of them.

Member access is invitation-based. Sign in with your invited account to inspect the repair.

Sign in to the archive ↗

Verification & scope

A deterministic toy typesetting model with integer widths and a stipulated rule set; it does not claim conformance to any engine. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.

Observations recorded using Python 3.12.14 at 2026-09-29T14:49:48.852596+00:00.

Case digest / 46d39c326eb3b00695b5a4adc1aea3e923a17fbf71825defe0f7e2d555beafce