broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(old, starts, limit):
    res = []
    for s in starts:
        found = ''
        for k in range(s - 2, -1, -1):
            ln = old[k]
            if ln[:1].isalpha() or ln[:1] in ('_', '$'):
                found = ln.rstrip()
                break
        res.append(found)
    return res
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 11], ['def xxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    2: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 12], ['def xxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    3: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 13], ['def xxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    4: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 14], ['def xxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    5: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 15], ['def xxxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
}[N]
for label, args, expected in cases:
    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)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(old, starts, limit):
    res = []
    for s in starts:
        found = ''
        for k in range(s - 2, -1, -1):
            ln = old[k]
            if ln[:1].isalpha() or ln[:1] in ('_', '$'):
                found = ln.rstrip()[:limit - 1]
                break
        res.append(found)
    return res
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 11], ['def xxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    2: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 12], ['def xxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    3: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 13], ['def xxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    4: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 14], ['def xxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    5: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 15], ['def xxxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
}[N]
for label, args, expected in cases:
    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)

prompt:
Very long signatures overflow the hunk header.

For each hunk whose old range starts at 1-based line s, search the old lines strictly before s, nearest first, for a line whose first character is a letter, "_" or "$". The heading is that line with trailing whitespace removed, truncated to limit characters; "" if none.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(old, starts, limit):
    res = []
    for s in starts:
        found = ''
        for k in range(s - 2, -1, -1):
            ln = old[k]
            if ln[:1].isalpha():
                found = ln.rstrip()[:limit]
                break
        res.append(found)
    return res
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 11], ['def xxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    2: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 12], ['def xxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    3: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 13], ['def xxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    4: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 14], ['def xxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    5: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 15], ['def xxxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
}[N]
for label, args, expected in cases:
    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)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(old, starts, limit):
    res = []
    for s in starts:
        found = ''
        for k in range(s - 2, -1, -1):
            ln = old[k]
            if ln[:1].isalpha() or ln[:1] == '_':
                found = ln.rstrip()[:limit]
                break
        res.append(found)
    return res
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 11], ['def xxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    2: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 12], ['def xxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    3: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 13], ['def xxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    4: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 14], ['def xxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    5: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 15], ['def xxxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
}[N]
for label, args, expected in cases:
    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)

prompt:
Hunks inside labels or macros that start with "_" or "$" show an older heading.

For each hunk whose old range starts at 1-based line s, search the old lines strictly before s, nearest first, for a line whose first character is a letter, "_" or "$". The heading is that line with trailing whitespace removed, truncated to limit characters; "" if none.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(lines, recorded):
    out = []
    state = None
    o, t = [], []
    unresolved = 0
    for ln in lines:
        if state is None:
            if ln.startswith('<<<<<<<'):
                state = 'o'
                o, t = [], []
            else:
                out.append(ln)
        elif state == 'o' and ln.startswith('|||||||'):
            state = 'b'
        elif state in ('o', 'b') and ln == '=======':
            state = 't'
        elif state == 't' and ln.startswith('>>>>>>>'):
            key = [o, t]
            match = next((r[2] for r in recorded if [r[0], r[1]] == key), None)
            if match is not None:
                out.extend(match)
            else:
                unresolved += 1
                out.extend(['<<<<<<<'] + o + ['======='] + t + ['>>>>>>>'])
            state = None
        elif state == 'o':
            o.append(ln)
        elif state == 't':
            t.append(ln)
    return {'lines': out, 'unresolved': unresolved}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic1'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p1', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p1', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    2: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic2'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p2', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p2', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    3: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic3'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p3', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p3', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    4: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic4'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p4', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p4', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    5: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic5'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p5', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p5', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
}[N]
for label, args, expected in cases:
    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)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(lines, recorded):
    out = []
    state = None
    o, t = [], []
    unresolved = 0
    for ln in lines:
        if state is None:
            if ln.startswith('<<<<<<<'):
                state = 'o'
                o, t = [], []
            else:
                out.append(ln)
        elif state == 'o' and ln.startswith('|||||||'):
            state = 'b'
        elif state in ('o', 'b') and ln == '=======':
            state = 't'
        elif state == 't' and ln.startswith('>>>>>>>'):
            key = [o, t]
            match = next((r[2] for r in recorded if sorted([r[0], r[1]]) == key), None)
            if match is not None:
                out.extend(match)
            else:
                unresolved += 1
                out.extend(['<<<<<<<'] + o + ['======='] + t + ['>>>>>>>'])
            state = None
        elif state == 'o':
            o.append(ln)
        elif state == 't':
            t.append(ln)
    return {'lines': out, 'unresolved': unresolved}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic1'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p1', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p1', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    2: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic2'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p2', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p2', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    3: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic3'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p3', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p3', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    4: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic4'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p4', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p4', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    5: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic5'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p5', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p5', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
}[N]
for label, args, expected in cases:
    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)

