{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/services/invariants/index.ts"],"sourcesContent":["import type { Task, TaskStatus } from '../../models/task';\n\n/**\n * Collapse a malformed snapshot to one row per task id.\n *\n * Normal writes replace tasks in place, but older or externally edited stores\n * can contain both a stale and a current copy. Prefer the copy with the newest\n * ISO update timestamp; equal or missing timestamps use the later array entry.\n * The winning row keeps the id's original position so repair does not reorder\n * an otherwise stable task list.\n */\nexport function canonicalizeTasks(tasks: readonly Task[]): Task[] {\n  const canonical: Task[] = [];\n  const indexById = new Map<number, number>();\n\n  for (const task of tasks) {\n    const existingIndex = indexById.get(task.id);\n    if (existingIndex === undefined) {\n      indexById.set(task.id, canonical.length);\n      canonical.push(task);\n      continue;\n    }\n\n    const existing = canonical[existingIndex];\n    if (!existing.updatedAt || !task.updatedAt || task.updatedAt >= existing.updatedAt) {\n      canonical[existingIndex] = task;\n    }\n  }\n\n  return canonical;\n}\n\n/**\n * Allowed forward transitions per source status.\n *\n * `failed` is recoverable (a delegation can be retried) so it may return to\n * pending or in_progress. `completed` is one-way to `deleted`; `deleted` is a\n * terminal tombstone.\n */\nexport const VALID_TRANSITIONS: Record<TaskStatus, ReadonlySet<TaskStatus>> = {\n  pending: new Set<TaskStatus>(['in_progress', 'completed', 'failed', 'deleted']),\n  in_progress: new Set<TaskStatus>(['pending', 'completed', 'failed', 'deleted']),\n  failed: new Set<TaskStatus>(['pending', 'in_progress', 'completed', 'deleted']),\n  completed: new Set<TaskStatus>(['deleted']),\n  deleted: new Set<TaskStatus>(),\n};\n\nexport function isTransitionValid(from: TaskStatus, to: TaskStatus): boolean {\n  if (from === to) return true;\n  return VALID_TRANSITIONS[from].has(to);\n}\n"],"mappings":";;;;;;;;;;AAWA,SAAgB,kBAAkB,OAAgC;CAChE,MAAM,YAAoB,CAAC;CAC3B,MAAM,4BAAY,IAAI,IAAoB;CAE1C,KAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,gBAAgB,UAAU,IAAI,KAAK,EAAE;EAC3C,IAAI,kBAAkB,KAAA,GAAW;GAC/B,UAAU,IAAI,KAAK,IAAI,UAAU,MAAM;GACvC,UAAU,KAAK,IAAI;GACnB;EACF;EAEA,MAAM,WAAW,UAAU;EAC3B,IAAI,CAAC,SAAS,aAAa,CAAC,KAAK,aAAa,KAAK,aAAa,SAAS,WACvE,UAAU,iBAAiB;CAE/B;CAEA,OAAO;AACT;;;;;;;;AASA,MAAa,oBAAiE;CAC5E,yBAAS,IAAI,IAAgB;EAAC;EAAe;EAAa;EAAU;CAAS,CAAC;CAC9E,6BAAa,IAAI,IAAgB;EAAC;EAAW;EAAa;EAAU;CAAS,CAAC;CAC9E,wBAAQ,IAAI,IAAgB;EAAC;EAAW;EAAe;EAAa;CAAS,CAAC;CAC9E,2BAAW,IAAI,IAAgB,CAAC,SAAS,CAAC;CAC1C,yBAAS,IAAI,IAAgB;AAC/B;AAEA,SAAgB,kBAAkB,MAAkB,IAAyB;CAC3E,IAAI,SAAS,IAAI,OAAO;CACxB,OAAO,kBAAkB,KAAK,CAAC,IAAI,EAAE;AACvC"}