fix: time on another tab no longer counts toward the report wait budget (BUG-1941)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
This commit is contained in:
2026-11-16 22:35:31 +08:00
co-authored by Claude Fable 5.2
parent 64ca04ec2a
commit 421addbc9d
3 changed files with 46 additions and 1 deletions
+12
View File
@@ -26117,3 +26117,15 @@
- 验证与防复发:`frontend/tests/admin-clarity-20261116.test.ts` 新增一条:新版本优先于旧版本和内置标价,未保存的已知型号取标价,无价格的不带出,「用过的模型」去重并把在用的排前。未在真实后台上操作过。内置标价是手工录入的,供应商调价后需要更新。
- 关联记录:BUG-1937、BUG-1922。
- 修复版本:`claude/model-price-presets-1940`,待 staging 实测。
## BUG-1941 | 报告生成中切到别的标签页,回来显示「生成时间超出预期」,已等待时间把离开的时间也算进去
- 状态:fixed-pending-verify(待 staging 实测)。
- 首次发现 / 最近更新:2026-11-16 / 2026-11-16。
- 触发与现象:产品负责人点生成报告后切到别的标签页,过一阵回来,页面显示「生成时间超出预期,已等待 35 分 42 秒,页面已暂停自动刷新」,要再等一会儿或点「继续等待」才看到报告。
- 触发条件:报告生成中,标签页在后台停留超过等待上限(8 分钟)。
- 根因:等待时间按「现在减去开始等待的时刻」算,标签页在后台时轮询和计时都停了,但这段时间仍被算进已等待时间。回到页面的第一次计时就超过上限,页面进入超时状态并停止轮询,即使报告正常在生成。
- 修复:`personal-report-page.tsx` 的计时发现两次计时间隔远大于一秒(超过 5 秒,说明页面曾在后台),就把这段间隔从已等待时间里扣掉;回到页面后继续显示生成进度并继续轮询,报告好了直接显示。
- 验证与防复发:`frontend/tests/report-polling-contract.test.ts` 新增一条,覆盖首次计时、正常一秒间隔、三十分钟离开三种情况,并钉住扣除逻辑。未在真实浏览器里复现过切换标签页。
- 关联记录:无。
- 修复版本:`claude/synastry-polish-20261116`,待 staging 实测。
@@ -297,6 +297,15 @@ export function pollIntervalForElapsed(elapsedMs: number): number {
return 15_000;
}
export const AWAY_GAP_MS = 5000;
/** Time since the last tick when it is long enough to mean the tab was hidden; otherwise 0. */
export function awayGapMs(lastTickAt: number | null, now: number): number {
if (lastTickAt === null) return 0;
const gap = now - lastTickAt;
return gap > AWAY_GAP_MS ? gap : 0;
}
export function formatWaitedDuration(elapsedMs: number): string {
const totalSeconds = Math.max(0, Math.floor(elapsedMs / 1000));
const minutes = Math.floor(totalSeconds / 60);
@@ -403,11 +412,23 @@ export function PersonalReportPage({
void load();
}, [load]);
// BUG-1941: the wait budget counts only time this tab was actually open.
// Ticks stop while the tab is hidden, so a gap much longer than one tick
// means the user was away; that gap is moved out of the waited time.
const lastTickAtRef = useRef<number | null>(null);
const tick = useCallback(() => {
if (waitStartedAt === null) {
lastTickAtRef.current = null;
return;
}
const elapsed = Date.now() - waitStartedAt;
const now = Date.now();
const away = awayGapMs(lastTickAtRef.current, now);
lastTickAtRef.current = now;
if (away > 0) {
setWaitStartedAt((startedAt) => (startedAt === null ? startedAt : startedAt + away));
return;
}
const elapsed = now - waitStartedAt;
setWaitedMs(elapsed);
if (elapsed >= POLL_BUDGET_MS) {
setState((current) => (current.phase === "generating" ? { phase: "timed-out" } : current));
@@ -3,6 +3,8 @@ import { readFileSync } from "node:fs";
import test from "node:test";
import {
AWAY_GAP_MS,
awayGapMs,
formatWaitedDuration,
POLL_BUDGET_MS,
pollIntervalForElapsed,
@@ -155,3 +157,13 @@ test("cancellation guards survive the rewrite and no poll runs outside the gener
assert.match(centerSource, /cancelled\.current = true;/);
assert.match(pageSource, /const generating = state\.phase === "generating";/);
});
test("BUG-1941: time spent on another tab is not counted toward the wait budget", () => {
assert.equal(awayGapMs(null, 1_000_000), 0, "the first tick has nothing to compare");
assert.equal(awayGapMs(1_000_000, 1_001_000), 0, "a normal one-second tick is waiting time");
assert.equal(awayGapMs(1_000_000, 1_000_000 + AWAY_GAP_MS), 0);
const thirtyMinutes = 30 * 60 * 1000;
assert.equal(awayGapMs(1_000_000, 1_000_000 + thirtyMinutes), thirtyMinutes);
assert.match(pageSource, /const away = awayGapMs\(lastTickAtRef\.current, now\);/);
assert.match(pageSource, /startedAt \+ away/, "the away gap moves the wait start forward");
});