{"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: 100,\n  cancel_wins: true,\n  dead_order: 'id',\n  dedup_key: 'id',\n  delay_cap: 150,\n  exponential: true,\n  fifo_ties: true,\n  max_attempts: 3,\n  priority_high_first: true,\n  retryable_codes: [429, 500],\n  stale_after: 1000,\n  stale_inclusive: true\n};\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 = payload.now;\n  const jobs: any[] = payload.jobs || [];\n  const map = new Map<any, {j: any, i: number}>();\n  jobs.forEach((j, i) => { map.set(j[policy.dedup_key], {j, i}); });\n  const items = Array.from(map.values()).sort((a, b) => a.i - b.i);\n  const ready: any[] = [], waiting: any[] = [], dead: any[] = [], cancelled: any[] = [];\n  for (const it of items) {\n    const j = it.j;\n    const retryable = policy.retryable_codes.includes(j.last_code);\n    if (j.cancelled && (policy.cancel_wins || !retryable)) { cancelled.push(it); 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 || (j.last_code !== 0 && !retryable)) { dead.push(it); continue; }\n    let due = j.due;\n    if (retryable) {\n      const mult = policy.exponential ? Math.pow(2, Math.max(0, j.attempt - 1)) : 1;\n      const delay = Math.min(policy.base_delay * mult, policy.delay_cap);\n      due = Math.max(due, j.failed_at + delay);\n    }\n    if (due > now) waiting.push({it, at: due}); else ready.push(it);\n  }\n  const hi = policy.priority_high_first, fifo = policy.fifo_ties;\n  ready.sort((a, b) => {\n    let c = cmp(a.j.priority, b.j.priority);\n    if (hi) c = -c;\n    if (c) return c;\n    c = cmp(a.j.created, b.j.created);\n    if (!fifo) 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  return {\n    ready: ready.map(x => x.j.id),\n    waiting: waiting.map(w => ({id: w.it.j.id, at: w.at})),\n    dead: dead.map(x => x.j.id),\n    cancelled: cancelled.map(x => x.j.id)\n  };\n}\n"}}