{"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 {isRetryable, effectiveDue, compareId} 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 dedupKey: string = policy.dedup_key;\n  const byKey = new Map<any, any>();\n  for (let i = 0; i < jobs.length; i++) {\n    const job = jobs[i];\n    const k = job[dedupKey];\n    byKey.set(k, {job: job, index: i});\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 = entry.job;\n    const index: number = entry.index;\n    const retryable: boolean = isRetryable(job.last_code, policy);\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 isDead: boolean = stale || job.attempt >= policy.max_attempts || (job.last_code !== 0 && !retryable);\n    if (isDead) {\n      dead.push({id: job.id, index: index});\n      continue;\n    }\n    const due: number = effectiveDue(job, policy, retryable);\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 = 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    dead.sort((a: any, b: any) => {\n      const c = compareId(a.id, b.id);\n      if (c !== 0) return c;\n      return a.index - b.index;\n    });\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 {\n    ready: ready.map((x: any) => x.id),\n    waiting: waiting.map((x: any) => ({id: x.id, at: x.at})),\n    dead: dead.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  priority_high_first: false,\n  fifo_ties: false,\n  dedup_key: \"key\",\n  dead_order: \"input\",\n  delay_cap: 400,\n  stale_after: 2000,\n  stale_inclusive: false\n};\n", "helpers.ts": "export function isRetryable(lastCode: any, policy: any): boolean {\n  const codes: any[] = Array.isArray(policy.retryable_codes) ? policy.retryable_codes : [];\n  return codes.indexOf(lastCode) !== -1;\n}\nexport function effectiveDue(job: any, policy: any, retryable: boolean): number {\n  if (!retryable || job.last_code === 0) return job.due;\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 candidate: number = job.failed_at + delay;\n  return Math.max(job.due, candidate);\n}\nexport 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"}}