FAILURE MAP
← Case archive

FA-80706 / Bidirectional text layout / Open access

Bidi hit testing: caret offset from edge · case 01

The caret lands on the wrong side of the hit character.

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

ROOT CAUSE

The caret offset is inverted relative to the trailing flag.

VERIFIED REPAIR

Offset is index + 1 for the trailing side, else index.

Unsuccessful approach: Using the visual half instead of the logical side misplaces RTL carets.

Case contract

Input [levels, x in half-character units]. Clamp to a visual slot (each character 2 units). The right half of an LTR character or the left half of an RTL character is its trailing side. Points before the line use the left half of slot 0, beyond it the right half of the last slot. Return [logical index, trailing, caret offset].

Why this case matters

Mixed right-to-left and left-to-right text must resolve levels and visual order exactly, or words, numbers and carets land in the wrong place.

1 / The failure

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis
    
    levels, px = x
    n = len(levels)
    if n == 0:
        return [None, False, 0]
    vis = reorder(levels)
    v = min(max(px // 2, 0), n - 1)
    i = vis[v]
    right_half = px - 2 * v >= 1
    rtl = levels[i] % 2 == 1
    trailing = right_half != rtl
    return [i, trailing, i if trailing else i + 1]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('click left of RTL line', [[1, 1, 1], -1], [2, True, 3]), ('regression: caret offset from edge', [[2, 2, 0, 0, 0], 13], [4, True, 5]), ('regression: caret offset from edge', [[3, 3], 5], [0, False, 0]), ('click past end', [[0, 1, 1], 9], [1, False, 1]), ('right half of RTL char', [[1, 1], 1], [1, False, 1]), ('regression: caret offset from edge', [[3, 3, 3, 1, 1, 1, 1, 1, 1, 3], 6], [6, True, 7]), ('regression: caret offset from edge', [[4, 1, 1, 1], 11], [0, True, 1]), ('regression: caret offset from edge', [[3, 3], -1], [1, True, 2])], [('regression: caret offset from edge', [[3, 3, 3, 3, 4, 4, 4, 1], 8], [3, True, 4]), ('regression: caret offset from edge', [[2, 2, 2, 2], -2], [0, False, 0]), ('regression: caret offset from edge', [[1, 1, 1, 4, 4, 2, 2], 17], [0, False, 0]), ('regression: caret offset from edge', [[1, 1, 1, 1, 1, 3, 3, 3, 3], -1], [8, True, 9]), ('click past end', [[0, 1, 1], 9], [1, False, 1]), ('click left of RTL line', [[1, 1, 1], -1], [2, True, 3]), ('regression: caret offset from edge', [[0, 1, 1, 1, 1, 0, 0, 0], 15], [7, True, 8]), ('regression: caret offset from edge', [[0, 0], 7], [1, True, 2])], [('regression: caret offset from edge', [[2, 2, 2, 2], 4], [2, False, 2]), ('regression: caret offset from edge', [[1, 1, 1, 1, 2, 2, 2, 2, 3, 2], 23], [0, False, 0]), ('click past end', [[0, 1, 1], 9], [1, False, 1]), ('regression: caret offset from edge', [[1, 1, 1, 2, 2, 2, 2, 1, 1, 1], -3], [9, True, 10]), ('right half of RTL char', [[1, 1], 1], [1, False, 1]), ('click left of RTL line', [[1, 1, 1], -1], [2, True, 3]), ('regression: caret offset from edge', [[1], -2], [0, True, 1]), ('regression: caret offset from edge', [[4, 4, 4, 4, 4, 4, 4, 2, 2, 2], 8], [4, False, 4])], [('regression: caret offset from edge', [[0, 0, 0, 1, 1, 1, 1, 3, 3], 3], [1, True, 2]), ('regression: caret offset from edge', [[1, 1, 1, 1, 1, 1, 0, 0, 3], 6], [2, True, 3]), ('regression: caret offset from edge', [[1, 1, 1], 8], [0, False, 0]), ('regression: caret offset from edge', [[3, 3, 3, 3, 1, 1, 1], -2], [6, True, 7]), ('right half of RTL char', [[1, 1], 1], [1, False, 1]), ('click past end', [[0, 1, 1], 9], [1, False, 1]), ('regression: caret offset from edge', [[2, 2, 3, 3, 1, 1, 1], 8], [1, False, 1]), ('regression: caret offset from edge', [[1, 1, 1, 1, 1, 3, 3, 3, 3], -1], [8, True, 9])], [('regression: caret offset from edge', [[3, 3, 3, 1, 1, 1, 1, 2], 2], [6, True, 7]), ('regression: caret offset from edge', [[2], 4], [0, True, 1]), ('regression: caret offset from edge', [[3, 3, 3, 2, 2], -2], [2, True, 3]), ('regression: caret offset from edge', [[3, 3, 3, 3, 1, 1, 1, 1], 18], [0, False, 0]), ('right half of RTL char', [[1, 1], 1], [1, False, 1]), ('click left of RTL line', [[1, 1, 1], -1], [2, True, 3]), ('regression: caret offset from edge', [[0, 0, 0, 0, 0, 0, 1, 1, 2, 0], 13], [8, True, 9]), ('regression: caret offset from edge', [[4, 4, 3], 1], [2, False, 2])]]
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
click left of RTL line[2, True, 2][2, True, 3]Failed
regression: caret offset from edge[4, True, 4][4, True, 5]Failed
regression: caret offset from edge[0, False, 1][0, False, 0]Failed
click past end[1, False, 2][1, False, 1]Failed
right half of RTL char[1, False, 2][1, False, 1]Failed
regression: caret offset from edge[6, True, 6][6, True, 7]Failed
regression: caret offset from edge[0, True, 0][0, True, 1]Failed
regression: caret offset from edge[1, True, 1][1, True, 2]Failed

