Skip to content

Commit b278ef6

Browse files
dukebwrekram1-node
andauthored
fix(tui): include reasoning tokens in throughput (#48689)
Co-authored-by: Aiden Cline <[email protected]>
1 parent 3e3a4ae commit b278ef6

4 files changed

Lines changed: 45 additions & 14 deletions

File tree

‎packages/core/test/session-runner.test.ts‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ import { SessionSystemPrompt } from "@opencode/core/session/system-prompt"
7676
import { ID, Model } from "@opencode/core/model"
7777
import { Location } from "@opencode/core/location"
7878
import { Provider } from "@opencode/core/provider"
79-
import { Cause, Context, Deferred, Effect, Exit, Fiber, Layer, Queue, Schema, Scope, Stream } from "effect"
79+
import { Cause, Context, DateTime, Deferred, Effect, Exit, Fiber, Layer, Queue, Schema, Scope, Stream } from "effect"
8080
import { TestClock } from "effect/testing"
8181
import { asc, desc, eq, sql } from "drizzle-orm"
8282
import { testEffect } from "./lib/effect"
@@ -3391,11 +3391,17 @@ describe("SessionRunnerLLM", () => {
33913391

33923392
scenario("consumes the full provider stream before recording its boundary and settling local tools", function* (s) {
33933393
yield* s.admit("Echo this")
3394+
const request = yield* s.llm.gate
33943395
const tail = yield* Deferred.make<void>()
33953396
const complete = yield* Deferred.make<void>()
33963397
const finished = yield* Deferred.make<void>()
33973398
yield* s.llm.push(
3398-
Stream.fromIterable(TestLLM.tool("call-streamed", "echo", { text: "hello" })).pipe(
3399+
Stream.fromIterable(
3400+
TestLLM.complete(
3401+
{ reason: { normalized: "tool-calls" }, usage: { outputTokens: 100, reasoningTokens: 80 } },
3402+
LLMEvent.toolCall({ id: "call-streamed", name: "echo", input: { text: "hello" } }),
3403+
),
3404+
).pipe(
33993405
Stream.concat(
34003406
Stream.fromEffect(Deferred.succeed(tail, undefined).pipe(Effect.andThen(Deferred.await(complete)))).pipe(
34013407
Stream.drain,
@@ -3413,25 +3419,39 @@ describe("SessionRunnerLLM", () => {
34133419
)
34143420
const run = yield* Effect.forkChild(s.resume)
34153421

3422+
yield* request.started
3423+
yield* TestClock.adjust("2 seconds")
3424+
yield* request.release
34163425
yield* tools.started
34173426
yield* Deferred.await(tail)
34183427
expect(s.requests).toHaveLength(1)
34193428
expect(yield* recordedEventTypes(sessionID)).not.toContain("session.step.streamed.1")
34203429
expect(requireAssistant(yield* s.context).time.completed).toBeUndefined()
3430+
yield* TestClock.adjust("3 seconds")
34213431
yield* Deferred.succeed(complete, undefined)
34223432
yield* Fiber.join(streamed)
34233433
expect(yield* Deferred.isDone(finished)).toBe(true)
34243434
const assistant = requireAssistant(yield* s.context)
34253435
expect(assistant.time.streamed).toBeDefined()
3436+
expect(DateTime.toEpochMillis(assistant.time.streamed!) - DateTime.toEpochMillis(assistant.time.created)).toBe(
3437+
5_000,
3438+
)
34263439
expect(assistant.time.completed).toBeUndefined()
34273440
expect(assistant.content).toMatchObject([{ type: "tool", state: { status: "running" } }])
34283441

3442+
yield* TestClock.adjust("10 seconds")
34293443
yield* tools.release
34303444
yield* Fiber.join(run)
34313445
const events = yield* recordedEventTypes(sessionID)
34323446
expect(events.indexOf("session.step.streamed.1")).toBeLessThan(events.indexOf("session.tool.success.2"))
34333447
expect(events.indexOf("session.tool.success.2")).toBeLessThan(events.indexOf("session.step.ended.1"))
34343448
expect(events.filter((type) => type === "session.step.streamed.1")).toHaveLength(2)
3449+
yield* replaySessionProjection(sessionID)
3450+
const replayed = (yield* s.context).find((message) => message.id === assistant.id)
3451+
expect(replayed).toMatchObject({
3452+
time: { created: assistant.time.created, streamed: assistant.time.streamed },
3453+
tokens: { output: 20, reasoning: 80 },
3454+
})
34353455
})
34363456

34373457
scenario("restores durable reasoning provider metadata in the next request", function* (s) {
@@ -5037,14 +5057,18 @@ describe("SessionRunnerLLM", () => {
50375057
scenario(`bounds jittered exponential backoff before output for ${failure.name}`, function* (s) {
50385058
yield* s.admit("Retry transport")
50395059
yield* s.llm.push(TestLLM.failAfter(failure(), LLMEvent.stepStart({ index: 0 })))
5040-
yield* s.llm.push(TestLLM.text("Recovered", "retry-success"))
5060+
yield* s.llm.push(
5061+
Stream.fromEffect(Effect.sleep(400)).pipe(
5062+
Stream.flatMap(() => Stream.fromIterable(TestLLM.text("Recovered", "retry-success"))),
5063+
),
5064+
)
50415065

50425066
const scheduled = yield* subscribeRetries(s)
50435067
const run = yield* s.resume.pipe(Effect.forkChild)
50445068
yield* Queue.take(scheduled)
50455069
yield* TestClock.adjust("1599 millis")
50465070
expect(s.requests).toHaveLength(1)
5047-
yield* TestClock.adjust("801 millis")
5071+
yield* TestClock.adjust("1201 millis")
50485072
yield* Fiber.join(run)
50495073

50505074
expect(s.requests).toHaveLength(2)
@@ -5058,6 +5082,10 @@ describe("SessionRunnerLLM", () => {
50585082
])
50595083
yield* replaySessionProjection(sessionID)
50605084
expect((yield* s.context).filter((message) => message.type === "assistant")).toHaveLength(1)
5085+
const assistant = requireAssistant(yield* s.context)
5086+
expect(DateTime.toEpochMillis(assistant.time.streamed!) - DateTime.toEpochMillis(assistant.time.created)).toBe(
5087+
400,
5088+
)
50615089
})
50625090
}
50635091

‎packages/tui/src/config/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export const Info = Schema.Struct({
158158
description: "Show user attachment and tool-result images in the session transcript",
159159
}),
160160
tps: Schema.optional(Schema.Boolean).annotate({
161-
description: "Show output tokens per second in assistant footers",
161+
description: "Show average tokens per second",
162162
}),
163163
markdown: Schema.optional(Schema.Literals(["source", "rendered"])).annotate({
164164
description: "Show Markdown syntax markers or conceal them in rendered transcript content",

‎packages/tui/src/routes/session/rows.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ export function turnTokensPerSecond(
378378
step.time.streamed === undefined ? [] : [Math.max(0, step.time.streamed - step.time.created)],
379379
)
380380
if (steps.length === 0 || durations.length !== steps.length) return
381-
const output = steps.reduce((total, step) => total + (step.tokens?.output ?? 0), 0)
381+
const output = steps.reduce((total, step) => total + (step.tokens?.output ?? 0) + (step.tokens?.reasoning ?? 0), 0)
382382
const duration = durations.reduce((total, value) => total + value, 0)
383383
if (output <= 0 || duration <= 0) return
384384
// Aggregate before dividing so each step is weighted by its provider-active duration.

‎packages/tui/test/cli/tui/session-rows.test.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,23 @@ test("measures turn duration from the user prompt across assistant steps", () =>
2525
expect(turnDuration(final, messages)).toBe(29_000)
2626
})
2727

28-
test("measures turn output throughput across model steps without tool time", () => {
28+
test("measures request throughput including reasoning across model changes without tool time", () => {
2929
const first = assistant("assistant-1", [])
30-
first.time = { created: 8_000, streamed: 10_000, completed: 20_000 }
30+
first.time = { created: 6_000, streamed: 10_000, completed: 20_000 }
3131
first.tokens = { input: 10, output: 20, reasoning: 5, cache: { read: 0, write: 0 } }
3232
const final = assistant("assistant-2", [])
33-
final.time = { created: 27_000, streamed: 30_000, completed: 31_000 }
33+
final.model = { id: "other-model", providerID: "other-provider", variant: "other-variant" }
34+
final.time = { created: 24_000, streamed: 30_000, completed: 31_000 }
3435
final.tokens = { input: 20, output: 30, reasoning: 10, cache: { read: 0, write: 0 } }
3536
const messages: SessionMessageInfo[] = [
3637
{ type: "user", id: "user-1", text: "Question", time: { created: 1_000 } },
3738
first,
3839
final,
3940
]
4041

41-
expect(turnTokensPerSecond(final, messages)).toBe(10)
42+
expect(turnTokensPerSecond(final, messages)).toBe(6.5)
43+
first.time.streamed = undefined
44+
expect(turnTokensPerSecond(final, messages)).toBeUndefined()
4245
})
4346

4447
test("omits turn throughput when a stream boundary is unavailable", () => {
@@ -79,10 +82,10 @@ test.each([false, true])(
7982
: [],
8083
),
8184
).toEqual([
82-
[2_000, 5],
83-
[3_000, 10],
84-
[6_000, 15],
85-
[4_000, 6],
85+
[2_000, 7],
86+
[3_000, 12],
87+
[6_000, 17],
88+
[4_000, 7],
8689
[0, undefined],
8790
])
8891
},

0 commit comments

Comments
 (0)