Design notes: the payload is an object with `now` (integer) and `jobs`. I also accept `payload.now` as the only source of the integer. If the payload is itself an array, `now` defaults to 0.

Deduplication: for each dedup_key value, keep the last job and its actual original index. Output order follows the surviving jobs' original indices.

Cancelled handling: with cancel_wins true, any cancelled job goes to cancelled. A cancelled job that is not retryable also goes to cancelled.

Dead conditions: stale (now-created >= stale_after), attempt >= max_attempts, or a nonzero last_code that is not retryable.

Delay: for a retryable job with failed_at present, due = max(due, failed_at + min(base_delay * 2^max(0, attempt-1), cap)).

```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 = {\"base_delay\":100,\"cancel_wins\":true,\"dead_order\":\"id\",\"dedup_key\":\"id\",\"delay_cap\":150,\"exponential\":true,\"fifo_ties\":true,\"max_attempts\":3,\"priority_high_first\":true,\"retryable_codes\":[429,500],\"stale_after\":1000,\"stale_inclusive\":true};\n","helpers.ts":"export function cmp(a: any, b: any): 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 now = Array.isArray(payload) ? 0 : (payload && payload.now) || 0;\n  const jobs: any[] = Array.isArray(payload) ? payload : (payload && payload.jobs) || [];\n  const last = new Map<any, number>();\n  jobs.forEach((j, i) => { last.set(j[policy.dedup_key], i); });\n  const kept: {j: any, i: number}[] = [];\n  jobs.forEach((j, i) => { if (last.get(j[policy.dedup_key]) === i) kept.push({j, i}); });\n  const ready: any[] = [], waiting: any[] = [], dead: any[] = [], cancelled: any[] = [];\n  for (const e of kept) {\n    const j = e.j;\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(e); 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 || 0) >= policy.max_attempts || (code !== 0 && !retry)) { dead.push(e); continue; }\n    let due = j.due || 0;\n    if (retry && j.failed_at !== undefined && j.failed_at !== null) {\n      const mult = policy.exponential ? Math.pow(2, Math.max(0, (j.attempt || 0) - 1)) : 1;\n      const d = j.failed_at + Math.min(policy.base_delay * mult, policy.delay_cap);\n      due = Math.max(due, d);\n    }\n    if (due > now) waiting.push({e, at: due}); else ready.push(e);\n  }\n  ready.sort((a, b) => {\n    let c = cmp(a.j.priority, b.j.priority);\n    if (policy.priority_high_first) c = -c;\n    if (c) return c;\n    c = cmp(a.j.created, b.j.created);\n    if (!policy.fifo_ties) c = -c;\n    if (c) return c;\n    return a.i - b.i;\n  });\n  if (policy.dead_order === 'id') dead.sort((a, b) => cmp(a.j.id, b.j.id) || a.i - b.i);\n  else dead.sort((a, b) => a.i - b.i);\n  waiting.sort((a, b) => a.e.i - b.e.i);\n  cancelled.sort((a, b) => a.i - b.i);\n  return {\n    ready: ready.map(e => e.j.id),\n    waiting: waiting.map(w => ({id: w.e.j.id, at: w.at})),\n    dead: dead.map(e => e.j.id),\n    cancelled: cancelled.map(e => e.j.id)\n  };\n}\n"}}
```