{
  "candidate": "function transition(state, event, policy) {\n  const { status, attempts } = state;\n  if (status === 'succeeded' || status === 'failed' || status === 'cancelled') {\n    return state;\n  }\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') || event.now < state.nextRunAt) {\n        return state;\n      }\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 retryable = event.retryable === true;\n      const lastError = retryable ? 'TRANSIENT' : 'PERMANENT';\n      if (retryable && 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 };\n      }\n      return { ...state, status: 'failed', nextRunAt: null, lastError };\n    }\n\n    default:\n      return state;\n  }\n}\n\nmodule.exports = { transition };\n",
  "tests": "'use strict';\nconst assert = require('node:assert/strict');\nconst test = require('node:test');\nconst { transition } = require('./candidate.cjs');\n\nconst initial = Object.freeze({\n  id: 'job-1', status: 'pending', attempts: 0, nextRunAt: 0, lastError: null,\n  metadata: Object.freeze({ owner: 'ops' })\n});\nconst policy = Object.freeze({ maxAttempts: 4, baseDelay: 10, maxDelay: 25 });\n\nfunction check(state, event, changes = {}) {\n  const expected = { ...state, ...changes };\n  const actual = transition(Object.freeze(state), Object.freeze(event), policy);\n  assert.deepStrictEqual(actual, expected);\n  return actual;\n}\n\ntest('retry lifecycle counts starts, enforces due times and caps the delay', () => {\n  let state = initial;\n  let now = 0;\n  let attempts = 0;\n  for (const delay of [10, 20, 25]) {\n    attempts += 1;\n    state = check(state, { type: 'start', now }, {\n      status: 'running', attempts, nextRunAt: null, lastError: null\n    });\n    check(state, { type: 'start', now });\n    now += 3;\n    state = check(state, { type: 'failure', now, retryable: true, error: 'private detail' }, {\n      status: 'retry', nextRunAt: now + delay, lastError: 'TRANSIENT'\n    });\n    now += delay;\n    check(state, { type: 'start', now: now - 1 });\n  }\n  state = check(state, { type: 'start', now }, {\n    status: 'running', attempts: 4, nextRunAt: null, lastError: null\n  });\n  check(state, { type: 'failure', now, retryable: true }, {\n    status: 'failed', nextRunAt: null, lastError: 'TRANSIENT'\n  });\n});\n\ntest('exhausted starts fail only when due', () => {\n  for (const status of ['pending', 'retry']) {\n    for (const attempts of [4, 5]) {\n      const state = { ...initial, status, attempts, nextRunAt: 50 };\n      check(state, { type: 'start', now: 49 });\n      check(state, { type: 'start', now: 50 }, {\n        status: 'failed', nextRunAt: null, lastError: 'ATTEMPTS_EXHAUSTED'\n      });\n    }\n  }\n});\n\ntest('running completions clear stale metadata and require literal true to retry', () => {\n  const state = { ...initial, status: 'running', attempts: 1, nextRunAt: 70, lastError: 'TRANSIENT' };\n  check(state, { type: 'success', now: 60 }, { status: 'succeeded', nextRunAt: null, lastError: null });\n  for (const flags of [{}, { retryable: false }, { retryable: 'true' }]) {\n    check(state, { type: 'failure', now: 60, error: 'private detail', ...flags }, {\n      status: 'failed', nextRunAt: null, lastError: 'PERMANENT'\n    });\n  }\n});\n\ntest('cancellation clears each active state and terminal states ignore every event', () => {\n  for (const status of ['pending', 'running', 'retry']) {\n    const state = { ...initial, status, attempts: 2, nextRunAt: 80, lastError: 'TRANSIENT' };\n    check(state, { type: 'cancel', now: 5 }, { status: 'cancelled', nextRunAt: null, lastError: null });\n  }\n  for (const status of ['succeeded', 'failed', 'cancelled']) {\n    const lastError = status === 'failed' ? 'PERMANENT' : null;\n    const state = { ...initial, status, attempts: 2, nextRunAt: null, lastError };\n    for (const type of ['start', 'cancel', 'success', 'failure', 'tick']) {\n      check(state, { type, now: 100, retryable: true });\n    }\n  }\n});\n\ntest('queued jobs ignore late completions and unknown events', () => {\n  for (const status of ['pending', 'retry']) {\n    const state = { ...initial, status, attempts: 1, nextRunAt: 20 };\n    for (const type of ['success', 'failure', 'tick']) {\n      check(state, { type, now: 25, retryable: true, error: 'private detail' });\n    }\n  }\n});\n",
  "explanation": "Only an eligible start consumes an attempt. A retryable failure schedules capped exponential backoff while budget remains. At the limit, the failure retains its TRANSIENT cause. A due start with no budget records ATTEMPTS_EXHAUSTED. Terminal states ignore later events, and cancellation clears queued work. Failure handling derives its error code solely from the retryable flag.\n\nChanged states use shallow copies to preserve unrelated fields. No-ops return the input state, and nested metadata is shared without modification. The tests cover a four-attempt lifecycle, timing and budget boundaries, cancellation, terminal and out-of-order events, error redaction, and frozen inputs. Neither the tests nor the grader were run in this single-response trial. Concurrency and persistence are outside the transition model."
}