SHA-256 / 73040a7a1852cae761ba16e1eeeb412d575b271440f354ef16f590c2273c91da

2 / The unsuccessful fix

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis
    
    levels, px = x
    n = len(levels)
    if n == 0:
        return [None, False, 0]
    vis = reorder(levels)
    v = min(max(px // 2, 0), n - 1)
    i = vis[v]
    right_half = px - 2 * v >= 1
    rtl = levels[i] % 2 == 1
    trailing = right_half != rtl
    return [i, trailing, i + 1 if right_half else i]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('click left of RTL line', [[1, 1, 1], -1], [2, True, 3]), ('regression: caret offset from edge', [[2, 2, 0, 0, 0], 13], [4, True, 5]), ('regression: caret offset from edge', [[3, 3], 5], [0, False, 0]), ('click past end', [[0, 1, 1], 9], [1, False, 1]), ('right half of RTL char', [[1, 1], 1], [1, False, 1]), ('regression: caret offset from edge', [[3, 3, 3, 1, 1, 1, 1, 1, 1, 3], 6], [6, True, 7]), ('regression: caret offset from edge', [[4, 1, 1, 1], 11], [0, True, 1]), ('regression: caret offset from edge', [[3, 3], -1], [1, True, 2])], [('regression: caret offset from edge', [[3, 3, 3, 3, 4, 4, 4, 1], 8], [3, True, 4]), ('regression: caret offset from edge', [[2, 2, 2, 2], -2], [0, False, 0]), ('regression: caret offset from edge', [[1, 1, 1, 4, 4, 2, 2], 17], [0, False, 0]), ('regression: caret offset from edge', [[1, 1, 1, 1, 1, 3, 3, 3, 3], -1], [8, True, 9]), ('click past end', [[0, 1, 1], 9], [1, False, 1]), ('click left of RTL line', [[1, 1, 1], -1], [2, True, 3]), ('regression: caret offset from edge', [[0, 1, 1, 1, 1, 0, 0, 0], 15], [7, True, 8]), ('regression: caret offset from edge', [[0, 0], 7], [1, True, 2])], [('regression: caret offset from edge', [[2, 2, 2, 2], 4], [2, False, 2]), ('regression: caret offset from edge', [[1, 1, 1, 1, 2, 2, 2, 2, 3, 2], 23], [0, False, 0]), ('click past end', [[0, 1, 1], 9], [1, False, 1]), ('regression: caret offset from edge', [[1, 1, 1, 2, 2, 2, 2, 1, 1, 1], -3], [9, True, 10]), ('right half of RTL char', [[1, 1], 1], [1, False, 1]), ('click left of RTL line', [[1, 1, 1], -1], [2, True, 3]), ('regression: caret offset from edge', [[1], -2], [0, True, 1]), ('regression: caret offset from edge', [[4, 4, 4, 4, 4, 4, 4, 2, 2, 2], 8], [4, False, 4])], [('regression: caret offset from edge', [[0, 0, 0, 1, 1, 1, 1, 3, 3], 3], [1, True, 2]), ('regression: caret offset from edge', [[1, 1, 1, 1, 1, 1, 0, 0, 3], 6], [2, True, 3]), ('regression: caret offset from edge', [[1, 1, 1], 8], [0, False, 0]), ('regression: caret offset from edge', [[3, 3, 3, 3, 1, 1, 1], -2], [6, True, 7]), ('right half of RTL char', [[1, 1], 1], [1, False, 1]), ('click past end', [[0, 1, 1], 9], [1, False, 1]), ('regression: caret offset from edge', [[2, 2, 3, 3, 1, 1, 1], 8], [1, False, 1]), ('regression: caret offset from edge', [[1, 1, 1, 1, 1, 3, 3, 3, 3], -1], [8, True, 9])], [('regression: caret offset from edge', [[3, 3, 3, 1, 1, 1, 1, 2], 2], [6, True, 7]), ('regression: caret offset from edge', [[2], 4], [0, True, 1]), ('regression: caret offset from edge', [[3, 3, 3, 2, 2], -2], [2, True, 3]), ('regression: caret offset from edge', [[3, 3, 3, 3, 1, 1, 1, 1], 18], [0, False, 0]), ('right half of RTL char', [[1, 1], 1], [1, False, 1]), ('click left of RTL line', [[1, 1, 1], -1], [2, True, 3]), ('regression: caret offset from edge', [[0, 0, 0, 0, 0, 0, 1, 1, 2, 0], 13], [8, True, 9]), ('regression: caret offset from edge', [[4, 4, 3], 1], [2, False, 2])]]
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
click left of RTL line[2, True, 2][2, True, 3]Failed
regression: caret offset from edge[4, True, 5][4, True, 5]Passed
regression: caret offset from edge[0, False, 1][0, False, 0]Failed
click past end[1, False, 2][1, False, 1]Failed
right half of RTL char[1, False, 2][1, False, 1]Failed
regression: caret offset from edge[6, True, 6][6, True, 7]Failed
regression: caret offset from edge[0, True, 1][0, True, 1]Passed
regression: caret offset from edge[1, True, 1][1, True, 2]Failed

SHA-256 / a723bd0d79329dcb10279f885383036ea42be87b110a5824ae4d64858d879484

3 / The verified repair

Exit 0
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis
    
    levels, px = x
    n = len(levels)
    if n == 0:
        return [None, False, 0]
    vis = reorder(levels)
    v = min(max(px // 2, 0), n - 1)
    i = vis[v]
    right_half = px - 2 * v >= 1
    rtl = levels[i] % 2 == 1
    trailing = right_half != rtl
    return [i, trailing, i + 1 if trailing else i]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('click left of RTL line', [[1, 1, 1], -1], [2, True, 3]), ('regression: caret offset from edge', [[2, 2, 0, 0, 0], 13], [4, True, 5]), ('regression: caret offset from edge', [[3, 3], 5], [0, False, 0]), ('click past end', [[0, 1, 1], 9], [1, False, 1]), ('right half of RTL char', [[1, 1], 1], [1, False, 1]), ('regression: caret offset from edge', [[3, 3, 3, 1, 1, 1, 1, 1, 1, 3], 6], [6, True, 7]), ('regression: caret offset from edge', [[4, 1, 1, 1], 11], [0, True, 1]), ('regression: caret offset from edge', [[3, 3], -1], [1, True, 2])], [('regression: caret offset from edge', [[3, 3, 3, 3, 4, 4, 4, 1], 8], [3, True, 4]), ('regression: caret offset from edge', [[2, 2, 2, 2], -2], [0, False, 0]), ('regression: caret offset from edge', [[1, 1, 1, 4, 4, 2, 2], 17], [0, False, 0]), ('regression: caret offset from edge', [[1, 1, 1, 1, 1, 3, 3, 3, 3], -1], [8, True, 9]), ('click past end', [[0, 1, 1], 9], [1, False, 1]), ('click left of RTL line', [[1, 1, 1], -1], [2, True, 3]), ('regression: caret offset from edge', [[0, 1, 1, 1, 1, 0, 0, 0], 15], [7, True, 8]), ('regression: caret offset from edge', [[0, 0], 7], [1, True, 2])], [('regression: caret offset from edge', [[2, 2, 2, 2], 4], [2, False, 2]), ('regression: caret offset from edge', [[1, 1, 1, 1, 2, 2, 2, 2, 3, 2], 23], [0, False, 0]), ('click past end', [[0, 1, 1], 9], [1, False, 1]), ('regression: caret offset from edge', [[1, 1, 1, 2, 2, 2, 2, 1, 1, 1], -3], [9, True, 10]), ('right half of RTL char', [[1, 1], 1], [1, False, 1]), ('click left of RTL line', [[1, 1, 1], -1], [2, True, 3]), ('regression: caret offset from edge', [[1], -2], [0, True, 1]), ('regression: caret offset from edge', [[4, 4, 4, 4, 4, 4, 4, 2, 2, 2], 8], [4, False, 4])], [('regression: caret offset from edge', [[0, 0, 0, 1, 1, 1, 1, 3, 3], 3], [1, True, 2]), ('regression: caret offset from edge', [[1, 1, 1, 1, 1, 1, 0, 0, 3], 6], [2, True, 3]), ('regression: caret offset from edge', [[1, 1, 1], 8], [0, False, 0]), ('regression: caret offset from edge', [[3, 3, 3, 3, 1, 1, 1], -2], [6, True, 7]), ('right half of RTL char', [[1, 1], 1], [1, False, 1]), ('click past end', [[0, 1, 1], 9], [1, False, 1]), ('regression: caret offset from edge', [[2, 2, 3, 3, 1, 1, 1], 8], [1, False, 1]), ('regression: caret offset from edge', [[1, 1, 1, 1, 1, 3, 3, 3, 3], -1], [8, True, 9])], [('regression: caret offset from edge', [[3, 3, 3, 1, 1, 1, 1, 2], 2], [6, True, 7]), ('regression: caret offset from edge', [[2], 4], [0, True, 1]), ('regression: caret offset from edge', [[3, 3, 3, 2, 2], -2], [2, True, 3]), ('regression: caret offset from edge', [[3, 3, 3, 3, 1, 1, 1, 1], 18], [0, False, 0]), ('right half of RTL char', [[1, 1], 1], [1, False, 1]), ('click left of RTL line', [[1, 1, 1], -1], [2, True, 3]), ('regression: caret offset from edge', [[0, 0, 0, 0, 0, 0, 1, 1, 2, 0], 13], [8, True, 9]), ('regression: caret offset from edge', [[4, 4, 3], 1], [2, False, 2])]]
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
click left of RTL line[2, True, 3][2, True, 3]Passed
regression: caret offset from edge[4, True, 5][4, True, 5]Passed
regression: caret offset from edge[0, False, 0][0, False, 0]Passed
click past end[1, False, 1][1, False, 1]Passed
right half of RTL char[1, False, 1][1, False, 1]Passed
regression: caret offset from edge[6, True, 7][6, True, 7]Passed
regression: caret offset from edge[0, True, 1][0, True, 1]Passed
regression: caret offset from edge[1, True, 2][1, True, 2]Passed

SHA-256 / 861bc74eed49d3fa85763c91549c00bc628883247fbb6e93878a94fdf4761741

Verification & scope

A deterministic toy bidi model over stipulated class labels and integer levels; it is inspired by, but does not claim conformance to, any published algorithm. 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:56.161379+00:00.

Case digest / c11b7cb904727f350028f38550e35f9a024e2d61956a00b141bcf6dead96565a