When selecting a “preferred” item (via find, filter, pagination anchors, etc.) or deciding based on a computed threshold, ensure two things:
1) Compare like with like: normalize/parse inputs so the keys you compare are in the same canonical form.
2) Use explicit priority + fallback: run a primary search first; only if it yields nothing should you consult a fallback source. Avoid making fallback evidence primary.
Practical checklist:
authType:modelId), always decode/resolve to the same shape before comparing to stored objects.Code pattern (key normalization + prioritized fallback):
// 1) Normalize first (so comparisons match)
const normalized = isCompactionMode
? resolveModelId(settings?.merged?.compactionModel?.trim() ?? '')
: undefined;
// 2) Primary then fallback
const preferred = (() => {
// primary: use the most reliable/expected source(s)
const primary = normalized
? availableModelEntries.find(({ authType, model }) =>
authType &&
authType === normalized.authType &&
model.id === normalized.modelId
)
: undefined;
if (primary) return primary;
// fallback: weaker/secondary match only if primary failed
return availableModelEntries.find(({ model }) => model.id === normalized?.modelId);
})();
Apply this same approach to anchor selection and transcript pagination: prefer session_update-derived ids first; only if absent should you use the history_truncated marker id.