{"files":{"main.ts":"import {run} from './core.ts';\nimport {POLICY} from './policy.ts';\nexport function evaluate(payload: any): any { return run(payload, POLICY); }\n","policy.ts":"export const POLICY: any = {\n  base_delay: 100,\n  exponential: true,\n  max_attempts: 3,\n  retryable_codes: [429, 500],\n  cancel_wins: true,\n  dedup_key: 'id',\n  fifo_ties: true,\n  priority_high_first: true,\n  dead_order: 'id',\n  delay_cap: 150,\n  stale_after: 1000,\n  stale_inclusive: true\n};\n","helpers.ts":"export function cmp(a: any, b: any): number {\n  return a < b ? -1 : a > b ? 1 : 0;\n}\n","core.ts":"import {cmp} from './helpers.ts';\n\nexport function run(payload: any, policy: any): any {\n  const jobs: any[] = Array.isArray(payload) ? payload : (payload && payload.jobs) || [];\n  const now: number = Array.isArray(payload) ? 0 : Number(payload && payload.now);\n  const last = new Map<any, number>();\n  jobs.forEach((j, i) => { last.set(j[policy.dedup_key], i); });\n  const items: {j: any, i: number}[] = [];\n  jobs.forEach((j, i) => { if (last.get(j[policy.dedup_key]) === i) items.push({j, i}); });\n  const ready: any[] = [], waiting: any[] = [], dead: any[] = [], cancelled: any[] = [];\n  for (const it of items) {\n    const j = it.j;\n    const code = j.last_code || 0;\n    const retryable = policy.retryable_codes.includes(code);\n    if (j.cancelled && (policy.cancel_wins || !retryable)) { cancelled.push(it); continue; }\n    const age = now - j.created;\n    const stale = policy.stale_inclusive ? age >= policy.stale_after : age > policy.stale_after;\n    if (stale || j.attempt >= policy.max_attempts || (code !== 0 && !retryable)) { dead.push(it); continue; }\n    let due = j.due === undefined || j.due === null ? 0 : j.due;\n    if (retryable && j.failed_at !== undefined && j.failed_at !== null) {\n      const mult = policy.exponential ? Math.pow(2, Math.max(0, (j.attempt || 0) - 1)) : 1;\n      const delay = Math.min(policy.base_delay * mult, policy.delay_cap);\n      due = Math.max(due, j.failed_at + delay);\n    }\n    if (due > now) waiting.push({it, at: due}); else ready.push(it);\n  }\n  ready.sort((a, b) => {\n    let c = cmp(a.j.priority, b.j.priority);\n    if (policy.priority_high_first) c = -c;\n    if (c) return c;\n    c = cmp(a.j.created, b.j.created);\n    if (!policy.fifo_ties) c = -c;\n    if (c) return c;\n    return a.i - b.i;\n  });\n  if (policy.dead_order === 'id') dead.sort((a, b) => cmp(a.j.id, b.j.id) || a.i - b.i);\n  else dead.sort((a, b) => a.i - b.i);\n  return {\n    ready: ready.map(x => x.j.id),\n    waiting: waiting.map(w => ({id: w.it.j.id, at: w.at})),\n    dead: dead.map(x => x.j.id),\n    cancelled: cancelled.map(x => x.j.id)\n  };\n}\n"}}