{"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';\n\nexport function run(payload: any, policy: any): any {\n  const now = payload.now;\n  const jobs: any[] = payload.jobs || [];\n  const last = new Map<any, number>();\n  jobs.forEach((j, i) => { last.set(j[policy.dedup_key], i); });\n  const idxs = Array.from(last.values()).sort((a, b) => a - b);\n  const ready: any[] = [];\n  const waiting: any[] = [];\n  const dead: any[] = [];\n  const cancelled: any[] = [];\n  for (const i of idxs) {\n    const j = jobs[i];\n    const retryable = policy.retryable_codes.includes(j.last_code);\n    if (j.cancelled && (policy.cancel_wins || !retryable)) {\n      cancelled.push({j, i});\n      continue;\n    }\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)) {\n      dead.push({j, i});\n      continue;\n    }\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({j, i, due});\n    else ready.push({j, i});\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  return {\n    ready: ready.map(x => x.j.id),\n    waiting: waiting.map(x => ({id: x.j.id, at: x.due})),\n    dead: dead.map(x => x.j.id),\n    cancelled: cancelled.map(x => x.j.id)\n  };\n}\n"}}