{
  "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 {compare} 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\n  const entries = Array.from(selected.values()).sort((a, b) => a.index - b.index);\n  const ready: typeof entries = [];\n  const dead: typeof entries = [];\n  const waiting: {id: any; at: number}[] = [];\n  const cancelled: any[] = [];\n  const now = payload.now;\n\n  for (const entry of entries) {\n    const job = entry.job;\n    const retryable = policy.retryable_codes.includes(job.last_code);\n\n    if (job.cancelled && (policy.cancel_wins || !retryable)) {\n      cancelled.push(job.id);\n      continue;\n    }\n\n    const age = 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\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\n    if (due > 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 = compare(a.job.priority, b.job.priority);\n    if (priority !== 0) return policy.priority_high_first ? -priority : priority;\n    const created = compare(a.job.created, b.job.created);\n    if (created !== 0) return policy.fifo_ties ? created : -created;\n    return a.index - b.index;\n  });\n\n  if (policy.dead_order === 'id') {\n    dead.sort((a, b) => compare(a.job.id, b.job.id) || a.index - b.index);\n  }\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  max_attempts: 4,\n  base_delay: 200,\n  exponential: false,\n  retryable_codes: [408, 503],\n  cancel_wins: false,\n  priority_high_first: false,\n  fifo_ties: false,\n  dedup_key: 'key',\n  stale_after: 2000,\n  stale_inclusive: false,\n  delay_cap: 400,\n  dead_order: 'input'\n};\n",
    "helpers.ts": "export function compare(a: any, b: any): number {\n  return a < b ? -1 : a > b ? 1 : 0;\n}\n"
  }
}