{"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, isRetryable} from './helpers.ts';\nexport function run(payload: any, policy: any): any {\n  const now = payload.now;\n  const jobs = Array.isArray(payload.jobs) ? payload.jobs : [];\n  const dedupField = policy.dedup_key;\n  const byKey = new Map<any, {job: any, index: number}>();\n  jobs.forEach((job: any, index: number) => {\n    const key = dedupField ? job[dedupField] : index;\n    byKey.set(key, {job, index});\n  });\n  const selected = 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 = entry.index;\n    const retryable = isRetryable(job, policy);\n    if (job.cancelled && (policy.cancel_wins || !retryable)) {\n      cancelled.push({id: job.id, index});\n      continue;\n    }\n    const stale = policy.stale_inclusive ? (now - job.created >= policy.stale_after) : (now - job.created > policy.stale_after);\n    const deadCondition = stale || job.attempt >= policy.max_attempts || (job.last_code !== 0 && !retryable);\n    if (deadCondition) {\n      dead.push({id: job.id, index});\n      continue;\n    }\n    const due = effectiveDue(job, policy, retryable);\n    if (due > now) {\n      waiting.push({id: job.id, at: due, index});\n    } else {\n      ready.push({id: job.id, priority: job.priority, created: job.created, index});\n    }\n  }\n  ready.sort((a: any, b: any) => {\n    const priorityDiff = policy.priority_high_first ? (b.priority - a.priority) : (a.priority - b.priority);\n    if (priorityDiff !== 0) return priorityDiff;\n    const createdDiff = policy.fifo_ties ? (a.created - b.created) : (b.created - a.created);\n    if (createdDiff !== 0) return createdDiff;\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) || (a.index - b.index));\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  \"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  return Array.isArray(policy.retryable_codes) && policy.retryable_codes.includes(job.last_code);\n}\nexport function effectiveDue(job: any, policy: any, retryable: boolean): number {\n  if (!retryable || job.last_code === 0) 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  const candidate = 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"}}