{
  "candidate": "function transition(state, event, policy) {\n  const { status, attempts } = state;\n  if (status === 'succeeded' || status === 'failed' || status === 'cancelled') return state;\n\n  switch (event.type) {\n    case 'cancel':\n      return { ...state, status: 'cancelled', nextRunAt: null, lastError: null };\n\n    case 'start':\n      if (status !== 'pending' && status !== 'retry') return state;\n      if (event.now < state.nextRunAt) return state;\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    case 'success':\n      if (status !== 'running') return state;\n      return { ...state, status: 'succeeded', nextRunAt: null, lastError: null };\n\n    case 'failure': {\n      if (status !== 'running') return state;\n      const transient = event.retryable === true;\n      if (transient && attempts < policy.maxAttempts) {\n        const delay = Math.min(policy.maxDelay, policy.baseDelay * 2 ** (attempts - 1));\n        return { ...state, status: 'retry', nextRunAt: event.now + delay, lastError: 'TRANSIENT' };\n      }\n      return { ...state, status: 'failed', nextRunAt: null, lastError: transient ? 'TRANSIENT' : 'PERMANENT' };\n    }\n\n    default:\n      return state;\n  }\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 initial = { id: 'a', status: 'pending', attempts: 0, nextRunAt: 0, lastError: null, metadata: { owner: 'ops' } };\nconst policy = { maxAttempts: 4, baseDelay: 10, maxDelay: 25 };\nconst failure = { type: 'failure', retryable: true, error: 'private diagnostic' };\n\n// Assert the full result and check all three inputs after every step.\nfunction step(state, event, changes = {}) {\n  const before = JSON.parse(JSON.stringify([state, event, policy]));\n  const next = transition(state, event, policy);\n  assert.deepStrictEqual([state, event, policy], before);\n  assert.deepStrictEqual(next, { ...before[0], ...changes });\n  return next;\n}\n\ntest('retry lifecycle consumes each attempt once and caps the delay', () => {\n  let job = initial;\n  let now = 0;\n  for (const [attempts, delay] of [[1, 10], [2, 20], [3, 25], [4, null]]) {\n    job = step(job, { type: 'start', now }, { status: 'running', attempts, nextRunAt: null, lastError: null });\n    step(job, { type: 'start', now });\n    now += 3;\n    if (delay === null) {\n      step(job, { ...failure, now }, { status: 'failed', nextRunAt: null, lastError: 'TRANSIENT' });\n    } else {\n      job = step(job, { ...failure, now }, { status: 'retry', nextRunAt: now + delay, lastError: 'TRANSIENT' });\n      now += delay;\n      step(job, { type: 'start', now: now - 1 });\n    }\n  }\n});\n\ntest('due time gates exhaustion for pending and retry jobs', () => {\n  for (const status of ['pending', 'retry']) {\n    for (const attempts of [4, 5]) {\n      const job = { ...initial, status, attempts, nextRunAt: 20 };\n      step(job, { type: 'start', now: 19 });\n      step(job, { type: 'start', now: 20 }, { status: 'failed', nextRunAt: null, lastError: 'ATTEMPTS_EXHAUSTED' });\n    }\n  }\n});\n\ntest('only literal true enables retries, and completion clears stale metadata', () => {\n  const job = { ...initial, status: 'running', attempts: 1, nextRunAt: 18, lastError: 'TRANSIENT' };\n  for (const flags of [{}, { retryable: false }, { retryable: 'true' }]) {\n    step(job, { type: 'failure', now: 30, error: 'private diagnostic', ...flags }, { status: 'failed', nextRunAt: null, lastError: 'PERMANENT' });\n  }\n  step(job, { type: 'success', now: 30 }, { status: 'succeeded', nextRunAt: null, lastError: null });\n});\n\ntest('cancellation clears queued work and all terminal states absorb late events', () => {\n  for (const status of ['pending', 'retry', 'running']) {\n    const job = status === 'pending' ? initial : { ...initial, status, attempts: 2, nextRunAt: 40, lastError: 'TRANSIENT' };\n    const cancelled = step(job, { type: 'cancel', now: 30 }, { status: 'cancelled', nextRunAt: null, lastError: null });\n    for (const type of ['start', 'success', 'failure', 'cancel', 'tick']) step(cancelled, { type, now: 50 });\n  }\n  for (const status of ['succeeded', 'failed']) {\n    const job = { ...initial, status, attempts: 2, nextRunAt: null, lastError: status === 'failed' ? 'PERMANENT' : null };\n    for (const type of ['start', 'success', 'failure', 'cancel', 'tick']) step(job, { type, now: 50 });\n  }\n});\n\ntest('queued jobs ignore premature results and unknown events', () => {\n  for (const status of ['pending', 'retry']) {\n    const job = { ...initial, status, attempts: status === 'retry' ? 1 : 0 };\n    for (const type of ['failure', 'success', 'tick']) step(job, { ...failure, type, now: 100 });\n  }\n});\n",
  "explanation": "A running job has already consumed its attempt. Only an eligible start increments the counter. A transient failure schedules capped exponential backoff while budget remains, and the final transient failure records TRANSIENT. An already-exhausted job records ATTEMPTS_EXHAUSTED when its start becomes due. Terminal states ignore late events, and failure handling never reads raw error text.\n\nChanged states use a shallow copy to preserve unrelated fields. No-ops return the original state. Nested metadata is shared and never modified by the transition.\n\nThe supplied regression tests cover the retry lifecycle, timing and budget boundaries, cancellation, late events, error codes, and input immutability. Neither the tests nor the grader were run, per the single-response constraint. The repair assumes valid inputs and models synchronous state transitions only."
}