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

N = 1
observations = []
def solve(s):
    return {
        'eligible': [r for r in s['history'] if r[0]>s['cursor']],
        'values': [r[1] for r in s['history'] if r[0]>s['cursor']][:s['demand']] if not s['cancel'] else [],
        'gap': bool(s['history']) and s['cursor']<s['history'][-1][0]-1,
        'terminal': s['terminal'] if s['want_terminal'] and not s['cancel'] and not s['replaying'] else None,
        'live': list(s['live']) if not s['replaying'] and not s['cancel'] else [],
        'deferred': list(s['live']) if s['replaying'] and not s['cancel'] else [],
        'cursor': max([s['cursor']]+[r[0] for r in s['history'] if r[0]>s['cursor']][:s['demand']]),
        'remaining': max(0,s['demand']-len([r for r in s['history'] if r[0]>s['cursor']])),
        'attach_live': not s['subscribed'] and s['terminal'] is None and not s['cancel'],
        'history_order': [r[0] for r in s['history']],
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': False}, {'eligible': [[1, 'a'], [3, 'b'], [4, 'c']], 'values': ['a'], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 3, 4]}), ({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 1, 'demand': 4, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': False}, {'eligible': [[3, 'b'], [4, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 4, 'remaining': 2, 'attach_live': False, 'history_order': [1, 3, 4]}), ({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 0, 'demand': 0, 'cancel': True, 'terminal': None, 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': True}, {'eligible': [[1, 'a'], [3, 'b'], [4, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 3, 4]}), ({'history': [], 'cursor': 0, 'demand': 1, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': [], 'last': 4, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 1, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 3, 'demand': 1, 'cancel': False, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 3, 'remaining': 1, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a']], 'cursor': 3, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 3, 'remaining': 0, 'attach_live': False, 'history_order': [1]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 3, 'demand': 4, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': True}, {'eligible': [[4, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 4, 'remaining': 3, 'attach_live': False, 'history_order': [1, 3, 4]})], 2: [({'history': [[1, 'a'], [6, 'b'], [8, 'c']], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 8, 'subscribed': False}, {'eligible': [[1, 'a'], [6, 'b'], [8, 'c']], 'values': ['a'], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 6, 8]}), ({'history': [[1, 'a'], [6, 'b'], [8, 'c']], 'cursor': 6, 'demand': 8, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 8, 'subscribed': True}, {'eligible': [[8, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 8, 'remaining': 7, 'attach_live': False, 'history_order': [1, 6, 8]}), ({'history': [[1, 'a']], 'cursor': 1, 'demand': 1, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': True, 'live': [], 'last': 8, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 1, 'attach_live': False, 'history_order': [1]}), ({'history': [[1, 'a'], [6, 'b'], [8, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': ['live'], 'last': 8, 'subscribed': True}, {'eligible': [[1, 'a'], [6, 'b'], [8, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': ['live'], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 6, 8]}), ({'history': [], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': [], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 1, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 6, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': [], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 6, 'remaining': 0, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []})], 3: [({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 9, 'demand': 12, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 12, 'subscribed': False}, {'eligible': [[12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 12, 'remaining': 11, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 0, 'demand': 1, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[1, 'a'], [9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a']], 'cursor': 0, 'demand': 1, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[1, 'a']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[1, 'a'], [9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': 'complete', 'live': ['live'], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 1, 'demand': 1, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [[9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 9, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 9, 'demand': 12, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 12, 'subscribed': True}, {'eligible': [[12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 12, 'remaining': 11, 'attach_live': False, 'history_order': [1, 9, 12]})], 4: [({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [12, 'b'], [16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 12, 'demand': 16, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 16, 'remaining': 15, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': [], 'last': 16, 'subscribed': True}, {'eligible': [[1, 'a'], [12, 'b'], [16, 'c']], 'values': ['a'], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 1, 'demand': 16, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[12, 'b'], [16, 'c']], 'values': ['b', 'c'], 'gap': False, 'terminal': 'complete', 'live': ['live'], 'deferred': [], 'cursor': 16, 'remaining': 14, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': ['live'], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': []}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 0, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [12, 'b'], [16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 12, 'demand': 16, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 16, 'subscribed': True}, {'eligible': [[16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 16, 'remaining': 15, 'attach_live': False, 'history_order': [1, 12, 16]})], 5: [({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 1, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 20, 'subscribed': False}, {'eligible': [[15, 'b'], [20, 'c']], 'values': ['b'], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 15, 'remaining': 0, 'attach_live': False, 'history_order': [1, 15, 20]}), ({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 0, 'demand': 20, 'cancel': True, 'terminal': None, 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [15, 'b'], [20, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 20, 'remaining': 17, 'attach_live': False, 'history_order': [1, 15, 20]}), ({'history': [[1, 'a']], 'cursor': 1, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': [], 'last': 20, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': 'complete', 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 1, 'attach_live': False, 'history_order': [1]}), ({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 0, 'demand': 20, 'cancel': False, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': [], 'last': 20, 'subscribed': True}, {'eligible': [[1, 'a'], [15, 'b'], [20, 'c']], 'values': ['a', 'b', 'c'], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 20, 'remaining': 17, 'attach_live': False, 'history_order': [1, 15, 20]}), ({'history': [[1, 'a']], 'cursor': 15, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 15, 'remaining': 0, 'attach_live': False, 'history_order': [1]}), ({'history': [], 'cursor': 15, 'demand': 0, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 20, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 15, 'remaining': 0, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 15, 'demand': 20, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 20, 'subscribed': True}, {'eligible': [[20, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 20, 'remaining': 19, 'attach_live': False, 'history_order': [1, 15, 20]})]}
for i,(state,expected) in enumerate(fixtures[N]):
    check("transition "+str(i), solve(state), 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(s):
    return {
        'eligible': [r for r in s['history'] if r[0]>s['cursor']],
        'values': [r[1] for r in s['history'] if r[0]>s['cursor']][:s['demand']] if not s['cancel'] else [],
        'gap': bool(s['history']) and s['cursor']<s['history'][0][0],
        'terminal': s['terminal'] if s['want_terminal'] and not s['cancel'] and not s['replaying'] else None,
        'live': list(s['live']) if not s['replaying'] and not s['cancel'] else [],
        'deferred': list(s['live']) if s['replaying'] and not s['cancel'] else [],
        'cursor': max([s['cursor']]+[r[0] for r in s['history'] if r[0]>s['cursor']][:s['demand']]),
        'remaining': max(0,s['demand']-len([r for r in s['history'] if r[0]>s['cursor']])),
        'attach_live': not s['subscribed'] and s['terminal'] is None and not s['cancel'],
        'history_order': [r[0] for r in s['history']],
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': False}, {'eligible': [[1, 'a'], [3, 'b'], [4, 'c']], 'values': ['a'], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 3, 4]}), ({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 1, 'demand': 4, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': False}, {'eligible': [[3, 'b'], [4, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 4, 'remaining': 2, 'attach_live': False, 'history_order': [1, 3, 4]}), ({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 0, 'demand': 0, 'cancel': True, 'terminal': None, 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': True}, {'eligible': [[1, 'a'], [3, 'b'], [4, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 3, 4]}), ({'history': [], 'cursor': 0, 'demand': 1, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': [], 'last': 4, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 1, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 3, 'demand': 1, 'cancel': False, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 3, 'remaining': 1, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a']], 'cursor': 3, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 3, 'remaining': 0, 'attach_live': False, 'history_order': [1]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 3, 'demand': 4, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': True}, {'eligible': [[4, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 4, 'remaining': 3, 'attach_live': False, 'history_order': [1, 3, 4]})], 2: [({'history': [[1, 'a'], [6, 'b'], [8, 'c']], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 8, 'subscribed': False}, {'eligible': [[1, 'a'], [6, 'b'], [8, 'c']], 'values': ['a'], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 6, 8]}), ({'history': [[1, 'a'], [6, 'b'], [8, 'c']], 'cursor': 6, 'demand': 8, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 8, 'subscribed': True}, {'eligible': [[8, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 8, 'remaining': 7, 'attach_live': False, 'history_order': [1, 6, 8]}), ({'history': [[1, 'a']], 'cursor': 1, 'demand': 1, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': True, 'live': [], 'last': 8, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 1, 'attach_live': False, 'history_order': [1]}), ({'history': [[1, 'a'], [6, 'b'], [8, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': ['live'], 'last': 8, 'subscribed': True}, {'eligible': [[1, 'a'], [6, 'b'], [8, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': ['live'], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 6, 8]}), ({'history': [], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': [], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 1, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 6, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': [], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 6, 'remaining': 0, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []})], 3: [({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 9, 'demand': 12, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 12, 'subscribed': False}, {'eligible': [[12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 12, 'remaining': 11, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 0, 'demand': 1, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[1, 'a'], [9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a']], 'cursor': 0, 'demand': 1, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[1, 'a']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[1, 'a'], [9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': 'complete', 'live': ['live'], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 1, 'demand': 1, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [[9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 9, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 9, 'demand': 12, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 12, 'subscribed': True}, {'eligible': [[12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 12, 'remaining': 11, 'attach_live': False, 'history_order': [1, 9, 12]})], 4: [({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [12, 'b'], [16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 12, 'demand': 16, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 16, 'remaining': 15, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': [], 'last': 16, 'subscribed': True}, {'eligible': [[1, 'a'], [12, 'b'], [16, 'c']], 'values': ['a'], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 1, 'demand': 16, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[12, 'b'], [16, 'c']], 'values': ['b', 'c'], 'gap': False, 'terminal': 'complete', 'live': ['live'], 'deferred': [], 'cursor': 16, 'remaining': 14, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': ['live'], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': []}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 0, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [12, 'b'], [16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 12, 'demand': 16, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 16, 'subscribed': True}, {'eligible': [[16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 16, 'remaining': 15, 'attach_live': False, 'history_order': [1, 12, 16]})], 5: [({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 1, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 20, 'subscribed': False}, {'eligible': [[15, 'b'], [20, 'c']], 'values': ['b'], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 15, 'remaining': 0, 'attach_live': False, 'history_order': [1, 15, 20]}), ({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 0, 'demand': 20, 'cancel': True, 'terminal': None, 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [15, 'b'], [20, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 20, 'remaining': 17, 'attach_live': False, 'history_order': [1, 15, 20]}), ({'history': [[1, 'a']], 'cursor': 1, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': [], 'last': 20, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': 'complete', 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 1, 'attach_live': False, 'history_order': [1]}), ({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 0, 'demand': 20, 'cancel': False, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': [], 'last': 20, 'subscribed': True}, {'eligible': [[1, 'a'], [15, 'b'], [20, 'c']], 'values': ['a', 'b', 'c'], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 20, 'remaining': 17, 'attach_live': False, 'history_order': [1, 15, 20]}), ({'history': [[1, 'a']], 'cursor': 15, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 15, 'remaining': 0, 'attach_live': False, 'history_order': [1]}), ({'history': [], 'cursor': 15, 'demand': 0, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 20, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 15, 'remaining': 0, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 15, 'demand': 20, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 20, 'subscribed': True}, {'eligible': [[20, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 20, 'remaining': 19, 'attach_live': False, 'history_order': [1, 15, 20]})]}
for i,(state,expected) in enumerate(fixtures[N]):
    check("transition "+str(i), solve(state), 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:
Replay cursor gap detection compares against the newest record; the returned lifecycle plan violates the stated transition contract.

Plan a subscription to a bounded replay observer. History entries are [sequence,value]; deliver entries strictly after cursor, in stored order, capped by demand. Subscriber replay is suppressed after cancellation. A terminal marker follows replay only when requested. A gap exists if the cursor precedes the oldest retained predecessor. Live events stay deferred until replay completes. Inputs are finite state snapshots, including transitional and stale states; output fields are the exact independent actions/metadata defined by the corrected expression table. Numeric fixture capacities and counts vary with N.
Field definitions, each independently computed from the input snapshot: eligible = [r for r in s['history'] if r[0]>s['cursor']]; values = [r[1] for r in s['history'] if r[0]>s['cursor']][:s['demand']] if not s['cancel'] else []; gap = bool(s['history']) and s['cursor']<s['history'][0][0]-1; terminal = s['terminal'] if s['want_terminal'] and not s['cancel'] and not s['replaying'] else None; live = list(s['live']) if not s['replaying'] and not s['cancel'] else []; deferred = list(s['live']) if s['replaying'] and not s['cancel'] else []; cursor = max([s['cursor']]+[r[0] for r in s['history'] if r[0]>s['cursor']][:s['demand']]); remaining = max(0,s['demand']-len([r for r in s['history'] if r[0]>s['cursor']])); attach_live = not s['subscribed'] and s['terminal'] is None and not s['cancel']; history_order = [r[0] for r in s['history']]

split: open-access


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

N = 1
observations = []
def solve(s):
    return {
        'eligible': [r for r in s['history'] if r[0]>s['cursor']],
        'values': [r[1] for r in s['history'] if r[0]>s['cursor']][:s['demand']] if not s['cancel'] else [],
        'gap': bool(s['history']) and s['cursor']<s['history'][0][0]-1,
        'terminal': s['terminal'],
        'live': list(s['live']) if not s['replaying'] and not s['cancel'] else [],
        'deferred': list(s['live']) if s['replaying'] and not s['cancel'] else [],
        'cursor': max([s['cursor']]+[r[0] for r in s['history'] if r[0]>s['cursor']][:s['demand']]),
        'remaining': max(0,s['demand']-len([r for r in s['history'] if r[0]>s['cursor']])),
        'attach_live': not s['subscribed'] and s['terminal'] is None and not s['cancel'],
        'history_order': [r[0] for r in s['history']],
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': False}, {'eligible': [[1, 'a'], [3, 'b'], [4, 'c']], 'values': ['a'], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 3, 4]}), ({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 1, 'demand': 4, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': False}, {'eligible': [[3, 'b'], [4, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 4, 'remaining': 2, 'attach_live': False, 'history_order': [1, 3, 4]}), ({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 0, 'demand': 0, 'cancel': True, 'terminal': None, 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': True}, {'eligible': [[1, 'a'], [3, 'b'], [4, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 3, 4]}), ({'history': [], 'cursor': 0, 'demand': 1, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': [], 'last': 4, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 1, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 3, 'demand': 1, 'cancel': False, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 3, 'remaining': 1, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a']], 'cursor': 3, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 3, 'remaining': 0, 'attach_live': False, 'history_order': [1]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 3, 'demand': 4, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': True}, {'eligible': [[4, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 4, 'remaining': 3, 'attach_live': False, 'history_order': [1, 3, 4]})], 2: [({'history': [[1, 'a'], [6, 'b'], [8, 'c']], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 8, 'subscribed': False}, {'eligible': [[1, 'a'], [6, 'b'], [8, 'c']], 'values': ['a'], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 6, 8]}), ({'history': [[1, 'a'], [6, 'b'], [8, 'c']], 'cursor': 6, 'demand': 8, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 8, 'subscribed': True}, {'eligible': [[8, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 8, 'remaining': 7, 'attach_live': False, 'history_order': [1, 6, 8]}), ({'history': [[1, 'a']], 'cursor': 1, 'demand': 1, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': True, 'live': [], 'last': 8, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 1, 'attach_live': False, 'history_order': [1]}), ({'history': [[1, 'a'], [6, 'b'], [8, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': ['live'], 'last': 8, 'subscribed': True}, {'eligible': [[1, 'a'], [6, 'b'], [8, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': ['live'], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 6, 8]}), ({'history': [], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': [], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 1, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 6, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': [], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 6, 'remaining': 0, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []})], 3: [({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 9, 'demand': 12, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 12, 'subscribed': False}, {'eligible': [[12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 12, 'remaining': 11, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 0, 'demand': 1, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[1, 'a'], [9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a']], 'cursor': 0, 'demand': 1, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[1, 'a']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[1, 'a'], [9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': 'complete', 'live': ['live'], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 1, 'demand': 1, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [[9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 9, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 9, 'demand': 12, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 12, 'subscribed': True}, {'eligible': [[12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 12, 'remaining': 11, 'attach_live': False, 'history_order': [1, 9, 12]})], 4: [({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [12, 'b'], [16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 12, 'demand': 16, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 16, 'remaining': 15, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': [], 'last': 16, 'subscribed': True}, {'eligible': [[1, 'a'], [12, 'b'], [16, 'c']], 'values': ['a'], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 1, 'demand': 16, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[12, 'b'], [16, 'c']], 'values': ['b', 'c'], 'gap': False, 'terminal': 'complete', 'live': ['live'], 'deferred': [], 'cursor': 16, 'remaining': 14, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': ['live'], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': []}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 0, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [12, 'b'], [16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 12, 'demand': 16, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 16, 'subscribed': True}, {'eligible': [[16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 16, 'remaining': 15, 'attach_live': False, 'history_order': [1, 12, 16]})], 5: [({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 1, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 20, 'subscribed': False}, {'eligible': [[15, 'b'], [20, 'c']], 'values': ['b'], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 15, 'remaining': 0, 'attach_live': False, 'history_order': [1, 15, 20]}), ({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 0, 'demand': 20, 'cancel': True, 'terminal': None, 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [15, 'b'], [20, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 20, 'remaining': 17, 'attach_live': False, 'history_order': [1, 15, 20]}), ({'history': [[1, 'a']], 'cursor': 1, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': [], 'last': 20, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': 'complete', 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 1, 'attach_live': False, 'history_order': [1]}), ({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 0, 'demand': 20, 'cancel': False, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': [], 'last': 20, 'subscribed': True}, {'eligible': [[1, 'a'], [15, 'b'], [20, 'c']], 'values': ['a', 'b', 'c'], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 20, 'remaining': 17, 'attach_live': False, 'history_order': [1, 15, 20]}), ({'history': [[1, 'a']], 'cursor': 15, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 15, 'remaining': 0, 'attach_live': False, 'history_order': [1]}), ({'history': [], 'cursor': 15, 'demand': 0, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 20, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 15, 'remaining': 0, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 15, 'demand': 20, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 20, 'subscribed': True}, {'eligible': [[20, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 20, 'remaining': 19, 'attach_live': False, 'history_order': [1, 15, 20]})]}
for i,(state,expected) in enumerate(fixtures[N]):
    check("transition "+str(i), solve(state), 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(s):
    return {
        'eligible': [r for r in s['history'] if r[0]>s['cursor']],
        'values': [r[1] for r in s['history'] if r[0]>s['cursor']][:s['demand']] if not s['cancel'] else [],
        'gap': bool(s['history']) and s['cursor']<s['history'][0][0]-1,
        'terminal': s['terminal'] if s['want_terminal'] and not s['cancel'] else None,
        'live': list(s['live']) if not s['replaying'] and not s['cancel'] else [],
        'deferred': list(s['live']) if s['replaying'] and not s['cancel'] else [],
        'cursor': max([s['cursor']]+[r[0] for r in s['history'] if r[0]>s['cursor']][:s['demand']]),
        'remaining': max(0,s['demand']-len([r for r in s['history'] if r[0]>s['cursor']])),
        'attach_live': not s['subscribed'] and s['terminal'] is None and not s['cancel'],
        'history_order': [r[0] for r in s['history']],
    }
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = {1: [({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': False}, {'eligible': [[1, 'a'], [3, 'b'], [4, 'c']], 'values': ['a'], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 3, 4]}), ({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 1, 'demand': 4, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': False}, {'eligible': [[3, 'b'], [4, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 4, 'remaining': 2, 'attach_live': False, 'history_order': [1, 3, 4]}), ({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 0, 'demand': 0, 'cancel': True, 'terminal': None, 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': True}, {'eligible': [[1, 'a'], [3, 'b'], [4, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 3, 4]}), ({'history': [], 'cursor': 0, 'demand': 1, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': [], 'last': 4, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 1, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 3, 'demand': 1, 'cancel': False, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 3, 'remaining': 1, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a']], 'cursor': 3, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 3, 'remaining': 0, 'attach_live': False, 'history_order': [1]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [3, 'b'], [4, 'c']], 'cursor': 3, 'demand': 4, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 4, 'subscribed': True}, {'eligible': [[4, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 4, 'remaining': 3, 'attach_live': False, 'history_order': [1, 3, 4]})], 2: [({'history': [[1, 'a'], [6, 'b'], [8, 'c']], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 8, 'subscribed': False}, {'eligible': [[1, 'a'], [6, 'b'], [8, 'c']], 'values': ['a'], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 6, 8]}), ({'history': [[1, 'a'], [6, 'b'], [8, 'c']], 'cursor': 6, 'demand': 8, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 8, 'subscribed': True}, {'eligible': [[8, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 8, 'remaining': 7, 'attach_live': False, 'history_order': [1, 6, 8]}), ({'history': [[1, 'a']], 'cursor': 1, 'demand': 1, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': True, 'live': [], 'last': 8, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 1, 'attach_live': False, 'history_order': [1]}), ({'history': [[1, 'a'], [6, 'b'], [8, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': ['live'], 'last': 8, 'subscribed': True}, {'eligible': [[1, 'a'], [6, 'b'], [8, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': ['live'], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 6, 8]}), ({'history': [], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': [], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 1, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 6, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': [], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 6, 'remaining': 0, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []})], 3: [({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 9, 'demand': 12, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 12, 'subscribed': False}, {'eligible': [[12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 12, 'remaining': 11, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 0, 'demand': 1, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[1, 'a'], [9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a']], 'cursor': 0, 'demand': 1, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[1, 'a']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[1, 'a'], [9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': 'complete', 'live': ['live'], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 1, 'demand': 1, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [[9, 'b'], [12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 9, 'remaining': 0, 'attach_live': False, 'history_order': [1, 9, 12]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [9, 'b'], [12, 'c']], 'cursor': 9, 'demand': 12, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 12, 'subscribed': True}, {'eligible': [[12, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 12, 'remaining': 11, 'attach_live': False, 'history_order': [1, 9, 12]})], 4: [({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [12, 'b'], [16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 12, 'demand': 16, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 16, 'remaining': 15, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 0, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': [], 'last': 16, 'subscribed': True}, {'eligible': [[1, 'a'], [12, 'b'], [16, 'c']], 'values': ['a'], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 0, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 1, 'demand': 16, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [[12, 'b'], [16, 'c']], 'values': ['b', 'c'], 'gap': False, 'terminal': 'complete', 'live': ['live'], 'deferred': [], 'cursor': 16, 'remaining': 14, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': ['live'], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': []}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 0, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': False, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [12, 'b'], [16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': False, 'history_order': [1, 12, 16]}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [12, 'b'], [16, 'c']], 'cursor': 12, 'demand': 16, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 16, 'subscribed': True}, {'eligible': [[16, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 16, 'remaining': 15, 'attach_live': False, 'history_order': [1, 12, 16]})], 5: [({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 1, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 20, 'subscribed': False}, {'eligible': [[15, 'b'], [20, 'c']], 'values': ['b'], 'gap': False, 'terminal': None, 'live': [], 'deferred': ['live'], 'cursor': 15, 'remaining': 0, 'attach_live': False, 'history_order': [1, 15, 20]}), ({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 0, 'demand': 20, 'cancel': True, 'terminal': None, 'want_terminal': False, 'replaying': True, 'live': ['live'], 'last': 0, 'subscribed': False}, {'eligible': [[1, 'a'], [15, 'b'], [20, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 20, 'remaining': 17, 'attach_live': False, 'history_order': [1, 15, 20]}), ({'history': [[1, 'a']], 'cursor': 1, 'demand': 1, 'cancel': False, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': [], 'last': 20, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': 'complete', 'live': [], 'deferred': [], 'cursor': 1, 'remaining': 1, 'attach_live': False, 'history_order': [1]}), ({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 0, 'demand': 20, 'cancel': False, 'terminal': None, 'want_terminal': True, 'replaying': True, 'live': [], 'last': 20, 'subscribed': True}, {'eligible': [[1, 'a'], [15, 'b'], [20, 'c']], 'values': ['a', 'b', 'c'], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 20, 'remaining': 17, 'attach_live': False, 'history_order': [1, 15, 20]}), ({'history': [[1, 'a']], 'cursor': 15, 'demand': 0, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 0, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 15, 'remaining': 0, 'attach_live': False, 'history_order': [1]}), ({'history': [], 'cursor': 15, 'demand': 0, 'cancel': True, 'terminal': None, 'want_terminal': True, 'replaying': False, 'live': ['live'], 'last': 20, 'subscribed': True}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 15, 'remaining': 0, 'attach_live': False, 'history_order': []}), ({'history': [], 'cursor': 0, 'demand': 0, 'cancel': False, 'terminal': None, 'want_terminal': False, 'replaying': False, 'live': [], 'last': 0, 'subscribed': False}, {'eligible': [], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 0, 'remaining': 0, 'attach_live': True, 'history_order': []}), ({'history': [[1, 'a'], [15, 'b'], [20, 'c']], 'cursor': 15, 'demand': 20, 'cancel': True, 'terminal': 'complete', 'want_terminal': True, 'replaying': True, 'live': ['live'], 'last': 20, 'subscribed': True}, {'eligible': [[20, 'c']], 'values': [], 'gap': False, 'terminal': None, 'live': [], 'deferred': [], 'cursor': 20, 'remaining': 19, 'attach_live': False, 'history_order': [1, 15, 20]})]}
for i,(state,expected) in enumerate(fixtures[N]):
    check("transition "+str(i), solve(state), 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:
Replay sends terminal notification to a cancelled subscriber; the returned lifecycle plan violates the stated transition contract.

Plan a subscription to a bounded replay observer. History entries are [sequence,value]; deliver entries strictly after cursor, in stored order, capped by demand. Subscriber replay is suppressed after cancellation. A terminal marker follows replay only when requested. A gap exists if the cursor precedes the oldest retained predecessor. Live events stay deferred until replay completes. Inputs are finite state snapshots, including transitional and stale states; output fields are the exact independent actions/metadata defined by the corrected expression table. Numeric fixture capacities and counts vary with N.
Field definitions, each independently computed from the input snapshot: eligible = [r for r in s['history'] if r[0]>s['cursor']]; values = [r[1] for r in s['history'] if r[0]>s['cursor']][:s['demand']] if not s['cancel'] else []; gap = bool(s['history']) and s['cursor']<s['history'][0][0]-1; terminal = s['terminal'] if s['want_terminal'] and not s['cancel'] and not s['replaying'] else None; live = list(s['live']) if not s['replaying'] and not s['cancel'] else []; deferred = list(s['live']) if s['replaying'] and not s['cancel'] else []; cursor = max([s['cursor']]+[r[0] for r in s['history'] if r[0]>s['cursor']][:s['demand']]); remaining = max(0,s['demand']-len([r for r in s['history'] if r[0]>s['cursor']])); attach_live = not s['subscribed'] and s['terminal'] is None and not s['cancel']; history_order = [r[0] for r in s['history']]

split: open-access