All files / src/features/sorting decisionQueue.ts

89.55% Statements 120/134
78.2% Branches 61/78
100% Functions 23/23
97.19% Lines 104/107

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267                                                                                  53x 53x 53x 53x           53x     365x       1157x       19x       783x 523x 523x 523x 522x 522x 521x 521x 521x   1x         776x 776x 776x 776x       776x 776x 514x 513x         310x 310x 310x             284x       94x 94x                   287x 287x 287x 296x 296x               287x   287x 287x       10x 9x 9x 9x 9x   8x     1x         76x 76x 76x 76x 75x 75x 74x 74x 60x   14x   74x             68x 303x       8x       11x 11x 11x 11x 11x 11x                   5x       24x 24x 24x 22x 22x         24x       12x 12x 12x 12x 14x 10x 10x 10x 10x 10x   12x       9x 9x 9x                     12x 12x 12x 8x 7x 7x 7x 7x       7x 7x 8x 8x   7x   12x 3x 3x            
// Client decision queue — pure + storage, no React (spec §3.1). One versioned,
// curator-keyed envelope per (curator, session) in localStorage; array order in
// storage IS flush order (no seq field). Every read verifies the envelope's
// curatorId/sessionId against the requested key and never deletes a foreign or
// corrupt blob — sweepExpired (GC) owns eventual cleanup. Storage guards follow
// the round-preference house pattern (typeof window + try/catch) so private
// mode degrades instead of throwing.
import { ulid } from "ulid";
 
export interface QueueKey {
  curatorId: string;
  sessionId: string;
  // Stamped onto the envelope at enqueue time so the flusher can route
  // disposition entries to /api/collections/{collectionId}/dispositions.
  collectionId?: string;
}
 
export type PendingEntry =
  | {
      kind: "decision";
      clientKey: string;
      type: "triage" | "pairwise_compare" | "usage";
      decision: Record<string, unknown>;
      enqueuedAt: string;
    }
  | {
      kind: "disposition";
      clientKey: string;
      itemId: string;
      disposition: "set_aside" | "deferred" | "clear";
      enqueuedAt: string;
    };
 
interface Envelope {
  v: 1;
  curatorId: string;
  sessionId: string;
  collectionId?: string;
  entries: PendingEntry[];
}
 
const PENDING_PREFIX = "adored.pending.v1.";
const QUARANTINE_PREFIX = "adored.quarantine.v1.";
const QUARANTINE_CAP = 50;
export const GC_TTL_MS = 30 * 24 * 60 * 60 * 1000; // 30 days — dwarfs any legitimate flush window
 
// Dispatched (same-document) after every successful queue write so the
// flusher's trigger hook can observe enqueues without polling; the payload is
// the pending storage key. Cross-document observation rides the Web Lock +
// re-read-under-lock in the flusher instead.
export const PENDING_CHANGED_EVENT = "adored:pending-changed";
 
export function newClientKey(): string {
  return ulid().toLowerCase(); // matches the shared ClientKey schema
}
 
export function pendingStorageKey(key: QueueKey): string {
  return `${PENDING_PREFIX}${key.curatorId}.${key.sessionId}`;
}
 
export function quarantineStorageKey(key: QueueKey): string {
  return `${QUARANTINE_PREFIX}${key.curatorId}.${key.sessionId}`;
}
 
function parseEnvelope(raw: string | null): Envelope | null {
  if (raw === null) return null;
  try {
    const parsed: unknown = JSON.parse(raw);
    Iif (typeof parsed !== "object" || parsed === null) return null;
    const env = parsed as Partial<Envelope>;
    if (env.v !== 1) return null;
    Iif (typeof env.curatorId !== "string" || typeof env.sessionId !== "string") return null;
    Iif (!Array.isArray(env.entries)) return null;
    return env as Envelope;
  } catch {
    return null;
  }
}
 
function readVerifiedEnvelope(storageKey: string, key: QueueKey): Envelope | null {
  Iif (typeof window === "undefined") return null;
  let raw: string | null = null;
  try {
    raw = window.localStorage.getItem(storageKey);
  } catch {
    return null;
  }
  const env = parseEnvelope(raw);
  if (env === null) return null;
  if (env.curatorId !== key.curatorId || env.sessionId !== key.sessionId) return null;
  return env;
}
 
function writeEnvelope(storageKey: string, env: Envelope): void {
  // Throws on quota — enqueue handles that; all other writers swallow below.
  window.localStorage.setItem(storageKey, JSON.stringify(env));
  try {
    window.dispatchEvent(new CustomEvent(PENDING_CHANGED_EVENT, { detail: storageKey }));
  } catch {
    // event construction can't realistically fail; never let it break a write
  }
}
 
export function readQueue(key: QueueKey): PendingEntry[] {
  return readVerifiedEnvelope(pendingStorageKey(key), key)?.entries ?? [];
}
 
export function readQueueCollectionId(key: QueueKey): string | undefined {
  const stored = readVerifiedEnvelope(pendingStorageKey(key), key)?.collectionId;
  return typeof stored === "string" ? stored : key.collectionId;
}
 
export type EnqueueResult = "persisted" | "needs_fallback";
 
