Skip to content

Commit ad549b3

Browse files
committed
fix(tui): retain cost estimates with partial usage history
1 parent b1bcc8a commit ad549b3

6 files changed

Lines changed: 373 additions & 61 deletions

File tree

‎packages/tui/src/feature-plugins/prompt/footer.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function PromptFooter(props: {
4646
)
4747
const cost = props.context.data.session.cost(props.sessionID)
4848
const estimate = enabled()
49-
? (costEstimate(
49+
? costEstimate(
5050
session.parentID
5151
? [session]
5252
: props.context.data.session
@@ -55,7 +55,7 @@ export function PromptFooter(props: {
5555
.filter((info) => info !== undefined),
5656
props.context.data.location.model.list(session.location),
5757
(id) => props.context.data.session.message.list(id),
58-
) ?? 0)
58+
)
5959
: 0
6060
return [
6161
usage ? formatContextUsage(usage.tokens, usage.percent) : undefined,

‎packages/tui/src/feature-plugins/sidebar/context.tsx‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,15 @@ export function SidebarContext(props: { context: Plugin.Context; sessionID: stri
2424
const info = session()
2525
if (!info) return 0
2626
const data = props.context.data.session
27-
return (
28-
costEstimate(
29-
info.parentID
30-
? [info]
31-
: data
32-
.family(props.sessionID)
33-
.map((id) => data.get(id))
34-
.filter((info) => info !== undefined),
35-
props.context.data.location.model.list(info.location),
36-
(id) => data.message.list(id),
37-
) ?? 0
27+
return costEstimate(
28+
info.parentID
29+
? [info]
30+
: data
31+
.family(props.sessionID)
32+
.map((id) => data.get(id))
33+
.filter((info) => info !== undefined),
34+
props.context.data.location.model.list(info.location),
35+
(id) => data.message.list(id),
3836
)
3937
})
4038

‎packages/tui/src/util/session.ts‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { ModelInfo, SessionInfo, SessionMessageInfo } from "@opencode/client"
22
import { lastAssistantWithUsage } from "@opencode/client/solid"
3-
import { TokenUsage } from "@opencode/schema/token-usage"
43
import { Locale } from "./locale"
54

65
type SessionNode = {
@@ -71,33 +70,38 @@ export function costEstimate(
7170
models: ReadonlyArray<ModelInfo> | undefined,
7271
messages: (sessionID: string) => ReadonlyArray<SessionMessageInfo>,
7372
) {
74-
return sessions.reduce<number | undefined>((total, session) => {
75-
if (total === undefined) return
73+
return sessions.reduce((total, session) => {
7674
const model = models?.find(
7775
(candidate) =>
7876
session.model && candidate.providerID === session.model.providerID && candidate.id === session.model.id,
7977
)
8078
if (!model) return total
81-
const usages = model.cost.some((cost) => cost.tier !== undefined)
82-
? messages(session.id).flatMap((message) => ("tokens" in message && message.tokens ? [message.tokens] : []))
83-
: [session.tokens]
84-
// Lifetime totals cannot select per-call tiers. Paginated or unloaded history must not produce a partial estimate.
85-
if (usages.reduce((sum, tokens) => sum + TokenUsage.total(tokens), 0) !== TokenUsage.total(session.tokens)) return
86-
return usages.reduce((sum, tokens) => {
79+
const base = model.cost.find((cost) => cost.tier === undefined)
80+
const estimate = total + tokenCost(session.tokens, base)
81+
if (!model.cost.some((cost) => cost.tier !== undefined)) return estimate
82+
// Only assistant messages retain individual call usage. Title, compaction, and unloaded usage stay at base rates.
83+
return messages(session.id).reduce((sum, message) => {
84+
if (message.type !== "assistant" || !message.tokens) return sum
85+
// Forks copy parent timestamps and messages, but start with zero recorded usage.
86+
if (session.fork && message.time.created <= session.time.created) return sum
87+
const tokens = message.tokens
8788
const context = tokens.input + tokens.cache.read + tokens.cache.write
8889
const tier = model.cost
8990
.filter((cost) => cost.tier?.type === "context" && context > cost.tier.size)
9091
.toSorted((a, b) => (b.tier?.size ?? 0) - (a.tier?.size ?? 0))[0]
91-
const rate = tier ?? model.cost.find((cost) => cost.tier === undefined)
92-
if (!rate) return sum
93-
return (
94-
sum +
95-
(tokens.input * rate.input +
96-
(tokens.output + tokens.reasoning) * rate.output +
97-
tokens.cache.read * rate.cache.read +
98-
tokens.cache.write * rate.cache.write) /
99-
1_000_000
100-
)
101-
}, total)
92+
if (!tier) return sum
93+
return sum + tokenCost(tokens, tier) - tokenCost(tokens, base)
94+
}, estimate)
10295
}, 0)
10396
}
97+
98+
function tokenCost(tokens: SessionInfo["tokens"], rate: ModelInfo["cost"][number] | undefined) {
99+
if (!rate) return 0
100+
return (
101+
(tokens.input * rate.input +
102+
(tokens.output + tokens.reasoning) * rate.output +
103+
tokens.cache.read * rate.cache.read +
104+
tokens.cache.write * rate.cache.write) /
105+
1_000_000
106+
)
107+
}

‎packages/tui/test/feature-plugins/prompt-footer.test.tsx‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,14 @@ test("prompt footer can hide details", async () => {
152152
})
153153

154154
test.each([
155-
{ sessionID: "session", input: 500_000, expected: "≈ $12.60" },
156-
{ sessionID: "child", input: 500_000, expected: "≈ $1.50" },
157-
{ sessionID: "child", input: 0, expected: undefined },
155+
{ sessionID: "session", input: 500_000, tiered: false, expected: "≈ $12.60" },
156+
{ sessionID: "child", input: 500_000, tiered: false, expected: "≈ $1.50" },
157+
{ sessionID: "child", input: 0, tiered: false, expected: undefined },
158+
{ sessionID: "session", input: 500_000, tiered: true, expected: "≈ $12.60" },
159+
{ sessionID: "child", input: 500_000, tiered: true, expected: "≈ $1.50" },
158160
])(
159-
"prompt footer estimates $sessionID spend with $input child input tokens",
160-
async ({ sessionID, input, expected }) => {
161+
"prompt footer estimates $sessionID spend with $input child input tokens (tiered: $tiered)",
162+
async ({ sessionID, input, tiered, expected }) => {
161163
const color = RGBA.fromInts(200, 200, 200)
162164
const context = {
163165
location: { directory: "/workspace" },
@@ -185,7 +187,23 @@ test.each([
185187
location: {
186188
model: {
187189
list: () => [
188-
{ providerID: "provider", id: "model", cost: [{ input: 3, output: 15, cache: { read: 0.3, write: 0 } }] },
190+
{
191+
providerID: "provider",
192+
id: "model",
193+
cost: [
194+
{ input: 3, output: 15, cache: { read: 0.3, write: 0 } },
195+
...(tiered
196+
? [
197+
{
198+
tier: { type: "context", size: 200_000 },
199+
input: 6,
200+
output: 22.5,
201+
cache: { read: 0.6, write: 0 },
202+
},
203+
]
204+
: []),
205+
],
206+
},
189207
],
190208
},
191209
},

‎packages/tui/test/feature-plugins/sidebar-context.test.tsx‎

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { RGBA } from "@opentui/core"
44
import { testRender } from "@opentui/solid"
55
import type { Context } from "@opencode/plugin/tui/context"
66
import type { ModelInfo } from "@opencode/client"
7+
import { createSignal } from "solid-js"
78
import { ConfigProvider } from "../../src/config"
89
import { createTuiResolvedConfig } from "../fixture/tui-runtime"
910
import { SidebarContext } from "../../src/feature-plugins/sidebar/context"
@@ -36,13 +37,16 @@ function context(options?: {
3637
family: () => family?.map((_, index) => (index === 0 ? "session" : `child-${index}`)) ?? [],
3738
cost: () => options?.cost ?? 0,
3839
message: {
39-
list: () =>
40-
(options?.steps ?? (options?.tokens ? [usage(options.tokens)] : [])).map((tokens, index) => ({
41-
id: `message-${index}`,
42-
type: "assistant",
43-
model: { providerID: "provider", id: "model" },
44-
tokens,
45-
})),
40+
list: (id: string) =>
41+
(id === "session" ? (options?.steps ?? (options?.tokens ? [usage(options.tokens)] : [])) : []).map(
42+
(tokens, index) => ({
43+
id: `message-${index}`,
44+
type: "assistant",
45+
model: { providerID: "provider", id: "model" },
46+
tokens,
47+
time: { created: 0 },
48+
}),
49+
),
4650
},
4751
},
4852
location: {
@@ -135,11 +139,19 @@ test("sidebar keeps the reported cost over the estimate", async () => {
135139
}
136140
})
137141

138-
test.each([true, false])("sidebar uses per-call tiers only with complete history: %s", async (complete) => {
142+
test.each([
143+
{ name: "complete history", complete: true, title: 0, child: 0, expected: "≈ $1.20 spent" },
144+
{ name: "partial history", complete: false, title: 0, child: 0, expected: "≈ $1.20 spent" },
145+
{ name: "title usage", complete: false, title: 10_000, child: 0, expected: "≈ $1.23 spent" },
146+
{ name: "unloaded child", complete: false, title: 10_000, child: 100_000, expected: "≈ $1.53 spent" },
147+
])("sidebar keeps tiered estimates with $name", async ({ complete, title, child, expected }) => {
139148
const app = await render(
140149
context({
141150
experiment: true,
142-
family: [{ model: { providerID: "provider", id: "model" }, tokens: usage(300_000, 20_000) }],
151+
family: [
152+
{ model: { providerID: "provider", id: "model" }, tokens: usage(300_000 + title, 20_000) },
153+
{ parentID: "session", model: { providerID: "provider", id: "model" }, tokens: usage(child) },
154+
],
143155
steps: complete ? [usage(150_000, 10_000), usage(150_000, 10_000)] : [usage(150_000, 10_000)],
144156
pricing: [
145157
...pricing,
@@ -150,11 +162,7 @@ test.each([true, false])("sidebar uses per-call tiers only with complete history
150162

151163
try {
152164
await app.renderOnce()
153-
if (!complete) {
154-
expect(app.captureCharFrame()).not.toContain("spent")
155-
return
156-
}
157-
expect(app.captureCharFrame()).toContain("≈ $1.20 spent")
165+
expect(app.captureCharFrame()).toContain(expected)
158166
} finally {
159167
app.renderer.destroy()
160168
}
@@ -220,3 +228,78 @@ test("sidebar hides the estimate without catalog pricing", async () => {
220228
app.renderer.destroy()
221229
}
222230
})
231+
232+
test("sidebar does not show parent spend inherited by a fresh fork", async () => {
233+
const raw = context({
234+
experiment: true,
235+
family: [{ model: { providerID: "provider", id: "model" }, tokens: usage(0) }],
236+
steps: [usage(300_000)],
237+
pricing: [
238+
...pricing,
239+
{ tier: { type: "context", size: 200_000 }, input: 6, output: 22.5, cache: { read: 0.6, write: 0 } },
240+
],
241+
})
242+
const get = raw.data.session.get
243+
raw.data.session.get = (id) => ({
244+
...get(id),
245+
fork: { sessionID: "parent", boundary: { type: "through", messageID: "parent-message" } },
246+
time: { created: 1, updated: 1 },
247+
})
248+
const app = await render(raw)
249+
250+
try {
251+
await app.renderOnce()
252+
expect(app.captureCharFrame()).toContain("Context")
253+
expect(app.captureCharFrame()).not.toContain("spent")
254+
} finally {
255+
app.renderer.destroy()
256+
}
257+
})
258+
259+
test.each([42, 80])("sidebar updates estimates as history and reported cost change at %i columns", async (width) => {
260+
const raw = context({
261+
experiment: true,
262+
family: [{ model: { providerID: "provider", id: "model" }, tokens: usage(300_000) }],
263+
pricing: [
264+
...pricing,
265+
{ tier: { type: "context", size: 200_000 }, input: 6, output: 22.5, cache: { read: 0.6, write: 0 } },
266+
],
267+
})
268+
const [messages, setMessages] = createSignal(raw.data.session.message.list("session"))
269+
const [cost, setCost] = createSignal(0)
270+
raw.data.session.message.list = () => messages()
271+
raw.data.session.cost = cost
272+
const app = await render(raw, width)
273+
274+
try {
275+
await app.renderOnce()
276+
expect(app.captureCharFrame()).toContain("≈ $0.90 spent")
277+
278+
setMessages([
279+
{
280+
id: "message",
281+
type: "assistant",
282+
model: { providerID: "provider", id: "model" },
283+
tokens: usage(250_000),
284+
time: { created: 0 },
285+
},
286+
])
287+
await app.renderOnce()
288+
expect(app.captureCharFrame()).toContain("≈ $1.65 spent")
289+
290+
setCost(1.23)
291+
await app.renderOnce()
292+
expect(app.captureCharFrame()).toContain("$1.23 spent")
293+
expect(app.captureCharFrame()).not.toContain("≈")
294+
295+
setCost(0)
296+
await app.renderOnce()
297+
expect(app.captureCharFrame()).toContain("≈ $1.65 spent")
298+
299+
setMessages([])
300+
await app.renderOnce()
301+
expect(app.captureCharFrame()).toContain("≈ $0.90 spent")
302+
} finally {
303+
app.renderer.destroy()
304+
}
305+
})

0 commit comments

Comments
 (0)