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(agent): expire fixed streaming rate buckets without renewal

zqchrisd3e3a2161e66
diff --git a/docs/dev-rules/response-speed.md b/docs/dev-rules/response-speed.md
index 87dc8a1bc9..fb9f7edb79 100644
--- a/docs/dev-rules/response-speed.md
+++ b/docs/dev-rules/response-speed.md
@@ -28,6 +28,7 @@ Pi 重试关闭失败片段,下一请求重新等待;每轮重新清空测
- 字符加权估计避免把一个传输片段当一个精确 token。它跨分片可加,但仍不是 tokenizer。
- 最近 1 秒滑窗,Pi/Claude 最多每 250ms 推进;Codex沿用 500ms 状态节流。
+ 窗口按固定 4ms 时间桶聚合,边缘最多提前裁掉不足 4ms 的估算量;旧桶不会因新输出续期。
窗口最多 256 个聚合点,图表最多 60 个采样点,不保留文本。
- 实时速率、峰值及未校准输出标「约」。真实 usage 到达后校准该响应的曲线及输出;
完成时显示真实输出除以已观测生成时间的最终平均。校准后的曲线和峰值仍是估计分配。
diff --git a/packages/maker-core/src/agents/shared/response-speed.test.ts b/packages/maker-core/src/agents/shared/response-speed.test.ts
index 0dc43634ec..787baa04b6 100644
--- a/packages/maker-core/src/agents/shared/response-speed.test.ts
+++ b/packages/maker-core/src/agents/shared/response-speed.test.ts
@@ -141,6 +141,31 @@ describe('response speed', () => {
expect(speed.snapshot(6_000)).toMatchObject({ outputTokens: 0, estimated: false, averageRate: 0 });
});
+ it('expires high-frequency output instead of renewing accumulated units', () => {
+ const speed = new ResponseSpeedTracker();
+ speed.reset('turn', 0);
+ speed.content(0);
+ for (let now = 1; now <= 10_000; now++) {
+ speed.delta('abcd', now); // 1 estimated token, 1000 deltas per second.
+ if (now === 1_000 || now === 5_000 || now === 10_000) {
+ expect(speed.snapshot(now).recentRate).toBeGreaterThanOrEqual(996);
+ expect(speed.snapshot(now).recentRate).toBeLessThanOrEqual(1000);
+ }
+ }
+ expect(speed.snapshot(10_000).samples.every(sample => sample.rate <= 1000)).toBe(true);
+ expect(responseSpeedHistory(speed.snapshot(10_000), 10_000).peak).toBeLessThanOrEqual(1000);
+ expect(speed.snapshot(11_000).recentRate).toBeNull();
+ speed.pause(11_000);
+ for (let now = 20_000; now <= 21_000; now++) speed.delta('abcd', now);
+ expect(speed.snapshot(21_000).recentRate).toBeLessThanOrEqual(1000);
+ expect(speed.snapshot(22_000).recentRate).toBeNull();
+ speed.finish(11_001, 22_000);
+ const done = speed.snapshot(22_000);
+ expect(done).toMatchObject({ estimated: false, outputTokens: 11_001,
+ durationMs: 13_000, averageRate: 11_001_000 / 13_000 });
+ expect(responseSpeedHistory(done, 22_000).peak).toBeLessThanOrEqual(1000);
+ });
+
it('character estimates are independent of chunk boundaries and samples stay bounded', () => {
const measure = (chunks: string[]) => {
const speed = new ResponseSpeedTracker();
diff --git a/packages/maker-core/src/agents/shared/response-speed.ts b/packages/maker-core/src/agents/shared/response-speed.ts
index 55782b203f..d11aba8d5e 100644
--- a/packages/maker-core/src/agents/shared/response-speed.ts
+++ b/packages/maker-core/src/agents/shared/response-speed.ts
@@ -1,6 +1,7 @@
import type { ResponseSpeedSnapshot, RateSample } from '@cindy/maker-shared/usage-format';
const WINDOW_MS = 1_000;
+const WINDOW_BUCKET_MS = 4;
const REFRESH_MS = 250;
const MAX_SAMPLES = 60;
@@ -77,7 +78,13 @@ export class ResponseSpeedTracker {
this.units += units;
this.reported = null;
this.lastDeltaAt = now;
- this.window.push({ time: now, units });
+ // Fixed bucket boundaries never move forward as new units arrive. At most
+ // four milliseconds of the oldest edge expire early; old output cannot
+ // keep renewing itself in a high-frequency stream.
+ const bucketAt = Math.floor(now / WINDOW_BUCKET_MS) * WINDOW_BUCKET_MS;
+ const bucket = this.window.at(-1);
+ if (bucket?.time === bucketAt) bucket.units += units;
+ else this.window.push({ time: bucketAt, units });
this.prune(now);
if (now - this.lastSampleAt < REFRESH_MS) return false;
this.lastSampleAt = now;
@@ -171,11 +178,9 @@ export class ResponseSpeedTracker {
}
private prune(now: number): void {
this.window = this.window.filter((point) => now - point.time < WINDOW_MS);
- // Bound storage even when a provider emits thousands of deltas in one millisecond.
- if (this.window.length > 256) {
- const first = this.window.splice(0, this.window.length - 255);
- this.window.unshift({ time: first.at(-1)!.time, units: first.reduce((sum, p) => sum + p.units, 0) });
- }
+ // A monotonic one-second window has at most 251 fixed buckets. Bound even
+ // malformed/backwards clocks without renewing discarded output's lifetime.
+ if (this.window.length > 256) this.window = this.window.slice(-256);
}
private recent(now: number): number | null {
if (!this.measurable || this.openAt === null || this.lastDeltaAt === null) return null;