prompt:
The same conflict seen from the other branch must be resolved by hand again.

Scan conflict blocks ("<<<<<<<" optionally followed by a label, optional "|||||||" base section, "=======", ">>>>>>>" optionally labelled). A block matches a recorded [side1, side2, resolution] when its two sides equal the recorded sides in either order; base lines are ignored. Matched blocks are replaced by the resolution; unmatched blocks are re-emitted as bare "<<<<<<<", ours, "=======", theirs, ">>>>>>>" and counted. Return lines and the unresolved count.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(lines, recorded):
    out = []
    state = None
    o, t = [], []
    unresolved = 0
    for ln in lines:
        if state is None:
            if ln.startswith('<<<<<<<'):
                state = 'o'
                o, t = [], []
            else:
                out.append(ln)
        elif state in ('o', 'b') and ln == '=======':
            state = 't'
        elif state == 't' and ln.startswith('>>>>>>>'):
            key = sorted([o, t])
            match = next((r[2] for r in recorded if sorted([r[0], r[1]]) == key), None)
            if match is not None:
                out.extend(match)
            else:
                unresolved += 1
                out.extend(['<<<<<<<'] + o + ['======='] + t + ['>>>>>>>'])
            state = None
        elif state == 'o':
            o.append(ln)
        elif state == 't':
            t.append(ln)
    return {'lines': out, 'unresolved': unresolved}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic1'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p1', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p1', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    2: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic2'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p2', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p2', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    3: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic3'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p3', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p3', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    4: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic4'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p4', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p4', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    5: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic5'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p5', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p5', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
}[N]
for label, args, expected in cases:
    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)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(lines, recorded):
    out = []
    state = None
    o, t = [], []
    unresolved = 0
    for ln in lines:
        if state is None:
            if ln.startswith('<<<<<<<'):
                state = 'o'
                o, t = [], []
            else:
                out.append(ln)
        elif state == 'o' and ln == '|||||||':
            state = 'b'
        elif state in ('o', 'b') and ln == '=======':
            state = 't'
        elif state == 't' and ln.startswith('>>>>>>>'):
            key = sorted([o, t])
            match = next((r[2] for r in recorded if sorted([r[0], r[1]]) == key), None)
            if match is not None:
                out.extend(match)
            else:
                unresolved += 1
                out.extend(['<<<<<<<'] + o + ['======='] + t + ['>>>>>>>'])
            state = None
        elif state == 'o':
            o.append(ln)
        elif state == 't':
            t.append(ln)
    return {'lines': out, 'unresolved': unresolved}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic1'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p1', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p1', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    2: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic2'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p2', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p2', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    3: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic3'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p3', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p3', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    4: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic4'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p4', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p4', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    5: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic5'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p5', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p5', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
}[N]
for label, args, expected in cases:
    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)

prompt:
diff3-style conflicts never match recorded resolutions.

