Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
refactor(cowork-ui): trim metadata tests and strengthen timing coverage
thinhlpg49ebb5e4cb6b
diff --git a/web-app/src/lib/__tests__/coworkRunner.test.ts b/web-app/src/lib/__tests__/coworkRunner.test.tsindex b7272459d..d26565b11 100644--- a/web-app/src/lib/__tests__/coworkRunner.test.ts+++ b/web-app/src/lib/__tests__/coworkRunner.test.ts@@ -17,6 +17,7 @@ import {} from '../coworkRunner'import { MAX_SESSION_TOKENS } from '../coworkBudget'import { encodeToolImageSentinel } from '../tool-image-sentinel'+import { coworkTurnsToUIMessages } from '../coworkTurns'const streamOf = (chunks: UIMessageChunk[]): ReadableStream<UIMessageChunk> =>new ReadableStream({@@ -124,9 +125,7 @@ describe('consumeStep', () => {expect(r.errorText).toBe('boom')})- // The transcript renders chat's token-speed popover from this block, so the- // whole finish metadata has to survive the fold, not just the usage fields.- it('keeps the finish metadata the transport stamped on the step', async () => {+ it('preserves finish metadata through the rendered transcript', async () => {const tokenSpeed = {tokenSpeed: 42.5,promptSpeed: 120,@@ -148,7 +147,11 @@ describe('consumeStep', () => {noopSink())expect(r.usage?.total_tokens).toBe(310)- expect(r.metadata).toMatchObject({ finishReason: 'stop', tokenSpeed })+ const messages = coworkTurnsToUIMessages(turnsFor(r, new Map()))+ expect(messages[0].metadata).toMatchObject({+ finishReason: 'stop',+ tokenSpeed,+ })})})@@ -434,50 +437,6 @@ describe('reasoning placement', () => {})})-describe('step metadata', () => {- const tokenSpeed = {- tokenSpeed: 42.5,- promptSpeed: 120,- tokenCount: 300,- durationMs: 7000,- }- const step: StepResult = {- text: 'answer',- reasoning: '',- toolCalls: [],- usage: null,- metadata: { usage: { totalTokens: 310 }, tokenSpeed },- aborted: false,- }-- // The row is what `coworkTurnsToUIMessages` turns into the assistant message- // `MessageItem` reads its metadata from.- it('rides the row that opens the step, not every row of it', () => {- const rows = turnsFor(- {- ...step,- toolCalls: [- { toolCallId: 'c1', toolName: 'read', input: {} } as PendingToolCall,- ],- },- new Map()- )- expect(rows[0].metadata).toEqual(step.metadata)- expect(rows[1].metadata).toBeUndefined()- })-- // A step that answers nothing produces no rows, so there is nothing to hang- // the metadata on -- and no message for it to describe.- it('has nothing to attach to when the step produced no rows', () => {- expect(- turnsFor(- { ...step, text: '', reasoning: '', toolCalls: [] },- new Map()- )- ).toEqual([])- })-})-describe('diff sidecar', () => {// A diff is a rendering aid. Sending it to the model doubles the cost of// every edit and corrupts the output the tool widget parses.diff --git a/web-app/src/lib/__tests__/stepMetadata.test.ts b/web-app/src/lib/__tests__/stepMetadata.test.tsindex 54a8bbabc..93bc07c6f 100644--- a/web-app/src/lib/__tests__/stepMetadata.test.ts+++ b/web-app/src/lib/__tests__/stepMetadata.test.ts@@ -33,7 +33,7 @@ describe('createStepMetadata', () => {type: 'finish-step',providerMetadata: { providerMetadata: { tokensPerSecond: 30, promptPerSecond: 120 } },})- vi.advanceTimersByTime(2000)+ vi.advanceTimersByTime(4000) // Observed rate is 15; provider rate is 30.const out = meta.onPart(finish({ inputTokens: 900, outputTokens: 60, totalTokens: 960 })diff --git a/web-app/src/lib/stepMetadata.ts b/web-app/src/lib/stepMetadata.tsindex 652850eb0..b1ec368d1 100644--- a/web-app/src/lib/stepMetadata.ts+++ b/web-app/src/lib/stepMetadata.ts@@ -29,14 +29,8 @@ type StreamPart = {const round2 = (n: number) => Math.round(n * 100) / 100/**- * Assembles the metadata a streamed step stamps on its message.- *- * One implementation for every surface that streams a step -- the chat- * transport and a subagent's own stream -- so the speed a Cowork lane shows is- * computed exactly as chat computes it. Generation speed is the engine's own- * when the provider reports timings (llama.cpp, MLX) and the observed output- * rate otherwise. The clock starts at the first content part, so prompt- * processing is not counted against generation.+ * Shared chat/subagent finish metadata. Prefer engine timings; otherwise measure+ * from the first content part to exclude prompt processing.*/export function createStepMetadata() {let startedAt: number | undefined