mirror of
https://github.com/tinyhumansai/openhuman.git
synced 2026-07-27 21:08:00 +00:00
Quiet core-state and rewards timeout diagnostics
## Summary
- Limits core-state bootstrap console warnings to the first failure plus the existing backoff suppression notice.
- Moves rewards page diagnostics from unconditional `console.debug` calls to the namespaced `debug` logger.
- Normalizes `/rewards/me` timeout/abort errors into a stable recoverable UI message.
- Adds focused coverage for rewards timeout handling and the quieter core-state warning contract.
## Problem
- #1235 reports noisy DevTools output during startup and rewards loading.
- Core-state polling already had a retry budget/backoff, but still emitted repeated bootstrap warnings.
- Rewards timeout failures surfaced raw transport messages and unconditional debug logs.
## Solution
- Keep detailed per-attempt core-state diagnostics behind the existing `debug('core-state')` logger while reducing default console warnings.
- Wrap rewards API failures with `normalizeRewardsApiError`, preserving backend errors while mapping timeout/abort failures to a retryable message.
- Keep rewards snapshot retries manual-only through the existing Try again button.
## Submission Checklist
- [x] Tests added or updated (happy path + at least one failure / edge case) per [Testing Strategy](../gitbooks/developing/testing-strategy.md#failure-path-requirement)
- [x] **Diff coverage ≥ 80%** — changed lines (Vitest + cargo-llvm-cov merged via `diff-cover`) meet the gate enforced by [`.github/workflows/coverage.yml`](../.github/workflows/coverage.yml). Run `pnpm test:coverage` and `pnpm test:rust` locally; PRs below 80% on changed lines will not merge.
- [x] Coverage matrix updated — N/A: behavior-only frontend diagnostics change, no feature row added/removed/renamed.
- [x] All affected feature IDs from the matrix are listed in the PR description under `## Related`
- [x] No new external network dependencies introduced (mock backend used per [Testing Strategy](../gitbooks/developing/testing-strategy.md#mock-policy))
- [x] Manual smoke checklist updated if this touches release-cut surfaces — N/A: no release-cut surface touched.
- [x] Linked issue closed via `Closes #NNN` in the `## Related` section
## Impact
- Desktop/web frontend console output is quieter during transient core/rewards failures.
- Users still see a recoverable rewards error state with the existing manual retry action.
- No API contract or persistence migration changes.
## Related
- Closes: #1235
- Follow-up PR(s)/TODOs: N/A
- Coverage matrix feature IDs: N/A: diagnostics-only change.
---
## AI Authored PR Metadata (required for Codex/Linear PRs)
### Linear Issue
- Key: N/A
- URL: N/A
### Commit & Branch
- Branch: codex/1235-quiet-core-rewards-timeouts
- Commit SHA: f531225b
### Validation Run
- [x] `pnpm --filter openhuman-app exec prettier --check src/providers/CoreStateProvider.tsx src/providers/__tests__/CoreStateProvider.test.tsx src/pages/Rewards.tsx src/services/api/rewardsApi.ts src/services/api/__tests__/rewardsApi.test.ts`
- [x] `pnpm --filter openhuman-app compile`
- [x] Focused tests: `pnpm --filter openhuman-app test -- src/services/api/__tests__/rewardsApi.test.ts src/providers/__tests__/CoreStateProvider.test.tsx src/pages/__tests__/Rewards.test.tsx`
- [x] `pnpm i18n:check`
- [x] `git diff --check`
- [x] Rust fmt/check (if changed): N/A, no Rust files changed.
- [x] Tauri fmt/check (if changed): N/A, no Tauri files changed.
### Validation Blocked
- `command:` N/A
- `error:` N/A
- `impact:` N/A
### Behavior Changes
- Intended behavior change: transient core-state failures no longer emit repeated default console warnings; rewards timeouts show a stable recoverable message.
- User-visible effect: quieter console and clearer Rewards retry state during backend/network slowness.
### Parity Contract
- Legacy behavior preserved: core-state still polls, backs off, and recovers; rewards still loads once and retries only on user action.
- Guard/fallback/dispatch parity checks: existing provider and rewards page tests updated.
### Duplicate / Superseded PR Handling
- Duplicate PR(s): N/A
- Canonical PR: N/A
- Resolution (closed/superseded/updated): N/A
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit
* **Bug Fixes**
* Polling warning refined to show only on the first bootstrap failure, reducing redundant alerts while preserving budget-exhaustion notices.
* **New Features**
* Rewards sync errors normalized so timeout/abort issues present a consistent retry message while preserving backend-provided error text for clarity.
* **Tests**
* Updated tests to align with the new warning and rewards-error behaviors.
<!-- review_stack_entry_start -->
[](https://app.coderabbit.ai/change-stack/tinyhumansai/openhuman/pull/2319?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)
<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Co-authored-by: aqilaziz <gonzes7@gmail.com>
Co-authored-by: Steven Enamakel <enamakel@tinyhumans.ai>
This commit is contained in:
co-authored by
Steven Enamakel
parent
21f4f9d8d2
commit
01a8227827
@@ -1,3 +1,4 @@
|
||||
import createDebug from 'debug';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
|
||||
import PillTabBar from '../components/PillTabBar';
|
||||
@@ -10,6 +11,8 @@ import type { RewardsSnapshot } from '../types/rewards';
|
||||
|
||||
type RewardsTab = 'referrals' | 'redeem' | 'rewards';
|
||||
|
||||
const log = createDebug('rewards');
|
||||
|
||||
function errorMessage(err: unknown): string {
|
||||
if (err && typeof err === 'object' && 'error' in err && typeof err.error === 'string') {
|
||||
return err.error;
|
||||
@@ -28,20 +31,21 @@ const Rewards = () => {
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const loadRewards = useCallback(async (signal?: { cancelled: boolean }) => {
|
||||
console.debug('[rewards] fetching snapshot');
|
||||
log('fetching snapshot');
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const result = await rewardsApi.getMyRewards();
|
||||
if (signal?.cancelled) return;
|
||||
setSnapshot(result);
|
||||
console.debug('[rewards] snapshot applied', {
|
||||
unlockedCount: result.summary.unlockedCount,
|
||||
totalCount: result.summary.totalCount,
|
||||
});
|
||||
log(
|
||||
'snapshot applied unlockedCount=%d totalCount=%d',
|
||||
result.summary.unlockedCount,
|
||||
result.summary.totalCount
|
||||
);
|
||||
} catch (err) {
|
||||
const message = errorMessage(err);
|
||||
console.debug('[rewards] snapshot load failed', message);
|
||||
log('snapshot load failed error=%s', message);
|
||||
if (signal?.cancelled) return;
|
||||
setSnapshot(null);
|
||||
setError(message);
|
||||
@@ -61,12 +65,12 @@ const Rewards = () => {
|
||||
}, [loadRewards]);
|
||||
|
||||
const handleTabChange = useCallback((next: RewardsTab) => {
|
||||
console.debug('[rewards] tab changed', { next });
|
||||
log('tab changed next=%s', next);
|
||||
setSelectedTab(next);
|
||||
}, []);
|
||||
|
||||
const handleRetry = useCallback(() => {
|
||||
console.debug('[rewards] retry requested');
|
||||
log('retry requested');
|
||||
void loadRewards();
|
||||
}, [loadRewards]);
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ export function coreStatePollFailureWarningMessage(failureCount: number): string
|
||||
if (failureCount <= 0) {
|
||||
return null;
|
||||
}
|
||||
if (failureCount <= MAX_BOOTSTRAP_RETRIES) {
|
||||
if (failureCount === 1) {
|
||||
return `[core-state] bootstrap poll failed (attempt ${failureCount}/${MAX_BOOTSTRAP_RETRIES}):`;
|
||||
}
|
||||
if (failureCount === SUPPRESS_POLL_WARNING_AT) {
|
||||
|
||||
@@ -685,14 +685,13 @@ describe('CoreStateProvider — identity-change cache clearing', () => {
|
||||
});
|
||||
|
||||
describe('coreStatePollFailureWarningMessage', () => {
|
||||
it('logs bounded bootstrap failures and one suppression notice', () => {
|
||||
it('warns once during bootstrap and once when warnings are suppressed', () => {
|
||||
expect(coreStatePollFailureWarningMessage(0)).toBeNull();
|
||||
expect(coreStatePollFailureWarningMessage(1)).toBe(
|
||||
'[core-state] bootstrap poll failed (attempt 1/5):'
|
||||
);
|
||||
expect(coreStatePollFailureWarningMessage(5)).toBe(
|
||||
'[core-state] bootstrap poll failed (attempt 5/5):'
|
||||
);
|
||||
expect(coreStatePollFailureWarningMessage(2)).toBeNull();
|
||||
expect(coreStatePollFailureWarningMessage(5)).toBeNull();
|
||||
expect(coreStatePollFailureWarningMessage(6)).toBe(
|
||||
'[core-state] bootstrap budget exhausted; continuing with backoff. Suppressing further warnings until recovery:'
|
||||
);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { normalizeRewardsSnapshot, rewardsApi } from '../rewardsApi';
|
||||
import { normalizeRewardsApiError, normalizeRewardsSnapshot, rewardsApi } from '../rewardsApi';
|
||||
|
||||
vi.mock('../../apiClient', () => ({ apiClient: { get: vi.fn() } }));
|
||||
|
||||
@@ -118,4 +118,50 @@ describe('rewardsApi', () => {
|
||||
error: 'Rewards service unavailable',
|
||||
});
|
||||
});
|
||||
|
||||
it('preserves backend application errors that contain "timeout" without remapping them', async () => {
|
||||
// A backend response like { success: false, error: 'Session timeout. Please log in again.' }
|
||||
// must reach the caller unchanged — it must NOT be replaced with the generic network-timeout
|
||||
// message, because it carries a real signal from the application layer.
|
||||
const { apiClient } = await import('../../apiClient');
|
||||
vi.mocked(apiClient.get).mockResolvedValueOnce({
|
||||
success: false,
|
||||
data: null,
|
||||
error: 'Session timeout. Please log in again.',
|
||||
});
|
||||
|
||||
await expect(rewardsApi.getMyRewards()).rejects.toMatchObject({
|
||||
success: false,
|
||||
error: 'Session timeout. Please log in again.',
|
||||
});
|
||||
});
|
||||
|
||||
it('normalizes /rewards/me timeouts into a recoverable message', async () => {
|
||||
const { apiClient } = await import('../../apiClient');
|
||||
vi.mocked(apiClient.get).mockRejectedValueOnce({
|
||||
success: false,
|
||||
error: 'Request timed out after 15s',
|
||||
});
|
||||
|
||||
await expect(rewardsApi.getMyRewards()).rejects.toMatchObject({
|
||||
success: false,
|
||||
error: 'Rewards sync timed out. Check your connection and try again.',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeRewardsApiError', () => {
|
||||
it('keeps useful backend errors intact', () => {
|
||||
expect(normalizeRewardsApiError({ error: 'Rewards service unavailable' })).toEqual({
|
||||
success: false,
|
||||
error: 'Rewards service unavailable',
|
||||
});
|
||||
});
|
||||
|
||||
it('maps abort-style timeout errors to a stable retry message', () => {
|
||||
expect(normalizeRewardsApiError(new DOMException('Aborted', 'AbortError'))).toEqual({
|
||||
success: false,
|
||||
error: 'Rewards sync timed out. Check your connection and try again.',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import type { ApiResponse } from '../../types/api';
|
||||
import createDebug from 'debug';
|
||||
|
||||
import type { ApiError, ApiResponse } from '../../types/api';
|
||||
import type { RewardsAchievement, RewardsSnapshot } from '../../types/rewards';
|
||||
import { apiClient } from '../apiClient';
|
||||
|
||||
const REWARDS_SNAPSHOT_TIMEOUT_MS = 15_000;
|
||||
const log = createDebug('rewards:api');
|
||||
|
||||
export type RewardsApiError = ApiError & { code?: string; status?: number };
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
return value && typeof value === 'object' && !Array.isArray(value)
|
||||
@@ -36,6 +41,43 @@ function asFiniteNumberOrNull(value: unknown): number | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
export function normalizeRewardsApiError(error: unknown): RewardsApiError {
|
||||
const raw = asRecord(error);
|
||||
const message =
|
||||
(typeof raw?.error === 'string' && raw.error) ||
|
||||
(typeof raw?.message === 'string' && raw.message) ||
|
||||
(error instanceof Error ? error.message : null) ||
|
||||
'Unable to load rewards';
|
||||
const code = typeof raw?.code === 'string' ? raw.code : undefined;
|
||||
const status = typeof raw?.status === 'number' ? raw.status : undefined;
|
||||
const name =
|
||||
(typeof raw?.name === 'string' && raw.name) ||
|
||||
(error instanceof Error ? error.name : undefined);
|
||||
const lowerMessage = message.toLowerCase();
|
||||
const isTimeout =
|
||||
lowerMessage.includes('timed out') ||
|
||||
lowerMessage.includes('timeout') ||
|
||||
code === 'ETIMEDOUT' ||
|
||||
code === 'ECONNABORTED' ||
|
||||
name === 'AbortError';
|
||||
|
||||
if (isTimeout) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Rewards sync timed out. Check your connection and try again.',
|
||||
...(code ? { code } : {}),
|
||||
...(status == null ? {} : { status }),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
error: message,
|
||||
...(code ? { code } : {}),
|
||||
...(status == null ? {} : { status }),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeAchievement(value: unknown): RewardsAchievement {
|
||||
const raw = asRecord(value) ?? {};
|
||||
const creditAmountUsd = asFiniteNumberOrNull(raw.creditAmountUsd);
|
||||
@@ -108,21 +150,44 @@ export function normalizeRewardsSnapshot(payload: unknown): RewardsSnapshot {
|
||||
|
||||
export const rewardsApi = {
|
||||
async getMyRewards(): Promise<RewardsSnapshot> {
|
||||
const response = await apiClient.get<ApiResponse<unknown>>('/rewards/me', {
|
||||
timeout: REWARDS_SNAPSHOT_TIMEOUT_MS,
|
||||
});
|
||||
let response: ApiResponse<unknown>;
|
||||
try {
|
||||
response = await apiClient.get<ApiResponse<unknown>>('/rewards/me', {
|
||||
timeout: REWARDS_SNAPSHOT_TIMEOUT_MS,
|
||||
});
|
||||
} catch (transportError) {
|
||||
// Transport-level failure (network error, timeout, abort) — normalize to
|
||||
// a stable retryable message. String-based timeout heuristics are only
|
||||
// safe here where the error comes from the HTTP layer, not from backend
|
||||
// application logic.
|
||||
const normalized = normalizeRewardsApiError(transportError);
|
||||
log(
|
||||
'snapshot transport failed error=%s code=%s status=%s',
|
||||
normalized.error,
|
||||
normalized.code ?? 'none',
|
||||
normalized.status ?? 'none'
|
||||
);
|
||||
throw normalized;
|
||||
}
|
||||
|
||||
if (!response.success) {
|
||||
throw {
|
||||
// Backend application error — preserve the exact message so callers see
|
||||
// the real signal (e.g. "Session timeout. Please log in again." must not
|
||||
// be remapped to the generic network-timeout message).
|
||||
const appError: RewardsApiError = {
|
||||
success: false,
|
||||
error: response.error ?? response.message ?? 'Unable to load rewards',
|
||||
};
|
||||
log('snapshot backend error error=%s', appError.error);
|
||||
throw appError;
|
||||
}
|
||||
|
||||
console.debug('[rewards] loaded backend snapshot', {
|
||||
achievementCount: Array.isArray((response.data as { achievements?: unknown[] })?.achievements)
|
||||
log(
|
||||
'loaded backend snapshot achievementCount=%d',
|
||||
Array.isArray((response.data as { achievements?: unknown[] })?.achievements)
|
||||
? (response.data as { achievements: unknown[] }).achievements.length
|
||||
: 0,
|
||||
});
|
||||
: 0
|
||||
);
|
||||
return normalizeRewardsSnapshot(response.data);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user