{"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 {compareAscending} from './helpers.ts';\nexport function run(payload: any, policy: any): any {\n  const now: number = payload.now;\n  const jobs: any[] = Array.isArray(payload.jobs) ? payload.jobs : [];\n  const dedupMap = new Map<any, any>();\n  for (let i = 0; i < jobs.length; i++) {\n    const job = jobs[i];\n    dedupMap.set(job[policy.dedup_key], {job: job, index: i});\n  }\n  const selected: any[] = Array.from(dedupMap.values());\n  const retryableCodes: any[] = Array.isArray(policy.retryable_codes) ? policy.retryable_codes : [];\n  const ready: any[] = [];\n  const waiting: any[] = [];\n  const dead: any[] = [];\n  const cancelled: any[] = [];\n  for (const entry of selected) {\n    const job = entry.job;\n    const index: number = entry.index;\n    const retryable: boolean = retryableCodes.indexOf(job.last_code) !== -1;\n    if (job.cancelled && (policy.cancel_wins || !retryable)) {\n      cancelled.push({id: job.id, index: index});\n      continue;\n    }\n    const age: number = now - job.created;\n    const stale: boolean = policy.stale_inclusive ? age >= policy.stale_after : age > policy.stale_after;\n    const exhausted: boolean = job.attempt >= policy.max_attempts;\n    const nonretryableFailure: boolean = job.last_code !== 0 && !retryable;\n    if (stale || exhausted || nonretryableFailure) {\n      dead.push({id: job.id, index: index});\n      continue;\n    }\n    let effectiveDue: number = job.due;\n    if (retryable && job.last_code !== 0) {\n      const factor: number = policy.exponential ? Math.pow(2, Math.max(0, job.attempt - 1)) : 1;\n      const delay: number = Math.min(policy.base_delay * factor, policy.delay_cap);\n      effectiveDue = Math.max(job.due, job.failed_at + delay);\n    }\n    if (effectiveDue > now) {\n      waiting.push({id: job.id, at: effectiveDue, index: index});\n    } else {\n      ready.push({id: job.id, priority: job.priority, created: job.created, index: index});\n    }\n  }\n  ready.sort((a: any, b: any) => {\n    if (a.priority !== b.priority) return policy.priority_high_first ? b.priority - a.priority : a.priority - b.priority;\n    if (a.created !== b.created) return policy.fifo_ties ? a.created - b.created : b.created - a.created;\n    return a.index - b.index;\n  });\n  dead.sort((a: any, b: any) => {\n    if (policy.dead_order === 'id') {\n      const c = compareAscending(a.id, b.id);\n      if (c !== 0) return c;\n    }\n    return a.index - b.index;\n  });\n  waiting.sort((a: any, b: any) => a.index - b.index);\n  cancelled.sort((a: any, b: any) => a.index - b.index);\n  return {ready: ready.map((x: any) => x.id), waiting: waiting.map((x: any) => ({id: x.id, at: x.at})), dead: dead.map((x: any) => x.id), cancelled: cancelled.map((x: any) => x.id)};\n}\n", "policy.ts": "export const POLICY: any = {\"base_delay\": 200, \"exponential\": false, \"max_attempts\": 4, \"retryable_codes\": [408, 503], \"cancel_wins\": false, \"dedup_key\": \"key\", \"fifo_ties\": false, \"priority_high_first\": false, \"dead_order\": \"input\", \"delay_cap\": 400, \"stale_after\": 2000, \"stale_inclusive\": false};\n", "helpers.ts": "export function compareAscending(a: any, b: any): number { return a < b ? -1 : a > b ? 1 : 0; }\n"}}