Resolving the current policy for queue by applying updates 1–6 in order:

- base_delay = 200, exponential = false, max_attempts = 4, retryable_codes = [408, 503] (update 1)
- cancel_wins = false, priority_high_first = false (update 4 overrides update 2)
- dedup_key = "key" (update 5 overrides update 2)
- fifo_ties = false (update 5 overrides update 2)
- dead_order = "input", delay_cap = 400 (update 3)
- stale_after = 2000, stale_inclusive = false (update 6 overrides update 5)

Interpretation choices in the implementation:

- **Dedup:** keep the last job for each key, with its original index.
- **Missing key:** a job with no dedup_key value is treated as unique.
- **Input:** `payload` has `now` and `jobs`. If `payload` is an array, it is treated as the job list with `now` = 0.
- **Cancelled jobs:** with cancel_wins false, a cancelled job goes to cancelled only if it is not retryable. A cancelled retryable job continues through the normal checks.
- **Failure:** a job has failed when last_code is nonzero. A retryable job whose failed_at is null or undefined keeps its due.
- **Dead:** the dead check applies to non-cancelled jobs, and a nonzero non-retryable last_code makes a job dead.
- **Delay:** with exponential false the multiplier is 1.
- **Waiting:** entries are `{id, at}`, where `at` is the computed due.


<br>

```json
{"files":{"main.ts":"import {run} from './core.ts';\nimport {POLICY} from './policy.ts';\nexport function evaluate(payload: any): any { return run(payload, POLICY); }\n","policy.ts":"export const POLICY: any = {\n  base_delay: 200,\n  exponential: false,\n  max_attempts: 4,\n  retryable_codes: [408, 503],\n  cancel_wins: false,\n  priority_high_first: false,\n  dedup_key: 'key',\n  fifo_ties: false,\n  dead_order: 'input',\n  delay_cap: 400,\n  stale_after: 2000,\n  stale_inclusive: false\n};\n","helpers.ts":"export function cmp(a: number, b: number): number { return a < b ? -1 : a > b ? 1 : 0; }\n","core.ts":"import {cmp} from './helpers.ts';\nexport function run(payload: any, policy: any): any {\n  const jobs: any[] = Array.isArray(payload) ? payload : (payload.jobs || []);\n  const now: number = Array.isArray(payload) ? 0 : payload.now;\n  const last = new Map<any, number>();\n  const uniq: number[] = [];\n  jobs.forEach((j, i) => {\n    const k = j[policy.dedup_key];\n    if (k === undefined || k === null) { uniq.push(i); return; }\n    last.set(k, i);\n  });\n  const sel: number[] = [];\n  jobs.forEach((j, i) => {\n    const k = j[policy.dedup_key];\n    if (k === undefined || k === null) sel.push(i);\n    else if (last.get(k) === i) sel.push(i);\n  });\n  const ready: any[] = [], waiting: any[] = [], dead: any[] = [], cancelled: any[] = [];\n  for (const i of sel) {\n    const j = jobs[i];\n    const code = j.last_code || 0;\n    const retry = policy.retryable_codes.includes(code);\n    if (j.cancelled && (policy.cancel_wins || !retry)) { cancelled.push({i, id: j.id}); continue; }\n    const age = now - j.created;\n    const stale = policy.stale_inclusive ? age >= policy.stale_after : age > policy.stale_after;\n    if (stale || j.attempt >= policy.max_attempts || (code !== 0 && !retry)) { dead.push({i, id: j.id}); continue; }\n    let due = j.due;\n    if (retry && j.failed_at !== undefined && j.failed_at !== null) {\n      const mult = policy.exponential ? Math.pow(2, Math.max(0, j.attempt - 1)) : 1;\n      const d = Math.min(policy.base_delay * mult, policy.delay_cap);\n      due = Math.max(due, j.failed_at + d);\n    }\n    if (due > now) waiting.push({i, id: j.id, at: due});\n    else ready.push({i, id: j.id, p: j.priority, c: j.created});\n  }\n  ready.sort((a, b) => {\n    let r = cmp(a.p, b.p);\n    if (policy.priority_high_first) r = -r;\n    if (r) return r;\n    r = cmp(a.c, b.c);\n    if (!policy.fifo_ties) r = -r;\n    if (r) return r;\n    return a.i - b.i;\n  });\n  if (policy.dead_order === 'id') dead.sort((a, b) => cmp(a.id, b.id) || a.i - b.i);\n  else dead.sort((a, b) => a.i - b.i);\n  waiting.sort((a, b) => a.i - b.i);\n  cancelled.sort((a, b) => a.i - b.i);\n  return {\n    ready: ready.map(x => x.id),\n    waiting: waiting.map(x => ({id: x.id, at: x.at})),\n    dead: dead.map(x => x.id),\n    cancelled: cancelled.map(x => x.id)\n  };\n}\n"}}
```