{"files":{"main.ts":"import {run} from './core.ts';\nimport {POLICY} from './policy.ts';\nexport function evaluate(payload: any): any { return run(payload, POLICY); }\n","core.ts":"import {compareIds} from './helpers.ts';\n\nexport function run(payload: any, policy: any): any {\n  const selected = new Map<any, {job: any; index: number}>();\n  payload.jobs.forEach((job: any, index: number) => {\n    selected.set(job[policy.dedup_key], {job, index});\n  });\n  const entries = Array.from(selected.values()).sort((a, b) => a.index - b.index);\n  const ready: {job: any; index: number}[] = [];\n  const waiting: {id: any; at: number}[] = [];\n  const dead: {job: any; index: number}[] = [];\n  const cancelled: any[] = [];\n\n  for (const entry of entries) {\n    const {job} = entry;\n    const retryable = policy.retryable_codes.includes(job.last_code);\n    if (job.cancelled && (policy.cancel_wins || !retryable)) {\n      cancelled.push(job.id);\n      continue;\n    }\n    const age = payload.now - job.created;\n    const stale = policy.stale_inclusive\n      ? age >= policy.stale_after\n      : age > policy.stale_after;\n    if (stale || job.attempt >= policy.max_attempts || (job.last_code !== 0 && !retryable)) {\n      dead.push(entry);\n      continue;\n    }\n    let due = job.due;\n    if (retryable) {\n      const multiplier = policy.exponential ? 2 ** Math.max(0, job.attempt - 1) : 1;\n      const delay = Math.min(policy.base_delay * multiplier, policy.delay_cap);\n      due = Math.max(due, job.failed_at + delay);\n    }\n    if (due > payload.now) {\n      waiting.push({id: job.id, at: due});\n    } else {\n      ready.push(entry);\n    }\n  }\n\n  ready.sort((a, b) => {\n    const priority = policy.priority_high_first\n      ? b.job.priority - a.job.priority\n      : a.job.priority - b.job.priority;\n    const created = policy.fifo_ties\n      ? a.job.created - b.job.created\n      : b.job.created - a.job.created;\n    return priority || created || a.index - b.index;\n  });\n  dead.sort((a, b) => policy.dead_order === 'id'\n    ? compareIds(a.job.id, b.job.id) || a.index - b.index\n    : a.index - b.index);\n\n  return {\n    ready: ready.map(entry => entry.job.id),\n    waiting,\n    dead: dead.map(entry => entry.job.id),\n    cancelled\n  };\n}\n","policy.ts":"export const POLICY: any = {\n  base_delay: 100,\n  exponential: true,\n  max_attempts: 3,\n  retryable_codes: [429, 500],\n  cancel_wins: true,\n  dedup_key: 'id',\n  fifo_ties: true,\n  priority_high_first: true,\n  dead_order: 'id',\n  delay_cap: 150,\n  stale_after: 1000,\n  stale_inclusive: true\n};\n","helpers.ts":"export function compareIds(a: any, b: any): number {\n  if (a < b) return -1;\n  if (a > b) return 1;\n  return 0;\n}\n"}}