sync-outcome.ts

6.8 KB · raw

1// Why a sync run ended, in a form both the service worker and the popup can2// name. Before this existed, "no eligible host", "runner not wired" and3// "synced fine, nothing to do" were all a bare `return` — indistinguishable4// to the user and to us. A failed sync and a successful no-op must never5// look the same again.67export type SyncOutcome =8  | "completed_changes"9  | "completed_no_changes"10  | "no_paired_devices"11  | "no_eligible_host"12  | "waiting_for_peer"13  | "runner_unavailable"14  | "sync_disabled"15  | "private_relay_unset"16  | "vault_locked"17  | "relay_unreachable"18  | "handshake_failed"19  | "vault_id_mismatch"20  | "peer_protocol_outdated"21  | "reset_required"22  | "sync_generation_unknown"23  | "sync_timeout"24  | "connection_lost"25  | "run_failed";2627/**28 * Outcomes where sync did not do what the user asked, so the UI shows red.29 *30 * Network-class failures (relay/handshake/timeout) are NOT in this set —31 * with a 10-minute background alarm the normal state for most minutes is32 * "the relay didn't answer this time". That is the network, not the app,33 * and the next alarm tick will retry. They live in `WARNINGS` so the34 * indicator goes yellow instead of red.35 */36const FAILURES: ReadonlySet<SyncOutcome> = new Set<SyncOutcome>([37  "no_eligible_host",38  "runner_unavailable",39  "private_relay_unset",40  "vault_id_mismatch",41  "peer_protocol_outdated",42  // §11: a device that owes a secure reset, or whose generation cannot be43  // read, has not synced and must be told so.44  "reset_required",45  "sync_generation_unknown",46  "run_failed",47]);4849/**50 * Network-class outcomes — the dial did not reach the peer this attempt.51 * The alarm keeps trying, so this is warn (yellow), not error (red).52 */53const WARNINGS: ReadonlySet<SyncOutcome> = new Set<SyncOutcome>([54  "relay_unreachable",55  "handshake_failed",56  "sync_timeout",57  "connection_lost",58  // vault_locked is the reader's vault being closed — not a misconfiguration,59  // not a broken sync. The user is in another app or hasn't unlocked yet; the60  // next unlock + retry will succeed. Same alarm-keeps-trying semantics as a61  // missed dial, so it lives here rather than in FAILURES.62  "vault_locked",63]);6465/**66 * A transport that hung up, as opposed to a protocol or vault problem.67 *68 * Lives here because two callers need the same judgement and must not drift:69 * the coordinator (a close while waiting for the peer's final SYNC_DONE echo70 * is a completed sync) and the trigger's error classifier (a close anywhere71 * else is network-class, so yellow rather than red).72 *73 * Deliberately does not match SyncFrameTooLargeError's "WebSocket close 1009"74 * — an oversized frame is a misbehaving peer and must keep failing.75 */76export function isConnectionClosedError(err: unknown): boolean {77  const message = err instanceof Error ? err.message : String(err);78  return /websocket closed|connection closed/i.test(message);79}8081export function isSyncFailure(outcome: SyncOutcome): boolean {82  return FAILURES.has(outcome);83}8485export function isSyncWarning(outcome: SyncOutcome): boolean {86  return WARNINGS.has(outcome);87}8889export interface SyncOutcomeSummary {90  status?: "idle" | "syncing" | "error";91  outcome?: SyncOutcome;92  entriesSent?: number;93  entriesReceived?: number;94  detail?: string;95  /** When the last attempt ended, success or not. */96  finishedAt?: number;97}9899/**100 * The one indicator light. `busy` pulses green, `ok` is solid green, `warn`101 * is yellow (a transient network miss — the next alarm tick retries),102 * `error` is red (the app is actually broken), `idle` is unlit. Nothing103 * that is actually broken may resolve to `ok` — a paired-but-unsyncable104 * device is red, not green. A dial that didn't reach the peer is yellow,105 * because the alarm will try again.106 */107export type SyncIndicator = "busy" | "ok" | "warn" | "error" | "idle";108109export function syncIndicator(s: SyncOutcomeSummary, pairedCount: number): SyncIndicator {110  if (s.status === "syncing") return "busy";111  if (s.outcome && isSyncFailure(s.outcome)) return "error";112  if (s.outcome && isSyncWarning(s.outcome)) return "warn";113  if (s.outcome === "completed_changes" || s.outcome === "completed_no_changes") {114    return "ok";115  }116  if (s.outcome === "waiting_for_peer") return "ok";117  // Paired with a healthy history but no attempt since this SW woke: still a118  // successful pair, which is what the light is claiming.119  if (pairedCount > 0 && s.outcome === undefined) return "ok";120  return "idle";121}122123const INDICATOR_LABEL: Record<SyncIndicator, string> = {124  busy: "Syncing",125  ok: "Sync healthy",126  warn: "Last sync attempt missed — will retry",127  error: "Sync problem",128  idle: "Not syncing",129};130131/** Colour alone is never the signal — this is the same claim in words. */132export function syncIndicatorLabel(133  indicator: SyncIndicator,134  s: SyncOutcomeSummary,135): string {136  return `${INDICATOR_LABEL[indicator]} — ${describeSyncOutcome(s)}`;137}138139const COPY: Record<SyncOutcome, string> = {140  completed_changes: "Synced",141  completed_no_changes: "Up to date — nothing to send or receive",142  no_paired_devices: "No devices paired yet",143  no_eligible_host: "Paired, but no device here can accept a sync",144  waiting_for_peer: "Waiting for the paired device to connect",145  runner_unavailable: "Sync isn't running in the background — reload Peach",146  sync_disabled: "Sync is switched off",147  private_relay_unset: "Private relay chosen but no address set",148  vault_locked: "Vault was locked — unlock and sync again",149  relay_unreachable: "Couldn't reach the relay",150  handshake_failed: "Reached the relay, but the device didn't answer",151  vault_id_mismatch: "That device holds a different vault",152  peer_protocol_outdated: "Update Peach on both devices, then re-pair them",153  reset_required:154    "Sync is reset on this device — update Peach everywhere, then re-pair each device",155  sync_generation_unknown: "This device's sync generation is unreadable — sync is stopped",156  sync_timeout: "The device stopped responding mid-sync",157  connection_lost: "The connection dropped mid-sync — will retry",158  run_failed: "Sync failed",159};160161/**162 * One plain-English line for a sync outcome. `detail` — the raw internal code163 * or the device inventory behind `no_eligible_host` — is appended rather than164 * hidden: it is the whole diagnostic on a real device, where devtools is not165 * an option.166 */167export function describeSyncOutcome(s: SyncOutcomeSummary): string {168  if (!s.outcome) return "Not synced yet";169  const base = COPY[s.outcome] ?? "Sync failed";170  if (s.outcome === "completed_changes") {171    const moved = [172      (s.entriesSent ?? 0) > 0 ? `${s.entriesSent} sent` : null,173      (s.entriesReceived ?? 0) > 0 ? `${s.entriesReceived} received` : null,174    ].filter(Boolean);175    return moved.length > 0 ? `${base} — ${moved.join(", ")}` : base;176  }177  return s.detail ? `${base} (${s.detail})` : base;178}