// Before the pilot: jobFor and the dedupe test as plain TypeScript, from Rebasely// ops/oncall/lib/watcher.ts at fcf3b6a9 (the dedupe test was inline in Watcher.enqueue).import type { ApiEvent, HandledEntry, Job, WatchConfig } from "./types.ts";const RANK: Record<string, number> = { critical: 0, page: 1, ticket: 2, direct: 3 };const parse = (s: string | null): Record<string, unknown> => { if (!s) return {}; try { const v = JSON.parse(s) as unknown; return v && typeof v === "object" && !Array.isArray(v) ? (v as Record<string, unknown>) : {}; } catch { return {}; }};export const TRIAGE_ONLY_PREFIXES = ["bug_report:", "bug_report_reply:", "bug_reports:", "ux_issues:"] as const;export function jobFor(e: ApiEvent, cfg: WatchConfig, now: number): Job | null { if (e.environment && !cfg.environments.includes(e.environment)) return null; const data = parse(e.data); if ((e.kind === "alert_opened" || e.kind === "alert_severity_rose") && e.key) { if (e.key.startsWith("oncall:") && !e.key.startsWith("oncall:drill:")) return null; if (TRIAGE_ONLY_PREFIXES.some((p) => e.key!.startsWith(p))) return null; const cls = typeof data.class === "string" ? data.class : "ticket"; if (cls === "direct") return null; return { id: `e${e.id}`, kind: "incident", key: e.key, alertKey: e.key, incident: e.kind === "alert_opened" ? e.at : null, rank: RANK[cls] ?? 2, event: { ...e, data }, queuedAt: now }; } if (e.kind === "abuse_flag") { const subject = typeof data.subject_id === "string" ? data.subject_id : String(e.id); return { id: `e${e.id}`, kind: "incident", key: `abuse:${subject}`, alertKey: null, incident: null, rank: 2, event: { ...e, data }, queuedAt: now }; } return null;}export const isDuplicate = (handled: Record<string, HandledEntry>, j: Job, now: number, dedupeHours: number): boolean => { const h = handled[j.key]; const fresh = h && now - h.at < dedupeHours * 3_600_000; return Boolean(fresh && (j.incident === null || h.incident === null || j.incident <= h.at));};