Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(usage): withdraw live tok/s when the host was suspended mid-generation
Chris905bb92cf53d
diff --git a/apps/desktop/src/renderer/__tests__/makerChatStoreLiveGeneration.test.ts b/apps/desktop/src/renderer/__tests__/makerChatStoreLiveGeneration.test.tsindex 57467885fd..a0ed9cb136 100644--- a/apps/desktop/src/renderer/__tests__/makerChatStoreLiveGeneration.test.ts+++ b/apps/desktop/src/renderer/__tests__/makerChatStoreLiveGeneration.test.ts@@ -94,6 +94,9 @@ describe('makerChatStore live generation at turn start', () => {expect(makerChatStore.getSnapshot(sessionId).agentStatus.tokenSpeed).toEqual(tokenSpeed);applyStatus(sessionId, { isRunning: true, tokenSpeed: { rate: 'fast' } });expect(makerChatStore.getSnapshot(sessionId).agentStatus.tokenSpeed).toEqual(tokenSpeed);+ // The host withdrew the speed (suspended mid-generation): the stale reading goes too.+ applyStatus(sessionId, { isRunning: true, generationReliable: false });+ expect(makerChatStore.getSnapshot(sessionId).agentStatus.tokenSpeed).toBeUndefined();applyStatus(sessionId, { isRunning: false, status: 'Done' });applyStatus(sessionId, { isRunning: true, status: 'Working' });expect(makerChatStore.getSnapshot(sessionId).agentStatus.tokenSpeed).toBeUndefined();diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.tsindex eeeb1b9291..81861fb2a3 100644--- a/apps/desktop/src/renderer/lib/makerChatStore.ts+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts@@ -7052,8 +7052,9 @@ function mergeLiveGenerationStatus(> {// Speed belongs to the turn that produced it: a new turn drops the old value,// frames without the field keep the latest one (including the final average).+ // An unreliable frame without it means the host withdrew the speed (suspend).const tokenSpeed = readTokenSpeedSnapshot(update.tokenSpeed)- ?? (isTurnStart ? undefined : previous.tokenSpeed);+ ?? (isTurnStart || update.generationReliable === false ? undefined : previous.tokenSpeed);// Turn start drops leftover metrics from the previous turn, then keeps any// live fields carried by this same status. A reconnect-shaped first event// (isRunning false→true with output / duration already present) must notdiff --git a/apps/mobile/src/__tests__/remoteSessionStore.test.ts b/apps/mobile/src/__tests__/remoteSessionStore.test.tsindex 9e55ff4bfb..dd341fd75b 100644--- a/apps/mobile/src/__tests__/remoteSessionStore.test.ts+++ b/apps/mobile/src/__tests__/remoteSessionStore.test.ts@@ -3300,6 +3300,9 @@ describe('remoteSessionStore', () => {pushMakerStatus('s1', { isRunning: true, status: 'Generating...', tokenSpeed });pushMakerStatus('s1', { isRunning: true, status: 'Running tool' });expect(remoteSessionStore.getSessionRunStatus('s1').tokenSpeed).toEqual(tokenSpeed);+ // The host withdrew the speed (suspended mid-generation): the stale reading goes too.+ pushMakerStatus('s1', { isRunning: true, generationReliable: false });+ expect(remoteSessionStore.getSessionRunStatus('s1').tokenSpeed).toBeUndefined();pushMakerStatus('s1', { isRunning: false });remoteSessionStore.applySessionActivity('dev-1', { sessionId: 's1', phase: 'running' });expect(remoteSessionStore.getSessionRunStatus('s1').tokenSpeed).toBeUndefined();diff --git a/apps/mobile/src/session/remoteSessionStore.ts b/apps/mobile/src/session/remoteSessionStore.tsindex cc0c48dfb2..f48f9f4079 100644--- a/apps/mobile/src/session/remoteSessionStore.ts+++ b/apps/mobile/src/session/remoteSessionStore.ts@@ -5012,8 +5012,9 @@ export const remoteSessionStore = {? data.generationReliable: (isTurnStart ? true : current.generationReliable);// 速度属于产生它的那一轮:新一轮清掉,没带字段的帧沿用最近一次(含收尾平均)。+ // 标记不可靠又不带速度的帧,表示电脑端撤回了速度(休眠/时钟异常)。const tokenSpeed = readTokenSpeedSnapshot(data?.tokenSpeed)- ?? (isTurnStart ? undefined : current.tokenSpeed);+ ?? (isTurnStart || data?.generationReliable === false ? undefined : current.tokenSpeed);// maker turn 边界 false→true 时清掉上一轮残留的 live task updates:它们是 turn 级// live 状态,残留到下一轮会被渲染层的孤儿兜底当作"仍在运行的子 agent"追加到消息流// 末尾(桌面端靠 idle demote / clear 清,手机 store 是常驻单例,只能在 turn 边界收口)。diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.mdindex b8a61bb4c3..4210cb4715 100644--- a/docs/dev-rules/protocol-compatibility.md+++ b/docs/dev-rules/protocol-compatibility.md@@ -11,6 +11,15 @@> **增量适用原则**:wire protocol 兼容对所有跨端改动生效,不因是小改而豁免。+## 实时生成速度++既有 maker status 可选携带 `tokenSpeed`(`rate`、`average`、`tokens`、`elapsedMs`、+`streaming`),由主机按 Pi `pi-token-speed` 扩展算法测得;仅用于显示,不参与费用、缓存或+上下文计数。新客户端校验数值,非法即丢弃;旧主机省略时沿用原 usage 计数路径,旧客户端忽略+新增字段。主机检测到挂起或时钟异常时,本轮后续帧不再携带该字段并标记+`generationReliable: false`,客户端据此丢弃已保留的速度。无需服务端更新、新通道、权限或+数据库 migration。+## 任务列表提前同步聊天正文同账号控制端声明 `session-list-messages-v1` 后,`sessions` 订阅同时接收普通用户/助手diff --git a/packages/maker-core/src/agents/claude-code/generation-timing.test.ts b/packages/maker-core/src/agents/claude-code/generation-timing.test.tsindex 4ed08da43d..4044f4314e 100644--- a/packages/maker-core/src/agents/claude-code/generation-timing.test.ts+++ b/packages/maker-core/src/agents/claude-code/generation-timing.test.ts@@ -2099,6 +2099,36 @@ describe('claude live token speed', () => {queue.end();});+ it('withdraws the speed when the host was suspended mid-generation', async () => {+ vi.useFakeTimers();+ const ctx = createTranslatorCtx();+ const queue = createAsyncQueue<AgentEvent>();+ const stream = (event: Record<string, unknown>, at: number) => {+ vi.setSystemTime(at);+ translateSdkMessage({ type: 'stream_event', event }, queue, ctx);+ };+ stream({ type: 'message_start', message: { model: 'claude-sonnet-4.5', usage: { input_tokens: 10 } } }, 1_000);+ stream({ type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } }, 1_000);+ stream({ type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'one two three four five' } }, 1_500);+ expect(ctx.rt.generation.tokenSpeed.snapshot(1_500)?.tokens).toBe(5);+ // The lid closes for five minutes: no heartbeat fires until the stream resumes.+ stream({ type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'six seven' } }, 301_500);+ stream({ type: 'message_delta', usage: { output_tokens: 7 } }, 302_000);+ vi.setSystemTime(302_000);+ translateSdkMessage(+ { type: 'result', stop_reason: 'end_turn', total_cost_usd: 0, num_turns: 1, usage: { input_tokens: 10, output_tokens: 7 } },+ queue,+ ctx,+ );+ queue.end();+ const events: AgentEvent[] = [];+ for await (const event of queue) events.push(event);+ const done = events.find((event) => event.type === 'status' && (event.data as { status?: string }).status === 'Done');+ expect(done?.data).toMatchObject({ generationReliable: false });+ expect(done?.data).not.toHaveProperty('tokenSpeed');+ resetClaudeGenerationTiming(ctx.rt.generation);+ });+it('counts Write tool arguments as generated output', () => {const ctx = createTranslatorCtx();const queue = createAsyncQueue<AgentEvent>();diff --git a/packages/maker-core/src/agents/claude-code/generation-timing.ts b/packages/maker-core/src/agents/claude-code/generation-timing.tsindex 3a461e5e3e..aabce5ce31 100644--- a/packages/maker-core/src/agents/claude-code/generation-timing.ts+++ b/packages/maker-core/src/agents/claude-code/generation-timing.ts@@ -65,6 +65,7 @@ function sampleHeartbeat(state: ClaudeGenerationState, now = Date.now()): void {now - previous > CLAUDE_GENERATION_HEARTBEAT_MS + CLAUDE_GENERATION_SUSPEND_GAP_MS) {state.reliable = false;+ state.tokenSpeed.invalidate();}state.heartbeatAt = now;}@@ -85,6 +86,7 @@ function closeInterval(state: ClaudeGenerationState, endedAt: number): void {if (startedAt === null) return;if (endedAt < startedAt) {state.reliable = false;+ state.tokenSpeed.invalidate();return;}state.durationMs += endedAt - startedAt;diff --git a/packages/maker-core/src/agents/codex/translator.test.ts b/packages/maker-core/src/agents/codex/translator.test.tsindex 46dca43f00..b6a25ef5a4 100644--- a/packages/maker-core/src/agents/codex/translator.test.ts+++ b/packages/maker-core/src/agents/codex/translator.test.ts@@ -516,9 +516,12 @@ describe('Codex generation timing', () => {try {const rt = newCodexRuntimeState();beginCodexGenerationTurn(rt, 'turn-1', 1_000);+ rt.tokenSpeed.recordDelta('a b c d e f g h i j', 1_000);wallClockSpy.mockReturnValue(40_000);finalizeCodexGenerationTurn(rt, 'turn-1', 2_000);expect(codexGenerationDurationMs(rt)).toBeUndefined();+ // The suspended wall clock would dilute the live speed too.+ expect(rt.tokenSpeed.snapshot(40_000)).toBeUndefined();} finally {wallClockSpy.mockRestore();}diff --git a/packages/maker-core/src/agents/codex/translator.ts b/packages/maker-core/src/agents/codex/translator.tsindex 2f5ff46150..2fc6f689d4 100644--- a/packages/maker-core/src/agents/codex/translator.ts+++ b/packages/maker-core/src/agents/codex/translator.ts@@ -170,6 +170,7 @@ function sampleCodexGenerationHeartbeat(rt: CodexRuntimeState, now = Date.now())now - previous > CODEX_GENERATION_HEARTBEAT_MS + CODEX_GENERATION_SUSPEND_GAP_MS) {rt.generationTimingReliable = false;+ rt.tokenSpeed.invalidate();}rt.generationHeartbeatAt = now;}@@ -204,6 +205,7 @@ function closeCodexGenerationInterval(rt: CodexRuntimeState, endedAt: number): vif (startedAt === null) return;if (endedAt < startedAt) {rt.generationTimingReliable = false;+ rt.tokenSpeed.invalidate();return;}rt.generationDurationMs += endedAt - startedAt;diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.tsindex ed569f0c24..abd35d291c 100644--- a/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts@@ -1610,6 +1610,10 @@ describe('pi translator', () => {const { queue, events } = makeQueue();translatePiEvent(ev({ type: 'agent_start' }), queue, ctx);translatePiEvent(ev({ type: 'message_start' }), queue, ctx);+ translatePiEvent(ev({ type: 'message_update', assistantMessageEvent: { type: 'text_start', contentIndex: 0 } }), queue, ctx);+ translatePiEvent(ev({ type: 'message_update', assistantMessageEvent: {+ type: 'text_delta', contentIndex: 0, delta: 'one two three four five',+ } }), queue, ctx);nowSpy.mockReturnValue(40_000);translatePiEvent(ev({@@ -1622,6 +1626,10 @@ describe('pi translator', () => {translatePiEvent(ev({ type: 'agent_settled' }), queue, ctx);const usage = (events.find((e) => e.type === 'done')!.data as { usage: Record<string, unknown> }).usage;expect(usage).not.toHaveProperty('durationMs');+ // The suspended wall clock would dilute the live speed too: the host withdraws it.+ const lastStatus = events.filter((e) => e.type === 'status').at(-1)?.data as Record<string, unknown>;+ expect(lastStatus).toMatchObject({ generationReliable: false });+ expect(lastStatus).not.toHaveProperty('tokenSpeed');} finally {nowSpy.mockRestore();}diff --git a/packages/maker-core/src/agents/pi/translator.ts b/packages/maker-core/src/agents/pi/translator.tsindex f953280070..02ada8ad97 100644--- a/packages/maker-core/src/agents/pi/translator.ts+++ b/packages/maker-core/src/agents/pi/translator.ts@@ -347,6 +347,7 @@ function samplePiGenerationHeartbeat(ctx: PiTranslateContext, now = Date.now()):PI_GENERATION_HEARTBEAT_MS + PI_GENERATION_SUSPEND_GAP_MS) {ctx.generationHeartbeatReliable = false;+ ctx.tokenSpeed.invalidate();}ctx.generationHeartbeatAt = now;}diff --git a/packages/maker-core/src/agents/shared/token-speed.test.ts b/packages/maker-core/src/agents/shared/token-speed.test.tsindex dfc60a0a26..5459202f17 100644--- a/packages/maker-core/src/agents/shared/token-speed.test.ts+++ b/packages/maker-core/src/agents/shared/token-speed.test.ts@@ -119,6 +119,20 @@ describe('TokenSpeedMeter (pi-token-speed algorithm)', () => {expect(lowest).toBeCloseTo(2_000, 5);});+ it('shows nothing for the rest of a turn whose clock was invalidated, until reset', () => {+ const meter = new TokenSpeedMeter();+ meter.start(0);+ meter.recordDelta('a b c d e f g h i j', 1_000);+ meter.invalidate();+ meter.recordDelta('a b c d e f g h i j', 300_000);+ meter.stop(20, 301_000);+ expect(meter.snapshot(301_000)).toBeUndefined();+ meter.reset();+ meter.start(0);+ meter.recordDelta('a b c d e f g h i j', 1_000);+ expect(meter.snapshot(1_000)?.tokens).toBe(10);+ });+it('snaps to the real output at the end and reports the average', () => {const meter = new TokenSpeedMeter();meter.start(0);diff --git a/packages/maker-core/src/agents/shared/token-speed.ts b/packages/maker-core/src/agents/shared/token-speed.tsindex 0010187147..c11e94aef0 100644--- a/packages/maker-core/src/agents/shared/token-speed.ts+++ b/packages/maker-core/src/agents/shared/token-speed.ts@@ -44,6 +44,7 @@ export class TokenSpeedMeter {private events: { time: number; tokens: number }[] = [];private windowStartIndex = 0;private lastPublishAt = -Infinity;+ private invalid = false;/** New turn: forget the previous measurement. */reset(): void {@@ -59,6 +60,15 @@ export class TokenSpeedMeter {this.events = [];this.windowStartIndex = 0;this.lastPublishAt = -Infinity;+ this.invalid = false;+ }++ /**+ * The generation clock is wrong for the rest of this turn (host suspended or+ * the clock went backwards): wall-clock time would dilute the speed, so show none.+ */+ invalidate(): void {+ this.invalid = true;}/**@@ -128,7 +138,7 @@ export class TokenSpeedMeter {}snapshot(now = Date.now()): TokenSpeedSnapshot | undefined {- if (!this.started || this.tokenCount <= 0) return undefined;+ if (!this.started || this.invalid || this.tokenCount <= 0) return undefined;const elapsedMs = this.elapsedMs(now);const average = elapsedMs > 0 ? (this.tokenCount * 1_000) / elapsedMs : 0;return {