Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 修复 token 最近速度与峰值异常放大 (#4351)

Dashfa8e086811eb
diff --git a/apps/desktop/src/renderer/__tests__/runningTokenRateHistory.test.ts b/apps/desktop/src/renderer/__tests__/runningTokenRateHistory.test.ts
index 42da50a915..90e9e02e78 100644
--- a/apps/desktop/src/renderer/__tests__/runningTokenRateHistory.test.ts
+++ b/apps/desktop/src/renderer/__tests__/runningTokenRateHistory.test.ts
@@ -134,11 +134,11 @@ it('records final usage and appends the next turn on a continuous generation tim
const next = recordRunningTokenRate(newTurn, {
...input,
startedAt: 2,
- outputTokens: 20,
- generationDurationMs: 500,
+ outputTokens: 40,
+ generationDurationMs: 1000,
});
expect(next.samples.map((sample) => sample.rate)).toEqual([100, 50, 40]);
- expect(next.samples.map((sample) => sample.durationMs)).toEqual([1000, 2000, 2500]);
+ expect(next.samples.map((sample) => sample.durationMs)).toEqual([1000, 2000, 3000]);
expect(next.latestRate).toBe(40);
});
@@ -202,3 +202,123 @@ describe('per-session speed history cache', () => {
expect(loadCachedRateHistory('cap-20')).not.toBeNull();
});
});
+
+it('does not publish the 21 tokens / 1 ms spike, including at completion', () => {
+ const first = recordRunningTokenRate(emptyRateHistory(null), {
+ ...input,
+ outputTokens: 3879,
+ generationDurationMs: 102361,
+ });
+ const correction = recordRunningTokenRate(first, {
+ ...input,
+ outputTokens: 3900,
+ generationDurationMs: 102362,
+ });
+ expect(correction.samples).toEqual([]);
+ expect(correction.peak).toBe(0);
+ const terminal = recordRunningTokenRate(correction, {
+ ...input,
+ startedAt: null,
+ outputTokens: 3900,
+ generationDurationMs: 102362,
+ });
+ expect(terminal.samples).toEqual([]);
+ expect(terminal.peak).toBe(0);
+});
+
+it('accumulates frequent reports into a full window without losing tokens', () => {
+ let history = begin();
+ for (let n = 1; n < 10; n++) {
+ history = recordRunningTokenRate(history, {
+ ...input,
+ outputTokens: n * 10,
+ generationDurationMs: n * 100,
+ });
+ expect(history.samples).toEqual([]);
+ }
+ history = recordRunningTokenRate(history, {
+ ...input,
+ outputTokens: 100,
+ generationDurationMs: 1000,
+ });
+ expect(history.samples.map((sample) => sample.rate)).toEqual([100]);
+ const pending = recordRunningTokenRate(history, {
+ ...input,
+ outputTokens: 121,
+ generationDurationMs: 1001,
+ });
+ expect(pending.samples).toEqual(history.samples);
+ expect(pending.peak).toBe(100);
+ const next = recordRunningTokenRate(pending, {
+ ...input,
+ outputTokens: 150,
+ generationDurationMs: 2000,
+ });
+ expect(next.samples.map((sample) => sample.rate)).toEqual([100, 50]);
+ expect(next.peak).toBe(100);
+});
+
+it('does not consume generation time before the matching output arrives', () => {
+ const first = recordRunningTokenRate(begin(), {
+ ...input,
+ outputTokens: 100,
+ generationDurationMs: 1000,
+ });
+ const timeOnly = recordRunningTokenRate(first, {
+ ...input,
+ outputTokens: 100,
+ generationDurationMs: 2999,
+ });
+ expect(timeOnly.samples).toEqual(first.samples);
+ const output = recordRunningTokenRate(timeOnly, {
+ ...input,
+ outputTokens: 200,
+ generationDurationMs: 3000,
+ });
+ expect(output.samples.map((sample) => sample.rate)).toEqual([100, 50]);
+ expect(output.peak).toBe(100);
+});
+
+it('detects a counter rollback within an unfinished sampling window', () => {
+ const pending = recordRunningTokenRate(begin(), {
+ ...input,
+ outputTokens: 90,
+ generationDurationMs: 900,
+ });
+ const reset = recordRunningTokenRate(pending, {
+ ...input,
+ outputTokens: 50,
+ generationDurationMs: 500,
+ });
+ expect(reset.samples).toEqual([]);
+ const next = recordRunningTokenRate(reset, {
+ ...input,
+ outputTokens: 100,
+ generationDurationMs: 1500,
+ });
+ expect(next.samples.map((sample) => sample.rate)).toEqual([50]);
+});
+
+it('re-establishes the same report after reliability returns and preserves completed history', () => {
+ const report = { ...input, outputTokens: 100, generationDurationMs: 1000 };
+ const active = recordRunningTokenRate(begin(), report);
+ const hidden = recordRunningTokenRate(active, { ...report, generationReliable: false });
+ expect(hidden.lastReport).toBeNull();
+ const resumed = recordRunningTokenRate(hidden, report);
+ expect(resumed.baseline).toEqual(active.baseline);
+ const short = recordRunningTokenRate(resumed, {
+ ...report,
+ outputTokens: 121,
+ generationDurationMs: 1001,
+ });
+ expect(short.latestRate).toBeNull();
+ expect(short.samples).toEqual(active.samples);
+ expect(short.peak).toBe(100);
+ const next = recordRunningTokenRate(short, {
+ ...report,
+ outputTokens: 150,
+ generationDurationMs: 2000,
+ });
+ expect(next.latestRate).toBe(50);
+ expect(next.samples.map((sample) => sample.durationMs)).toEqual([1000, 2000]);
+});
diff --git a/apps/desktop/src/renderer/__tests__/runningTokenRatePopover.test.tsx b/apps/desktop/src/renderer/__tests__/runningTokenRatePopover.test.tsx
index 044ddf5a72..9a1652e79d 100644
--- a/apps/desktop/src/renderer/__tests__/runningTokenRatePopover.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/runningTokenRatePopover.test.tsx
@@ -55,6 +55,7 @@ it('keeps pinned history while its trigger falls back to tokens or elapsed-only'
history: {
startedAt: 1,
baseline: null,
+ lastReport: null,
peak: 100,
latestRate: 100,
samples: [{ durationMs: 1000, outputTokens: 100, rate: 100 }],
@@ -96,6 +97,7 @@ it('pins the card on click and dismisses with Escape, returning focus to the spe
history={{
startedAt: 1,
baseline: { durationMs: 10000, outputTokens: 1000 },
+ lastReport: { durationMs: 10000, outputTokens: 1000 },
peak: 120,
latestRate: 100,
samples: [
@@ -128,7 +130,14 @@ it.each(['Escape', 'close'] as const)(
rateText="100 tok/s"
averageRate="110"
outputTokens={1000}
- history={{ startedAt: 1, baseline: null, peak: 100, latestRate: null, samples: [] }}
+ history={{
+ startedAt: 1,
+ baseline: null,
+ lastReport: null,
+ peak: 100,
+ latestRate: null,
+ samples: [],
+ }}
/>,
);
const trigger = screen.getByRole('button');
@@ -200,6 +209,9 @@ it('keeps the pinned card across turns while awaiting a fresh rate', async () =>
expect(screen.getByRole('dialog').textContent).toContain('—');
const previousLine = chart.querySelectorAll('path')[2].getAttribute('d');
rerender(<Harness startedAt={2} outputTokens={20} generationDurationMs={500} />);
+ expect(chart.querySelectorAll('path')[2].getAttribute('d')).toBe(previousLine);
+ expect(screen.getByRole('dialog').textContent).toContain('—');
+ rerender(<Harness startedAt={2} outputTokens={40} generationDurationMs={1000} />);
expect(screen.getByRole('dialog').textContent).toContain('40');
const nextLine = screen.getByRole('img').querySelectorAll('path')[2].getAttribute('d');
expect(nextLine).not.toBe(previousLine);
@@ -217,7 +229,14 @@ it('keeps a clicked panel open through outside clicks, focus changes and repeate
rateText="100 tok/s"
averageRate="110"
outputTokens={1000}
- history={{ startedAt: 1, baseline: null, peak: 0, latestRate: null, samples: [] }}
+ history={{
+ startedAt: 1,
+ baseline: null,
+ lastReport: null,
+ peak: 0,
+ latestRate: null,
+ samples: [],
+ }}
onPinnedChange={onPinnedChange}
/>
</>,
@@ -294,61 +313,72 @@ it('restores speed history from the per-session cache after remounting', () => {
second.unmount();
// 另一个会话没有历史:从零等待,不串会话。
- const third = render(
- <Harness sessionKey="cache-b" startedAt={1} outputTokens={0} generationDurationMs={0} />,
- );
+ render(<Harness sessionKey="cache-b" startedAt={1} outputTokens={0} generationDurationMs={0} />);
fireEvent.click(screen.getByRole('button'));
expect(screen.getByRole('img').querySelectorAll('path')).toHaveLength(1);
expect(screen.getByRole('dialog').textContent).toContain('—');
});
-it('does not fabricate a rate when restoring an idle session whose next turn finished while away', () => {
- clearRateHistoryCache();
- function Harness({
- sessionKey,
- startedAt,
- outputTokens,
- generationDurationMs,
- }: {
- sessionKey: string | null;
- startedAt: number | null;
- outputTokens: number;
- generationDurationMs: number;
- }) {
- const history = useRunningTokenRateHistory({
+it.each([
+ [500, 10000],
+ [50, 500],
+ [50, 10000],
+ [500, 500],
+])(
+ 'preserves the last measured rate on idle restore with %i tokens / %i ms',
+ (outputTokens, generationDurationMs) => {
+ clearRateHistoryCache();
+ function Harness({
sessionKey,
startedAt,
outputTokens,
generationDurationMs,
- generationReliable: true,
- });
- return (
- <RunningTokenRatePopover
- elapsedText="10s"
- rate={history.latestRate === null ? null : String(history.latestRate)}
- rateText="speed"
- averageRate={null}
- outputTokens={outputTokens}
- history={history}
- />
+ }: {
+ sessionKey: string | null;
+ startedAt: number | null;
+ outputTokens: number;
+ generationDurationMs: number;
+ }) {
+ const history = useRunningTokenRateHistory({
+ sessionKey,
+ startedAt,
+ outputTokens,
+ generationDurationMs,
+ generationReliable: true,
+ });
+ return (
+ <RunningTokenRatePopover
+ elapsedText="10s"
+ rate={history.latestRate === null ? null : String(history.latestRate)}
+ rateText="speed"
+ averageRate={null}
+ outputTokens={outputTokens}
+ history={history}
+ />
+ );
+ }
+ const first = render(
+ <Harness sessionKey="idle-a" startedAt={1} outputTokens={0} generationDurationMs={0} />,
);
- }
- const first = render(
- <Harness sessionKey="idle-a" startedAt={1} outputTokens={0} generationDurationMs={0} />,
- );
- first.rerender(
- <Harness sessionKey="idle-a" startedAt={1} outputTokens={100} generationDurationMs={1000} />,
- );
- first.unmount();
+ first.rerender(
+ <Harness sessionKey="idle-a" startedAt={1} outputTokens={100} generationDurationMs={1000} />,
+ );
+ first.unmount();
- // 切离期间会话在后台跑完新一轮;切回时已空闲,store 里是新轮的累计计数。
- const second = render(
- <Harness sessionKey="idle-a" startedAt={null} outputTokens={500} generationDurationMs={10000} />,
- );
- fireEvent.click(screen.getByRole('button'));
- // 新轮计数减旧轮 baseline 得出的 44.4 tok/s 是伪造区间,不得追加;
- // 图表仍只有缓存里的 1 个采样点(仅轴线),最新速度保持最后的真实测量。
- expect(screen.getByRole('img').querySelectorAll('path')).toHaveLength(1);
- expect(screen.getByRole('dialog').textContent).not.toContain('44.4');
- expect(screen.getByRole('dialog').textContent).toContain('100');
-});
+ // 切离期间会话在后台跑完新一轮;切回时已空闲,store 里是新轮的累计计数。
+ render(
+ <Harness
+ sessionKey="idle-a"
+ startedAt={null}
+ outputTokens={outputTokens}
+ generationDurationMs={generationDurationMs}
+ />,
+ );
+ fireEvent.click(screen.getByRole('button'));
+ // 无论新轮计数增大还是减小,都不能与旧轮比较或追加伪造区间;
+ // 图表仍只有缓存里的 1 个采样点(仅轴线),最新速度保持最后的真实测量。
+ expect(screen.getByRole('img').querySelectorAll('path')).toHaveLength(1);
+ expect(screen.getByRole('dialog').textContent).not.toContain('44.4');
+ expect(screen.getByText('100', { selector: 'span' })).toBeTruthy();
+ },
+);
diff --git a/apps/desktop/src/renderer/features/cc-agent/RunningTokenRatePopover.tsx b/apps/desktop/src/renderer/features/cc-agent/RunningTokenRatePopover.tsx
index 70c9604e53..c001b7ea44 100644
--- a/apps/desktop/src/renderer/features/cc-agent/RunningTokenRatePopover.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/RunningTokenRatePopover.tsx
@@ -26,9 +26,9 @@ export function useRunningTokenRateHistory(input: {
const [history, setHistory] = useState<RateHistory>(() => {
const cached = sessionKey ? loadCachedRateHistory(sessionKey) : null;
if (!cached) return emptyRateHistory(null);
- // 空闲态恢复时必须丢弃 baseline:此时看不到计数属于哪一轮,若会话在
- // 后台跑完了新一轮,用旧轮 baseline 去减新轮累计计数会伪造区间速度。
- return startedAt === null ? { ...cached, baseline: null } : cached;
+ // 空闲态恢复时丢弃两个计数起点:无法判断计数属于哪一轮,既不能
+ // 用旧 baseline 计算区间,也不能用旧 lastReport 判定当前轮计数回退。
+ return startedAt === null ? { ...cached, baseline: null, lastReport: null } : cached;
});
useEffect(() => {
setHistory((previous) =>
diff --git a/apps/desktop/src/renderer/features/cc-agent/lib/runningTokenRateHistory.ts b/apps/desktop/src/renderer/features/cc-agent/lib/runningTokenRateHistory.ts
index 6351e26a7f..2b40c5f4c7 100644
--- a/apps/desktop/src/renderer/features/cc-agent/lib/runningTokenRateHistory.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/lib/runningTokenRateHistory.ts
@@ -1,20 +1,29 @@
-export interface RateSample {
- /** Cumulative measured generation time across turns, excluding unmeasured gaps. */
+interface RateCounters {
durationMs: number;
outputTokens: number;
+}
+
+export interface RateSample extends RateCounters {
+ /** Cumulative measured generation time across turns, excluding unmeasured gaps. */
+ durationMs: number;
rate: number;
}
+// Millisecond-scale usage batches are not meaningful throughput measurements.
+const MIN_SAMPLE_DURATION_MS = 1000;
+
export interface RateHistory {
startedAt: number | null;
- baseline: { durationMs: number; outputTokens: number } | null;
+ baseline: RateCounters | null;
+ // Track resets even while the measurement baseline waits for a full window.
+ lastReport: RateCounters | null;
samples: RateSample[];
peak: number;
latestRate: number | null;
}
export function emptyRateHistory(startedAt: number | null): RateHistory {
- return { startedAt, baseline: null, samples: [], peak: 0, latestRate: null };
+ return { startedAt, baseline: null, lastReport: null, samples: [], peak: 0, latestRate: null };
}
const MAX_RATE_SAMPLES = 60;
@@ -33,13 +42,15 @@ export function recordRunningTokenRate(
// Terminal status clears startedAt before the status bar finishes its linger/fade.
// Keep its identity so a final paired usage report can still be recorded.
const startedAt = input.startedAt ?? history.startedAt;
- const previous = history.baseline;
+ const previous = history.lastReport;
const reset =
history.startedAt !== startedAt ||
(previous !== null &&
(generationDurationMs < previous.durationMs || outputTokens < previous.outputTokens));
// Reset only the measurement baseline; completed intervals remain in the chart.
- const current = reset ? { ...history, startedAt, baseline: null, latestRate: null } : history;
+ const current = reset
+ ? { ...history, startedAt, baseline: null, lastReport: null, latestRate: null }
+ : history;
if (
!generationReliable ||
startedAt === null ||
@@ -48,24 +59,38 @@ export function recordRunningTokenRate(
outputTokens < 0 ||
generationDurationMs < 0
) {
- return current.baseline || current.latestRate !== null
- ? { ...current, baseline: null, latestRate: null }
+ return current.baseline || current.lastReport || current.latestRate !== null
+ ? { ...current, baseline: null, lastReport: null, latestRate: null }
: current;
}
+ if (
+ previous?.durationMs === generationDurationMs &&
+ previous.outputTokens === outputTokens &&
+ !reset
+ ) {
+ return current;
+ }
const baseline = { durationMs: generationDurationMs, outputTokens };
+ const observed = { ...current, lastReport: baseline };
if (!current.baseline) {
// Opening midway through a turn must not label its cumulative average as a recent sample.
- return { ...current, baseline };
+ return { ...observed, baseline };
}
const durationDelta = generationDurationMs - current.baseline.durationMs;
const tokenDelta = outputTokens - current.baseline.outputTokens;
- if (durationDelta === 0 && tokenDelta === 0) return current;
+ // A time-only refresh cannot close a token interval: the matching usage may
+ // arrive later in a batch. Keep both counters anchored to the last sample.
+ // An explicitly empty output stream can still measure zero throughput.
+ if (outputTokens > 0 && outputTokens === previous?.outputTokens) return observed;
if (durationDelta === 0) {
// A corrected count without a matching time cannot produce a rate.
- return { ...current, baseline };
+ return { ...observed, baseline };
}
+ // Keep accumulating both counters, including at completion. An unfinished
+ // window must not replace the last valid rate or inflate the observed peak.
+ if (durationDelta < MIN_SAMPLE_DURATION_MS) return observed;
const rate = (tokenDelta * 1000) / durationDelta;
- if (!Number.isFinite(rate)) return { ...current, baseline };
+ if (!Number.isFinite(rate)) return { ...observed, baseline };
const samples = [
...current.samples.slice(-(MAX_RATE_SAMPLES - 1)),
{
@@ -75,7 +100,7 @@ export function recordRunningTokenRate(
},
];
return {
- startedAt,
+ ...observed,
baseline,
samples,
peak: Math.max(...samples.map((sample) => sample.rate)),