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