{"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 {cappedDelay, isRetryable} from './helpers.ts';\nexport function run(payload: any, policy: any): any {\n  const now: number = payload.now;\n  const jobs: any[] = payload.jobs;\n  const dedupField: string = policy.dedup_key;\n  const byKey = new Map<any, {job: any, index: number}>();\n  for (let i = 0; i < jobs.length; i++) {\n    const job = jobs[i];\n    byKey.set(job[dedupField], {job: job, index: i});\n  }\n  const selected: {job: any, index: number}[] = Array.from(byKey.values());\n  const ready: {id: string, priority: number, created: number, index: number}[] = [];\n  const waiting: {id: string, at: number, index: number}[] = [];\n  const dead: {id: string, index: number}[] = [];\n  const cancelled: {id: string, index: number}[] = [];\n  for (const entry of selected) {\n    const job = entry.job;\n    const index = entry.index;\n    const retryable = 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 stale = policy.stale_inclusive ? (now - job.created >= policy.stale_after) : (now - job.created > policy.stale_after);\n    if (stale || job.attempt >= policy.max_attempts || (job.last_code !== 0 && !retryable)) {\n      dead.push({id: job.id, index: index});\n      continue;\n    }\n    let effectiveDue: number = job.due;\n    if (retryable) {\n      const delay = cappedDelay(job.attempt, policy);\n      effectiveDue = Math.max(job.due, job.failed_at + delay);\n    }\n    if (effectiveDue > now) {\n      waiting.push({id: job.id, at: effectiveDue, 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  dead.sort((a: any, b: any) => {\n    if (policy.dead_order === 'id') {\n      if (a.id < b.id) return -1;\n      if (a.id > b.id) return 1;\n      return a.index - b.index;\n    }\n    return 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\": 100, \"cancel_wins\": true, \"dead_order\": \"id\", \"dedup_key\": \"id\", \"delay_cap\": 150, \"exponential\": true, \"fifo_ties\": true, \"max_attempts\": 3, \"priority_high_first\": true, \"retryable_codes\": [429, 500], \"stale_after\": 1000, \"stale_inclusive\": true};\n", "helpers.ts": "export function isRetryable(lastCode: number, policy: any): boolean { return policy.retryable_codes.includes(lastCode); }\nexport function cappedDelay(attempt: number, policy: any): number { const factor = policy.exponential ? (2 ** Math.max(0, attempt - 1)) : 1; return Math.min(policy.base_delay * factor, policy.delay_cap); }\n"}}