Scan conflict blocks ("<<<<<<<" optionally followed by a label, optional "|||||||" base section, "=======", ">>>>>>>" optionally labelled). A block matches a recorded [side1, side2, resolution] when its two sides equal the recorded sides in either order; base lines are ignored. Matched blocks are replaced by the resolution; unmatched blocks are re-emitted as bare "<<<<<<<", ours, "=======", theirs, ">>>>>>>" and counted. Return lines and the unresolved count.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(lines, recorded):
    out = []
    state = None
    o, t = [], []
    unresolved = 0
    for ln in lines:
        if state is None:
            if ln == '<<<<<<<':
                state = 'o'
                o, t = [], []
            else:
                out.append(ln)
        elif state == 'o' and ln.startswith('|||||||'):
            state = 'b'
        elif state in ('o', 'b') and ln == '=======':
            state = 't'
        elif state == 't' and ln.startswith('>>>>>>>'):
            key = sorted([o, t])
            match = next((r[2] for r in recorded if sorted([r[0], r[1]]) == key), None)
            if match is not None:
                out.extend(match)
            else:
                unresolved += 1
                out.extend(['<<<<<<<'] + o + ['======='] + t + ['>>>>>>>'])
            state = None
        elif state == 'o':
            o.append(ln)
        elif state == 't':
            t.append(ln)
    return {'lines': out, 'unresolved': unresolved}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic1'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p1', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p1', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    2: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic2'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p2', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p2', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    3: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic3'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p3', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p3', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    4: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic4'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p4', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p4', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    5: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic5'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p5', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p5', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
}[N]
for label, args, expected in cases:
    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)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(lines, recorded):
    out = []
    state = None
    o, t = [], []
    unresolved = 0
    for ln in lines:
        if state is None:
            if ln.startswith('<<<<<<< '):
                state = 'o'
                o, t = [], []
            else:
                out.append(ln)
        elif state == 'o' and ln.startswith('|||||||'):
            state = 'b'
        elif state in ('o', 'b') and ln == '=======':
            state = 't'
        elif state == 't' and ln.startswith('>>>>>>>'):
            key = sorted([o, t])
            match = next((r[2] for r in recorded if sorted([r[0], r[1]]) == key), None)
            if match is not None:
                out.extend(match)
            else:
                unresolved += 1
                out.extend(['<<<<<<<'] + o + ['======='] + t + ['>>>>>>>'])
            state = None
        elif state == 'o':
            o.append(ln)
        elif state == 't':
            t.append(ln)
    return {'lines': out, 'unresolved': unresolved}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic1'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p1', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p1', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    2: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic2'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p2', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p2', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    3: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic3'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p3', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p3', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    4: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic4'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p4', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p4', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
    5: [('recorded resolution reused', [['pre', '<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'post'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['pre', 'x = 3', 'post'], 'unresolved': 0}), ('sides swapped still match', [['<<<<<<< HEAD', 'x = 2', '=======', 'x = 1', '>>>>>>> topic5'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0}), ('base section does not affect the fingerprint', [['<<<<<<< ours', 'c', '||||||| base', 'orig', '=======', 'a', 'b', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('unknown conflict stays unresolved', [['<<<<<<< ours', 'p5', '=======', 'q', '>>>>>>> theirs', 'tail'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['<<<<<<<', 'p5', '=======', 'q', '>>>>>>>', 'tail'], 'unresolved': 1}), ('unlabelled markers', [['<<<<<<<', 'a', 'b', '=======', 'c', '>>>>>>>'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['abc'], 'unresolved': 0}), ('two conflicts, one known', [['<<<<<<< ours', 'x = 1', '=======', 'x = 2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', 'm', '=======', 'n', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3', 'mid', '<<<<<<<', 'm', '=======', 'n', '>>>>>>>'], 'unresolved': 1}), ('labelled base marker', [['<<<<<<< ours', 'x = 2', '||||||| base', 'x = 0', '=======', 'x = 1', '>>>>>>> theirs'], [[['x = 1'], ['x = 2'], ['x = 3']], [['a', 'b'], ['c'], ['abc']]]], {'lines': ['x = 3'], 'unresolved': 0})],
}[N]
for label, args, expected in cases:
    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)

prompt:
Conflicts produced with branch labels pass through untouched.

Scan conflict blocks ("<<<<<<<" optionally followed by a label, optional "|||||||" base section, "=======", ">>>>>>>" optionally labelled). A block matches a recorded [side1, side2, resolution] when its two sides equal the recorded sides in either order; base lines are ignored. Matched blocks are replaced by the resolution; unmatched blocks are re-emitted as bare "<<<<<<<", ours, "=======", theirs, ">>>>>>>" and counted. Return lines and the unresolved count.

split: open-access