fix(persistence): address PR review feedback (4 low-cost improvements)

From PR #114 external review agent — adopted the real, low-cost findings;
remaining items (false positives / design trade-offs) explained in PR replies:

- coerceEpoch: !Number.isNaN → Number.isFinite — reject ±Infinity, which
  previously slipped through and produced Invalid Date via new Date(Infinity)
- enforceRetentionCap pass2/pass3: decrement overflow only when idbDelete
  actually succeeds — a failed best-effort delete no longer under-evicts
- cloudListStories: explicit column list instead of select() — avoid pulling
  the bulky session_jsonb when only metadata is needed
- Supabase stories: composite primary key (user_id, id) + onConflict user_id,id
  — avoid a cross-user Session.id collision rejecting the second user's save
  (skeleton not yet deployed, so the migration is edited in place)

typecheck + build:cf green.
This commit is contained in:
Kai ki
2026-06-25 19:20:55 +08:00
parent 610dba78b7
commit da74e3e763
4 changed files with 19 additions and 9 deletions
+3 -1
View File
@@ -19,7 +19,9 @@ export const STORY_SCHEMA_VERSION = 1;
* crosses a storage/serialization boundary and could arrive as a non-number,
* guarding against the historical `t.getTime is not a function` white-screen. */
export function coerceEpoch(value: unknown, fallback: number): number {
if (typeof value === "number" && !Number.isNaN(value)) return value;
// Number.isFinite (not just !isNaN) so ±Infinity also falls through to the
// fallback — new Date(Infinity).getTime() is NaN, not a usable epoch.
if (typeof value === "number" && Number.isFinite(value)) return value;
const d = value instanceof Date ? value : new Date(value as string | number);
const t = d.getTime();
return Number.isNaN(t) ? fallback : t;