{"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 {effectiveDue, isRetryable} from './helpers.ts';\nexnexport 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 dedupKey: string = policy.dedup_key;\n  const byKey = new Map<any, {job: any, index: number}>();\n  jobs.forEach((job: any, index: number) => {\n    const k = job ? job[dedupKey] : undefined;\n    byKey.set(k, {job: job, index: index});\n  });\n  const selected = Array.from(byKey.values()).sort((a: any, b: any) => a.index - b.index);\n  const readyItems: any[] = [];\n  const waiting: any[] = [];\n  const deadItems: any[] = [];\n  const cancelled: any[] = [];\n  for (const item of selected) {\n    const job = item.job;\n    const retryable = isRetryable(job, policy);\n    if (job.cancelled && (policy.cancel_wins || !retryable)) {\n      cancelled.push({id: job.id, index: item.index});\n      continue;\n    }\n    const stale = policy.stale_inclusive ? (now - job.created >= policy.stale_after) : (now - job.created > 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: item.index});\n      continue;\n    }\n    const at = effectiveDue(job, policy, retryable);\n    if (at > now) {\n      waiting.push({id: job.id, at: at, index: item.index});\n    } else {\n      readyItems.push({id: job.id, priority: job.priority, created: job.created, index: item.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      if (a.id < b.id) return -1;\n      if (a.id > b.id) return 1;\n      return a.index - b.index;\n    });\n  } else {\n    deadItems.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 {\n    ready: readyItems.map((x: any) => x.id),\n    waiting: waiting.map((x: any) => ({id: x.id, at: x.at})),\n    dead: deadItems.map((x: any) => x.id),\n    cancelled: cancelled.map((x: any) => x.id)\n  };\n}\n", "policy.ts": "export const POLICY: any = {\n  base_delay: 200,\n  exponential: false,\n  max_attempts: 4,\n  retryable_codes: [408, 503],\n  cancel_wins: false,\n  dedup_key: \"key\",\n  fifo_ties: false,\n  priority_high_first: false,\n  dead_order: \"input\",\n  delay_cap: 400,\n  stale_after: 2000,\n  stale_inclusive: false\n};\n", "helpers.ts": "export function isRetryable(job: any, policy: any): boolean {\n  const codes: any[] = Array.isArray(policy.retryable_codes) ? policy.retryable_codes : [];\n  return codes.indexOf(job.last_code) !== -1;\n}\n\nexport function effectiveDue(job: any, policy: any, retryable: boolean): number {\n  if (!retryable || job.last_code === 0 || typeof job.failed_at !== 'number') return job.due;\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  return Math.max(job.due, job.failed_at + delay);\n}\n"}}