{"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, compareByIndex, 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 dedupMap = new Map<any, any>();\n  for (let i = 0; i < jobs.length; i++) {\n    const job = jobs[i];\n    const dedupValue = job[policy.dedup_key];\n    dedupMap.set(dedupValue, {job: job, index: i});\n  }\n  const selected: any[] = Array.from(dedupMap.values());\n  const retryableSet = new Set<any>(policy.retryable_codes || []);\n  const readyItems: any[] = [];\n  const waitingItems: any[] = [];\n  const deadItems: any[] = [];\n  const cancelledItems: 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, retryableSet);\n    if (job.cancelled && (policy.cancel_wins || !retryable)) {\n      cancelledItems.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    const nonretryableFailure: boolean = 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    const due: number = effectiveDue(job, policy, retryable);\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    if (a.priority !== b.priority) {\n      return policy.priority_high_first ? b.priority - a.priority : a.priority - b.priority;\n    }\n    if (a.created !== b.created) {\n      return policy.fifo_ties ? a.created - b.created : b.created - a.created;\n    }\n    return a.index - b.index;\n  });\n  if (policy.dead_order === 'id') {\n    deadItems.sort((a: any, b: any) => {\n      const c = compareId(a.id, b.id);\n      return c !== 0 ? c : a.index - b.index;\n    });\n  } else {\n    deadItems.sort(compareByIndex);\n  }\n  waitingItems.sort(compareByIndex);\n  cancelledItems.sort(compareByIndex);\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\": 200, \"cancel_wins\": false, \"dead_order\": \"input\", \"dedup_key\": \"key\", \"delay_cap\": 400, \"exponential\": false, \"fifo_ties\": false, \"max_attempts\": 4, \"priority_high_first\": false, \"retryable_codes\": [408, 503], \"stale_after\": 2000, \"stale_inclusive\": false};\n", "helpers.ts": "export function isRetryable(lastCode: any, retryableCodes: Set<any>): boolean { return retryableCodes.has(lastCode); }\nexport function effectiveDue(job: any, policy: any, retryable: boolean): number {\n  if (!retryable) { 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  return Math.max(job.due, job.failed_at + delay);\n}\nexport function compareByIndex(a: any, b: any): number { return a.index - b.index; }\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"}}