{"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} from './helpers.ts';\nexport function run(payload: any, policy: any): any {\n  const now: number = payload && typeof payload.now === 'number' ? payload.now : 0;\n  const jobs: any[] = payload && Array.isArray(payload.jobs) ? payload.jobs : [];\n  const dedupKey: string = policy.dedup_key;\n  const byKey = new Map<any, {job: any, index: number}>();\n  for (let i = 0; i < jobs.length; i++) {\n    const job = jobs[i];\n    byKey.set(job[dedupKey], {job: job, index: i});\n  }\n  const selected = Array.from(byKey.values()).sort((a: any, b: any) => a.index - b.index);\n  const retryableCodes: any[] = Array.isArray(policy.retryable_codes) ? policy.retryable_codes : [];\n  const readyItems: any[] = [];\n  const waitingItems: any[] = [];\n  const deadItems: any[] = [];\n  const cancelledItems: any[] = [];\n  for (const item of selected) {\n    const job = item.job;\n    const index = item.index;\n    const retryable = retryableCodes.includes(job.last_code);\n    if (job.cancelled) {\n      if (policy.cancel_wins || !retryable) {\n        cancelledItems.push({id: job.id, index: index});\n        continue;\n      }\n    }\n    const age = now - job.created;\n    const stale = policy.stale_inclusive ? age >= policy.stale_after : age > policy.stale_after;\n    const nonretryableFailure = job.last_code !== 0 && !retryable;\n    if (stale || job.attempt >= policy.max_attempts || nonretryableFailure) {\n      deadItems.push({id: job.id, index: index});\n      continue;\n    }\n    let due = job.due;\n    if (retryable && job.last_code !== 0) {\n      const factor = policy.exponential ? Math.pow(2, Math.max(0, job.attempt - 1)) : 1;\n      const delay = Math.min(policy.base_delay * factor, policy.delay_cap);\n      const retryAt = job.failed_at + delay;\n      due = Math.max(due, retryAt);\n    }\n    if (due > now) {\n      waitingItems.push({id: job.id, at: due, index: index});\n    } else {\n      readyItems.push({id: job.id, priority: job.priority, created: job.created, index: index});\n    }\n  }\n  readyItems.sort((a: any, b: any) => {\n    const p = policy.priority_high_first ? b.priority - a.priority : a.priority - b.priority;\n    if (p !== 0) return p;\n    const c = 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    deadItems.sort((a: any, b: any) => {\n      const d = compareId(a.id, b.id);\n      if (d !== 0) return d;\n      return a.index - b.index;\n    });\n  } else {\n    deadItems.sort((a: any, b: any) => a.index - b.index);\n  }\n  waitingItems.sort((a: any, b: any) => a.index - b.index);\n  cancelledItems.sort((a: any, b: any) => a.index - b.index);\n  return {\n    ready: readyItems.map((x: any) => x.id),\n    waiting: waitingItems.map((x: any) => ({id: x.id, at: x.at})),\n    dead: deadItems.map((x: any) => x.id),\n    cancelled: cancelledItems.map((x: any) => x.id)\n  };\n}\n", "policy.ts": "export const POLICY: any = {\"base_delay\": 100, \"exponential\": true, \"max_attempts\": 3, \"retryable_codes\": [429, 500], \"cancel_wins\": true, \"dedup_key\": \"id\", \"fifo_ties\": true, \"priority_high_first\": true, \"dead_order\": \"id\", \"delay_cap\": 150, \"stale_after\": 1000, \"stale_inclusive\": true};\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(a);\n  const sb = String(b);\n  if (sa < sb) return -1;\n  if (sa > sb) return 1;\n  return 0;\n}\n"}}