Skip to content

fix(ui): canonicalize restored content tabs - #1047

Merged
joelteply merged 1 commit into
canaryfrom
codex/fix-stale-content-tabs-canary
May 7, 2026
Merged

joelteply merged 1 commit into
canaryfrom
codex/fix-stale-content-tabs-canary

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Summary

  • validate restored entity-backed content tabs against the current database before rendering
  • canonicalize stale room tabs by resolving uniqueId through RoutingService, so old UUIDs like the bogus General tab recover to the real room entity
  • dedupe equivalent restored surfaces across content recipes that render the same room/chat view
  • remap currentItemId when the focused tab was a dropped stale duplicate

Why

A reseed/schema change can leave user_states.content_state with a stale General tab whose old UUID no longer exists. The UI then restores a bogus tab (Loading members..., old messages) next to the real General room. This fixes the persisted-state recovery path instead of requiring localStorage/manual DB cleanup.

Validation

  • npm run build:ts
  • pre-push gate: TypeScript clean, ESLint baseline-tolerant pass
  • local fixture: persisted good chat/general + stale academy-training/general collapses to one canonical General tab and preserves focus

@joelteply joelteply left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quality fix overall — generic RoutingService.resolve replaces brittle hardcoded literal-uniqueId match, canonical-path dedup is the right shape, focus-remap handled. Two specific concerns worth weighing before merge:

(A) catch path is destructive on DB transient (CORRECTNESS). try { resolved = await RoutingService.resolve(...) } catch (error) { console.warn(...) } leaves resolved = null, then the next branch if (!resolved) { dropping stale persisted tab }. Net effect: if RoutingService throws (DB briefly unavailable, IPC socket temporarily down on startup), every persisted tab gets DROPPED and the warn log is the only surviving evidence. Suggest: distinguish 'resolved=null definitively' (entity gone — drop) from 'threw' (couldn't validate — KEEP the tab as-is, log warn). Otherwise a transient startup race silently destroys the user's tab state.

(B) O(N) sequential DB calls (PERF). for (const item of openItems) { await RoutingService.resolve(...) } is sequential. With ~10-20 persisted tabs across rooms+games+wikis the user-perceived restore-startup time grows linearly with tab count, and this runs both at initializeContentTabs AND inside the onStateChange debounce (1000ms throttled but still fires on every userState change). Easy parallelization via Promise.all(openItems.map(...)) — order preservation maintained because dedup happens after, and RoutingService.resolve is per-entity-independent.

(meta) Task #90 in continuum land — '3-way room-set drift, collapse onto ROOM_UNIQUE_IDS as SSOT' remains pending. This PR is the right pragmatic-fix-on-the-read-side, but the underlying drift (multiple sources of truth for what 'general' even means) is still a foundation crack. Worth pinning #90 explicitly in this PR's body or linking issue so it doesn't fall off.

@joelteply

Copy link
Copy Markdown
Contributor Author

LGTM with two evidence-honesty flags worth addressing.

Architecture: clean

Strong fix — removes the hardcoded isLegacyGeneral string-match hack and replaces with general-purpose RoutingService.resolve() validation. Three nice moves:

  • isSameContentSurface helper (UserStateEntity.ts:30) — generalizes the dedupe rule: same entityType + same view = same surface, even if the type strings differ. Previous logic only matched on exact type, which would miss e.g. chat vs chat-academy-training aliases pointing at the same room.
  • resolveCurrentItemId (ContentStateService.ts:121) — when current is deduped-out, maps to canonical equivalent via contentItemsMatch, falls back to first item. Closes the dead-pointer race cleanly.
  • sanitizePersistedContentItems rewrite (MainWidget.ts:528) — RoutingService.resolve drops stale entity refs, canonicalizes entityId/uniqueId/title from current DB, dedupes by buildContentPath(), remaps current to canonical. Net effect for the bug: stale academy-training/general (07a5ba59) → resolved as chat/general (2789ca42) → deduped against existing chat/general → currentItemId remapped → one canonical General tab.

Concerns

1. Resolve-error swallowing (HONESTY). Lines ~537-547 of MainWidget — RoutingService.resolve throws are caught with console.warn then the tab gets dropped as "stale". This conflates two cases that should be handled differently: (a) resolved === null (entity genuinely gone, drop is correct) vs (b) resolve threw (transient network/IPC failure, dropping a legitimate tab is wrong — better to keep the tab and retry next sync). Suggested: only drop on null, on throw keep the original item with a lastValidationError marker.

2. Async-error fire-and-forget (EVIDENCE). Line 87: onStateChange: () => offMainThread(() => { void this.syncUserStateToContentState(); }, 1000). The void is required because the wrapper is sync, but it means any async failure inside syncUserStateToContentState (e.g. RoutingService throws across all items) goes nowhere. Same anti-pattern Joel keeps reinforcing across airc PRs (#527, #538, #552). Suggested: .catch(err => console.error('syncUserStateToContentState failed:', err)) so it surfaces in the browser console at minimum.

Minor

  • Two dedupe rules now coexist: isSameContentSurface (entityType+view) in UserStateEntity, buildContentPath in MainWidget. Probably fine for now since they should agree, but could drift. Worth a follow-up to unify or document why they differ.
  • sanitizePersistedContentItems dedupe loop is O(N²) — fine for typical <20 open tabs, just noting.
  • Old sanitizePersistedContentItems was sync; new one is async. All callers updated correctly.

LGTM modulo (1) — that one's a real correctness gap. (2) is a polish/diagnostic concern. Both can be follow-up commits if you want to ship as-is.

@joelteply
joelteply force-pushed the codex/fix-stale-content-tabs-canary branch from bd851ab to 09483d7 Compare May 7, 2026 16:14
@joelteply
joelteply force-pushed the codex/fix-stale-content-tabs-canary branch from 09483d7 to f83fb13 Compare May 7, 2026 16:16
@joelteply

Copy link
Copy Markdown
Contributor Author

Updated again at f83fb13 to cover the remaining diagnostic concern too.\n\nResolved review items:\n- RoutingService.resolve throws now keep the original persisted tab and log the validation failure; only a definitive unresolved/null result drops a stale tab.\n- Restored entity-tab validation now runs via Promise.all, preserving result order while avoiding sequential DB/IPC latency.\n- The onStateChange fire-and-forget sync now attaches a catch handler so async failures surface in the browser console instead of disappearing.\n\nValidation after the fix: npm run build:ts, precommit browser-ping, and pre-push gate all passed.

@joelteply
joelteply merged commit 18247fe into canary May 7, 2026
3 checks passed
@joelteply
joelteply deleted the codex/fix-stale-content-tabs-canary branch May 7, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant