{"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: any = payload.now;\n  const jobs: any[] = Array.isArray(payload.jobs) ? payload.jobs : [];\n  const byKey = new Map<any, any>();\n  jobs.forEach((job: any, index: number) => {\n    byKey.set(job[policy.dedup_key], {job: job, index: index});\n  });\n  const selected: any[] = Array.from(byKey.values()).sort((a: any, b: any) => a.index - b.index);\n  const ready: any[] = [];\n  const waiting: any[] = [];\n  const dead: any[] = [];\n  const cancelled: any[] = [];\n  for (const item of selected) {\n    const job: any = item.job;\n    const index: number = item.index;\n    const retryable: boolean = isRetryable(job, policy);\n    if (job.cancelled && (policy.cancel_wins || !retryable)) {\n      cancelled.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 deadByCode: boolean = job.last_code !== 0 && !retryable;\n    if (stale || job.attempt >= policy.max_attempts || deadByCode) {\n      dead.push({id: job.id, index: index});\n      continue;\n    }\n    const due: any = 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    if (a.priority !== b.priority) return policy.priority_high_first ? b.priority - a.priority : a.priority - b.priority;\n    if (a.created !== b.created) return policy.fifo_ties ? a.created - b.created : b.created - a.created;\n    return a.index - b.index;\n  });\n  if (policy.dead_order === 'id') {\n    dead.sort((a: any, b: any) => {\n      const c: number = compareId(a.id, b.id);\n      return c !== 0 ? c : 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 {ready: ready.map((x: any) => x.id), waiting: waiting.map((x: any) => ({id: x.id, at: x.at})), dead: dead.map((x: any) => x.id), cancelled: cancelled.map((x: any) => x.id)};\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(job: any, policy: any): boolean { return Array.isArray(policy.retryable_codes) && policy.retryable_codes.includes(job.last_code); }\nexport function effectiveDue(job: any, policy: any, retryable: boolean): any { if (!retryable || job.last_code === 0) return job.due; const factor: number = policy.exponential ? Math.pow(2, Math.max(0, job.attempt - 1)) : 1; const delay: number = Math.min(policy.base_delay * factor, policy.delay_cap); return Math.max(job.due, job.failed_at + delay); }\nexport function compareId(a: any, b: any): number { if (typeof a === 'number' && typeof b === 'number') return a - b; const sa: string = String(a); const sb: string = String(b); if (sa < sb) return -1; if (sa > sb) return 1; return 0; }\n"}}