export async function enqueue(
  key: QueueKey,
  entry: PendingEntry,
  onQuotaExceeded?: () => Promise<void>,
): Promise<EnqueueResult> {
  Iif (typeof window === "undefined") return "needs_fallback";
  const storageKey = pendingStorageKey(key);
  const build = (): Envelope => {
    const existing = readVerifiedEnvelope(storageKey, key);
    return {
      v: 1,
      curatorId: key.curatorId,
      sessionId: key.sessionId,
      collectionId: existing?.collectionId ?? key.collectionId,
      entries: [...(existing?.entries ?? []), entry],
    };
  };
  try {
    // Happy path is fully synchronous: persisted before this promise resolves.
    writeEnvelope(storageKey, build());
    return "persisted";
  } catch {
    // Quota (spec F18): shrink the queue via the injected flush, retry ONCE,
    // else the caller degrades to today's awaited direct submit.
    if (onQuotaExceeded) {
      try {
        await onQuotaExceeded();
        writeEnvelope(storageKey, build());
        return "persisted";
      } catch {
        return "needs_fallback";
      }
    }
    return "needs_fallback";
  }
}
 
function rewriteEntries(key: QueueKey, next: (entries: PendingEntry[]) => PendingEntry[]): boolean {
  Iif (typeof window === "undefined") return false;
  const storageKey = pendingStorageKey(key);
  const env = readVerifiedEnvelope(storageKey, key);
  if (env === null) return false;
  const entries = next(env.entries);
  if (entries.length === env.entries.length) return false;
  try {
    if (entries.length === 0) {
      window.localStorage.removeItem(storageKey);
    } else {
      writeEnvelope(storageKey, { ...env, entries });
    }
    return true;
  } catch {
    return false;
  }
}
 
export function removeEntries(key: QueueKey, clientKeys: string[]): void {
  const drop = new Set(clientKeys);
  rewriteEntries(key, (entries) => entries.filter((e) => !drop.has(e.clientKey)));
}
 
export function removeQueued(key: QueueKey, clientKey: string): boolean {
  return rewriteEntries(key, (entries) => entries.filter((e) => e.clientKey !== clientKey));
}
 
export function quarantineEntries(key: QueueKey, entries: PendingEntry[]): void {
  Iif (typeof window === "undefined" || entries.length === 0) return;
  const storageKey = quarantineStorageKey(key);
  const existing = readVerifiedEnvelope(storageKey, key)?.entries ?? [];
  const kept = [...existing, ...entries].slice(-QUARANTINE_CAP); // FIFO eviction, newest kept
  try {
    window.localStorage.setItem(
      storageKey,
      JSON.stringify({ v: 1, curatorId: key.curatorId, sessionId: key.sessionId, entries: kept }),
    );
  } catch {
    // quota on the quarantine shelf: these entries are terminal either way
  }
}
 
export function readQuarantine(key: QueueKey): PendingEntry[] {
  return readVerifiedEnvelope(quarantineStorageKey(key), key)?.entries ?? [];
}
 
function allStorageKeys(): string[] {
  const keys: string[] = [];
  try {
    for (let i = 0; i < window.localStorage.length; i++) {
      const k = window.localStorage.key(i);
      Eif (k !== null) keys.push(k);
    }
  } catch {
    // storage unavailable — nothing to list
  }
  return keys;
}
 
export function listPendingKeysForCurator(curatorId: string): QueueKey[] {
  Iif (typeof window === "undefined") return [];
  const prefix = `${PENDING_PREFIX}${curatorId}.`;
  const out: QueueKey[] = [];
  for (const storageKey of allStorageKeys()) {
    if (!storageKey.startsWith(prefix)) continue;
    const sessionId = storageKey.slice(prefix.length);
    const key: QueueKey = { curatorId, sessionId };
    const env = readVerifiedEnvelope(storageKey, key);
    Iif (env === null) continue; // corrupt/foreign — GC owns it, recovery ignores it
    out.push(typeof env.collectionId === "string" ? { ...key, collectionId: env.collectionId } : key);
  }
  return out;
}
 
export function deleteQueue(key: QueueKey): void {
  Iif (typeof window === "undefined") return;
  try {
    window.localStorage.removeItem(pendingStorageKey(key));
  } catch {
    // storage unavailable — nothing to delete
  }
}
 
// GC sweep (spec §3.4): every pending/quarantine key — including OTHER
// curators' and 403-stopped queues — whose NEWEST enqueuedAt is older than the
// TTL is deleted. Corrupt or empty blobs have no newest entry and sweep as
// expired (this is the promised eventual cleanup for blobs reads skip over).
export function sweepExpired(nowMs: number): void {
  Iif (typeof window === "undefined") return;
  const doomed: string[] = [];
  for (const storageKey of allStorageKeys()) {
    if (!storageKey.startsWith(PENDING_PREFIX) && !storageKey.startsWith(QUARANTINE_PREFIX)) continue;
    let newest = 0;
    let raw: string | null = null;
    try {
      raw = window.localStorage.getItem(storageKey);
    } catch {
      continue;
    }
    const env = parseEnvelope(raw);
    for (const entry of env?.entries ?? []) {
      const at = Date.parse((entry as { enqueuedAt?: string }).enqueuedAt ?? "");
      Eif (Number.isFinite(at) && at > newest) newest = at;
    }
    if (nowMs - newest > GC_TTL_MS) doomed.push(storageKey);
  }
  for (const storageKey of doomed) {
    try {
      window.localStorage.removeItem(storageKey);
    } catch {
      // best-effort
    }
  }
}