{"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 {compareId, effectiveDue} from './helpers.ts';\nexport function run(payload: any, policy: any): any {\n  const now: any = payload.now;\n  const jobs: any[] = Array.isArray(payload.jobs) ? payload.jobs : [];\n  const dedupKey: string = policy.dedup_key;\n  const byKey: any = new Map();\n  jobs.forEach((job: any, index: number) => {\n    byKey.set(job[dedupKey], {job: job, index: index});\n  });\n  const selected: any[] = Array.from(byKey.values());\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: any = entry.job;\n    const index: number = entry.index;\n    const retryable: boolean = Array.isArray(policy.retryable_codes) && policy.retryable_codes.includes(job.last_code);\n    if (job.cancelled && (policy.cancel_wins || !retryable)) {\n      cancelled.push({id: job.id, index: index});\n      continue;\n    }\n    const stale: boolean = policy.stale_inclusive ? (now - job.created >= policy.stale_after) : (now - job.created > policy.stale_after);\n    if (stale || job.attempt >= policy.max_attempts || (job.last_code !== 0 && !retryable)) {\n      dead.push({id: job.id, index: index});\n      continue;\n    }\n    const due: any = effectiveDue(job, retryable, policy);\n    if (due > now) {\n      waiting.push({id: job.id, at: due, 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    const p: number = policy.priority_high_first ? (b.priority - a.priority) : (a.priority - b.priority);\n    if (p !== 0) return p;\n    const c: number = policy.fifo_ties ? (a.created - b.created) : (b.created - a.created);\n    if (c !== 0) return c;\n    return a.index - b.index;\n  });\n  if (policy.dead_order === 'id') {\n    dead.sort((a: any, b: any) => compareId(a.id, b.id));\n  } else {\n    dead.sort((a: any, b: any) => 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 = {\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 compareId(a: any, b: any): number {\n  if (typeof a === 'number' && typeof b === 'number') return a - b;\n  const sa: string = String(a);\n  const sb: string = String(b);\n  if (sa < sb) return -1;\n  if (sa > sb) return 1;\n  return 0;\n}\nexport function effectiveDue(job: any, retryable: boolean, policy: any): any {\n  let due: any = job.due;\n  if (retryable && job.last_code !== 0 && job.failed_at !== null && job.failed_at !== undefined) {\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    const retryDue: any = job.failed_at + delay;\n    if (retryDue > due) due = retryDue;\n  }\n  return due;\n}\n"}}