{
  "candidate": "function transition(state, event, policy) {\n  const { status, attempts, nextRunAt } = state;\n  const { type, now, retryable } = event;\n\n  if (status === 'succeeded' || status === 'failed' || status === 'cancelled') {\n    return state;\n  }\n\n  if (type === 'cancel') {\n    return { ...state, status: 'cancelled', nextRunAt: null, lastError: null };\n  }\n\n  if (type === 'start' && (status === 'pending' || status === 'retry') && now >= nextRunAt) {\n    if (attempts >= policy.maxAttempts) {\n      return { ...state, status: 'failed', nextRunAt: null, lastError: 'ATTEMPTS_EXHAUSTED' };\n    }\n    return { ...state, status: 'running', attempts: attempts + 1, nextRunAt: null, lastError: null };\n  }\n\n  if (status !== 'running') {\n    return state;\n  }\n\n  if (type === 'success') {\n    return { ...state, status: 'succeeded', nextRunAt: null, lastError: null };\n  }\n\n  if (type === 'failure') {\n    const lastError = retryable === true ? 'TRANSIENT' : 'PERMANENT';\n    if (retryable === true && attempts < policy.maxAttempts) {\n      const delay = Math.min(policy.maxDelay, policy.baseDelay * 2 ** (attempts - 1));\n      return { ...state, status: 'retry', nextRunAt: now + delay, lastError };\n    }\n    return { ...state, status: 'failed', nextRunAt: null, lastError };\n  }\n\n  return state;\n}\n\nmodule.exports = { transition };\n",
  "tests": "const assert = require('node:assert/strict');\nconst { test } = require('node:test');\nconst { transition } = require('./candidate.cjs');\n\nconst policy = Object.freeze({ maxAttempts: 3, baseDelay: 100, maxDelay: 150 });\nconst pending = Object.freeze({ id: 'job-7', status: 'pending', attempts: 0, nextRunAt: 0, lastError: null });\n\n test('retry lifecycle respects due time, caps backoff, and consumes the budget on starts', () => {\n  const running = transition(pending, { type: 'start', now: 0 }, policy);\n  assert.deepEqual(running, { ...pending, status: 'running', attempts: 1, nextRunAt: null });\n  assert.deepEqual(transition(running, { type: 'start', now: 1 }, policy), running);\n\n  const retry = transition(running, { type: 'failure', now: 10, retryable: true }, policy);\n  assert.deepEqual(retry, { ...running, status: 'retry', nextRunAt: 110, lastError: 'TRANSIENT' });\n  assert.deepEqual(transition(retry, { type: 'start', now: 109 }, policy), retry);\n\n  const second = transition(retry, { type: 'start', now: 110 }, policy);\n  assert.deepEqual(second, { ...running, attempts: 2 });\n  const secondRetry = transition(second, { type: 'failure', now: 125, retryable: true }, policy);\n  assert.deepEqual(secondRetry, { ...retry, attempts: 2, nextRunAt: 275 });\n\n  const third = transition(secondRetry, { type: 'start', now: 275 }, policy);\n  assert.deepEqual(third, { ...running, attempts: 3 });\n  assert.deepEqual(transition(third, { type: 'failure', now: 280, retryable: true }, policy), {\n    ...third, status: 'failed', lastError: 'TRANSIENT'\n  });\n});\n\ntest('a due retry with an exhausted budget fails without consuming an attempt', () => {\n  const retry = { ...pending, status: 'retry', attempts: 4, nextRunAt: 100, lastError: 'TRANSIENT' };\n  assert.deepEqual(transition(retry, { type: 'start', now: 99 }, policy), retry);\n  assert.deepEqual(transition(retry, { type: 'start', now: 100 }, policy), {\n    ...retry, status: 'failed', nextRunAt: null, lastError: 'ATTEMPTS_EXHAUSTED'\n  });\n});\n\ntest('cancel clears queued metadata and terminal states ignore all events', () => {\n  const retry = { ...pending, status: 'retry', attempts: 1, nextRunAt: 100, lastError: 'TRANSIENT' };\n  const cancelled = transition(retry, { type: 'cancel', now: 50 }, policy);\n  assert.deepEqual(cancelled, { ...retry, status: 'cancelled', nextRunAt: null, lastError: null });\n\n  for (const status of ['cancelled', 'succeeded', 'failed']) {\n    const terminal = { ...cancelled, status, lastError: status === 'failed' ? 'PERMANENT' : null };\n    for (const type of ['start', 'success', 'failure', 'cancel', 'tick']) {\n      assert.deepEqual(transition(terminal, { type, now: 200, retryable: true }, policy), terminal, `${status} / ${type}`);\n    }\n  }\n});\n\ntest('running outcomes clear stale metadata and preserve frozen inputs and extra fields', () => {\n  const state = Object.freeze({\n    ...pending, status: 'running', attempts: 1, nextRunAt: 80, lastError: 'TRANSIENT',\n    metadata: Object.freeze({ batch: 4 })\n  });\n  assert.deepEqual(transition(state, { type: 'success', now: 100 }, policy), {\n    ...state, status: 'succeeded', nextRunAt: null, lastError: null\n  });\n\n  for (const retryable of [undefined, false, 'true']) {\n    const event = Object.freeze({ type: 'failure', now: 100, retryable, error: 'password=demo-only' });\n    assert.deepEqual(transition(state, event, policy), {\n      ...state, status: 'failed', nextRunAt: null, lastError: 'PERMANENT'\n    });\n  }\n\n  const event = Object.freeze({ type: 'failure', now: 100, retryable: true, error: 'password=demo-only' });\n  assert.deepEqual(transition(state, event, policy), {\n    ...state, status: 'retry', nextRunAt: 200, lastError: 'TRANSIENT'\n  });\n  assert.equal(state.status, 'running');\n  assert.equal(event.error, 'password=demo-only');\n  assert.equal(policy.maxAttempts, 3);\n});\n\ntest('pending and retry jobs ignore completion and unknown events', () => {\n  const retry = { ...pending, status: 'retry', attempts: 1, nextRunAt: 100, lastError: 'TRANSIENT' };\n  for (const state of [pending, retry]) {\n    for (const type of ['success', 'failure', 'tick']) {\n      assert.deepEqual(transition(state, { type, now: 200, retryable: true }, policy), state);\n    }\n  }\n});\n",
  "explanation": "The original runner mutated its input, accepted out-of-order events, and could spend attempts past the retry budget or restart cancelled jobs. The repair gates starts on status, due time, and remaining budget. It makes terminal states absorb every event and uses capped exponential backoff only for explicitly retryable failures with attempts remaining. State changes clear scheduling and error fields where required and record only the specified error codes.\n\nState changes use shallow copies that preserve unrelated fields. No-ops return the original state, and nested metadata retains its identity. The implementation assumes valid inputs and uses native JavaScript number arithmetic. Concurrency, persistence, clock synchronization, and command execution are outside scope.\n\nThe regression tests cover attempt accounting, time boundaries, capped backoff, exhausted budgets, terminal states, stale metadata, and raw error handling. Execution was omitted per the single-response instructions. No test or grader result is claimed."
}