fix(tui): include reasoning tokens in throughput (#48689)

Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com>
This commit is contained in:
Brendan Duke
2026-09-17 21:12:06 -04:00
committed by GitHub
Unverified
parent 3e3a4ae46b
commit b278ef6b82
4 changed files with 45 additions and 14 deletions
+32 -4
View File
@@ -76,7 +76,7 @@ import { SessionSystemPrompt } from "@opencode/core/session/system-prompt"
import { ID, Model } from "@opencode/core/model"
import { Location } from "@opencode/core/location"
import { Provider } from "@opencode/core/provider"
import { Cause, Context, Deferred, Effect, Exit, Fiber, Layer, Queue, Schema, Scope, Stream } from "effect"
import { Cause, Context, DateTime, Deferred, Effect, Exit, Fiber, Layer, Queue, Schema, Scope, Stream } from "effect"
import { TestClock } from "effect/testing"
import { asc, desc, eq, sql } from "drizzle-orm"
import { testEffect } from "./lib/effect"
@@ -3391,11 +3391,17 @@ describe("SessionRunnerLLM", () => {
scenario("consumes the full provider stream before recording its boundary and settling local tools", function* (s) {
yield* s.admit("Echo this")
const request = yield* s.llm.gate
const tail = yield* Deferred.make<void>()
const complete = yield* Deferred.make<void>()
const finished = yield* Deferred.make<void>()
yield* s.llm.push(
Stream.fromIterable(TestLLM.tool("call-streamed", "echo", { text: "hello" })).pipe(
Stream.fromIterable(
TestLLM.complete(
{ reason: { normalized: "tool-calls" }, usage: { outputTokens: 100, reasoningTokens: 80 } },
LLMEvent.toolCall({ id: "call-streamed", name: "echo", input: { text: "hello" } }),
),
).pipe(
Stream.concat(
Stream.fromEffect(Deferred.succeed(tail, undefined).pipe(Effect.andThen(Deferred.await(complete)))).pipe(
Stream.drain,
@@ -3413,25 +3419,39 @@ describe("SessionRunnerLLM", () => {
)
const run = yield* Effect.forkChild(s.resume)
yield* request.started
yield* TestClock.adjust("2 seconds")
yield* request.release
yield* tools.started
yield* Deferred.await(tail)
expect(s.requests).toHaveLength(1)
expect(yield* recordedEventTypes(sessionID)).not.toContain("session.step.streamed.1")
expect(requireAssistant(yield* s.context).time.completed).toBeUndefined()
yield* TestClock.adjust("3 seconds")
yield* Deferred.succeed(complete, undefined)
yield* Fiber.join(streamed)
expect(yield* Deferred.isDone(finished)).toBe(true)
const assistant = requireAssistant(yield* s.context)
expect(assistant.time.streamed).toBeDefined()
expect(DateTime.toEpochMillis(assistant.time.streamed!) - DateTime.toEpochMillis(assistant.time.created)).toBe(
5_000,
)
expect(assistant.time.completed).toBeUndefined()
expect(assistant.content).toMatchObject([{ type: "tool", state: { status: "running" } }])
yield* TestClock.adjust("10 seconds")
yield* tools.release
yield* Fiber.join(run)
const events = yield* recordedEventTypes(sessionID)
expect(events.indexOf("session.step.streamed.1")).toBeLessThan(events.indexOf("session.tool.success.2"))
expect(events.indexOf("session.tool.success.2")).toBeLessThan(events.indexOf("session.step.ended.1"))
expect(events.filter((type) => type === "session.step.streamed.1")).toHaveLength(2)
yield* replaySessionProjection(sessionID)
const replayed = (yield* s.context).find((message) => message.id === assistant.id)
expect(replayed).toMatchObject({
time: { created: assistant.time.created, streamed: assistant.time.streamed },
tokens: { output: 20, reasoning: 80 },
})
})
scenario("restores durable reasoning provider metadata in the next request", function* (s) {
@@ -5037,14 +5057,18 @@ describe("SessionRunnerLLM", () => {
scenario(`bounds jittered exponential backoff before output for ${failure.name}`, function* (s) {
yield* s.admit("Retry transport")
yield* s.llm.push(TestLLM.failAfter(failure(), LLMEvent.stepStart({ index: 0 })))
yield* s.llm.push(TestLLM.text("Recovered", "retry-success"))
yield* s.llm.push(
Stream.fromEffect(Effect.sleep(400)).pipe(
Stream.flatMap(() => Stream.fromIterable(TestLLM.text("Recovered", "retry-success"))),
),
)
const scheduled = yield* subscribeRetries(s)
const run = yield* s.resume.pipe(Effect.forkChild)
yield* Queue.take(scheduled)
yield* TestClock.adjust("1599 millis")
expect(s.requests).toHaveLength(1)
yield* TestClock.adjust("801 millis")
yield* TestClock.adjust("1201 millis")
yield* Fiber.join(run)
expect(s.requests).toHaveLength(2)
@@ -5058,6 +5082,10 @@ describe("SessionRunnerLLM", () => {
])
yield* replaySessionProjection(sessionID)
expect((yield* s.context).filter((message) => message.type === "assistant")).toHaveLength(1)
const assistant = requireAssistant(yield* s.context)
expect(DateTime.toEpochMillis(assistant.time.streamed!) - DateTime.toEpochMillis(assistant.time.created)).toBe(
400,
)
})
}
+1 -1
View File
@@ -158,7 +158,7 @@ export const Info = Schema.Struct({
description: "Show user attachment and tool-result images in the session transcript",
}),
tps: Schema.optional(Schema.Boolean).annotate({
description: "Show output tokens per second in assistant footers",
description: "Show average tokens per second",
}),
markdown: Schema.optional(Schema.Literals(["source", "rendered"])).annotate({
description: "Show Markdown syntax markers or conceal them in rendered transcript content",
+1 -1
View File
@@ -378,7 +378,7 @@ export function turnTokensPerSecond(
step.time.streamed === undefined ? [] : [Math.max(0, step.time.streamed - step.time.created)],
)
if (steps.length === 0 || durations.length !== steps.length) return
const output = steps.reduce((total, step) => total + (step.tokens?.output ?? 0), 0)
const output = steps.reduce((total, step) => total + (step.tokens?.output ?? 0) + (step.tokens?.reasoning ?? 0), 0)
const duration = durations.reduce((total, value) => total + value, 0)
if (output <= 0 || duration <= 0) return
// Aggregate before dividing so each step is weighted by its provider-active duration.
+11 -8
View File
@@ -25,12 +25,13 @@ test("measures turn duration from the user prompt across assistant steps", () =>
expect(turnDuration(final, messages)).toBe(29_000)
})
test("measures turn output throughput across model steps without tool time", () => {
test("measures request throughput including reasoning across model changes without tool time", () => {
const first = assistant("assistant-1", [])
first.time = { created: 8_000, streamed: 10_000, completed: 20_000 }
first.time = { created: 6_000, streamed: 10_000, completed: 20_000 }
first.tokens = { input: 10, output: 20, reasoning: 5, cache: { read: 0, write: 0 } }
const final = assistant("assistant-2", [])
final.time = { created: 27_000, streamed: 30_000, completed: 31_000 }
final.model = { id: "other-model", providerID: "other-provider", variant: "other-variant" }
final.time = { created: 24_000, streamed: 30_000, completed: 31_000 }
final.tokens = { input: 20, output: 30, reasoning: 10, cache: { read: 0, write: 0 } }
const messages: SessionMessageInfo[] = [
{ type: "user", id: "user-1", text: "Question", time: { created: 1_000 } },
@@ -38,7 +39,9 @@ test("measures turn output throughput across model steps without tool time", ()
final,
]
expect(turnTokensPerSecond(final, messages)).toBe(10)
expect(turnTokensPerSecond(final, messages)).toBe(6.5)
first.time.streamed = undefined
expect(turnTokensPerSecond(final, messages)).toBeUndefined()
})
test("omits turn throughput when a stream boundary is unavailable", () => {
@@ -79,10 +82,10 @@ test.each([false, true])(
: [],
),
).toEqual([
[2_000, 5],
[3_000, 10],
[6_000, 15],
[4_000, 6],
[2_000, 7],
[3_000, 12],
[6_000, 17],
[4_000, 7],
[0, undefined],
])
},