trae 0.2.26: a token_usage naming no cache count is unknown, not a 0% miss (yetone/magpie#3991, #3979)

Trae CN users see cache read 0 / write 0 on every call since 10-28
(modeyang: 786 trae-cn calls on 10-29 all 0, 77 hits on 10-27; an
identical resend also 0). The plugin passes on what token_usage says:
cache_read_input_tokens / cache_creation_input_tokens. A token_usage
with no cache field at all (chansee97's real super_completion_query
capture in #455: {"prompt_tokens":1717,"completion_tokens":2,
"total_tokens":1719}) came out as a usage with no cache field, which
magpie can only read as a counted miss. CDX-5 (#434) reports Trae's
chat replies switched to that shape at 10-27 23:07; not verified with
raw bytes. Trae's own client (3.5.20, libai_agent.dylib) still has the
9-field TokenUsageEvent, so the fields exist in the protocol.

Now:
- no cache field named at all -> magpie_cache_unknown: true (as the
  Kiro plugin does), so magpie shows the cache as unknown;
- DeepSeek's prompt_cache_hit_tokens, cached_tokens and
  prompt/input_tokens_details.cached_tokens are read as cache reads
  (largest of them, so a 0 in one spelling never hides a hit);
- an explicit 0 stays a counted miss.

This cannot make Trae cache; if Trae no longer reports cache, the
plugin cannot know the hit rate.

Run (mote, bun 1.4.2):
- packages/trae bun test: 230 pass, 0 fail
- whole repo HOME=$(mktemp -d) bun test: 4324 pass, 9 skip, 0 fail
- bun scripts/check.mjs: exit 0
Broken by hand: the old index.mjs with the new tests fails 4 (3 new
cases + request.test's stream usage).

Not tried: a real Trae CN account (none available), so whether Trae
now omits the cache fields or sends 0 is not confirmed.
This commit is contained in:
yetone
2026-10-31 20:21:40 +08:00
parent fee318fed4
commit 8693d77c00
5 changed files with 57 additions and 4 deletions
+5 -1
View File
@@ -113,7 +113,11 @@ agent's `POST https://trae-api-cn.mchost.guru/api/agent/v3/llm_utils_chat`:
What `token_usage` says the prompt cache read and wrote
(`cache_read_input_tokens`, `cache_creation_input_tokens`) goes on under
those names, and `reasoning_tokens` as
`completion_tokens_details.reasoning_tokens`.
`completion_tokens_details.reasoning_tokens`. DeepSeek's
`prompt_cache_hit_tokens` and `cached_tokens` are read as cache reads
too. A `token_usage` that names no cache count at all goes on with
`magpie_cache_unknown: true`, so magpie shows the cache as unknown,
not as a 0% miss (0.2.26).
`done`'s `finish_reason` `length`/`max_tokens` is OpenAI's `length`.
- Queue events are ignored.
- A `{"reasoning_content": …}` object written into the text is reasoning,
+32
View File
@@ -96,3 +96,35 @@ for (const stream of [true, false]) {
})
})
}
// yetone/magpie#3979, #3991 (ferock): every Trae CN call showed cache 0.
// A token_usage that names no cache at all — Trae's own bytes, as
// plugins#455 captured one — is told to magpie as unknown
// (magpie_cache_unknown), so it shows no hit rate, not a 0% Trae never
// said; a counted 0 above stays a miss.
const UNCOUNTED = 'event: token_usage\ndata: {"prompt_tokens":1717,"completion_tokens":2,"total_tokens":1719}\n\n'
for (const stream of [true, false]) {
test(`a token_usage that names no cache says the cache is unknown (#3991, stream=${stream})`, async () => {
f = fakeTrae()
f.route(
"POST /api/agent/v3/llm_utils_chat",
() =>
new Response('event: output\ndata: {"response":"[]"}\n\n' + UNCOUNTED + 'event: done\ndata: {"done_with":"end","message":"success"}\n\n', {
headers: { "content-type": "text/event-stream" },
}),
)
const res = await ask({ model: "glm-5", stream, messages: [{ role: "user", content: "hi" }] })
const usage = stream
? (await res.text()).split("\n").filter((l) => l.startsWith("data: {")).map((l) => JSON.parse(l.slice(6))).at(-1).usage
: (await res.json()).usage
expect(usage).toEqual({ prompt_tokens: 1717, completion_tokens: 2, total_tokens: 1719, magpie_cache_unknown: true })
})
}
test("DeepSeek's own prompt_cache_hit_tokens is read as the cache read", async () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "ok" }], ["token_usage", { prompt_tokens: 3000, completion_tokens: 2, prompt_cache_hit_tokens: 2048, prompt_cache_miss_tokens: 952 }], ["done", {}]]))
const res = await ask({ model: "glm-5", messages: [{ role: "user", content: "hi" }] })
expect((await res.json()).usage).toEqual({ prompt_tokens: 3000, completion_tokens: 2, total_tokens: 3002, prompt_tokens_details: { cached_tokens: 2048 } })
})
+18 -1
View File
@@ -1479,6 +1479,22 @@ const eventName = (s) =>
// Claude (whether prompt_tokens counts them too is judged there, by
// whether it is at least as big); without them every request read as no
// cache hit (01huadalang on magpie's Discord: K3, 53 calls, all 0)
//
// A count that names no cache at all — Trae's token_usage as
// {"prompt_tokens":1717,"completion_tokens":2,"total_tokens":1719}, a shape
// plugins#455 captured from Trae — says magpie_cache_unknown, as the Kiro
// plugin does: Trae didn't say what its cache read, so magpie shows no hit
// rate rather than a 0% Trae never gave. Before, it and a count of 0 both
// went on with no cache field, which magpie can only read as a miss, so
// since 10-28 every chat showed 0 (yetone/magpie#3979, #3991; plugins#434
// says Trae stopped sending cache_read_input_tokens then). A count naming
// the cache, a 0 included, is Trae's own word and goes as before.
// DeepSeek's prompt_cache_hit_tokens, a bare cached_tokens and the
// Responses-style input_tokens_details are read too, the largest of them,
// should Trae pass a model's own usage on.
const CACHE_NAMED = ["cache_read_input_tokens", "cacheReadInputTokens", "cache_creation_input_tokens", "cacheCreationInputTokens", "prompt_cache_hit_tokens", "cached_tokens"]
const namesCache = (u) =>
CACHE_NAMED.some((k) => u[k] != null) || u.prompt_tokens_details?.cached_tokens != null || u.input_tokens_details?.cached_tokens != null
function tokensOf(u) {
if (!u || typeof u !== "object") return null
const n = (...v) => Number(v.find((x) => x != null) ?? 0) || 0
@@ -1486,9 +1502,10 @@ function tokensOf(u) {
const c = n(u.completion_tokens, u.output_tokens, u.completionTokens, u.outputTokens)
if (!p && !c) return null
const out = { prompt_tokens: p, completion_tokens: c, total_tokens: n(u.total_tokens, u.totalTokens) || p + c }
const read = n(u.prompt_tokens_details?.cached_tokens)
const read = Math.max(...[u.prompt_tokens_details?.cached_tokens, u.input_tokens_details?.cached_tokens, u.prompt_cache_hit_tokens, u.cached_tokens].map((x) => Number(x) || 0))
const write = n(u.prompt_tokens_details?.cache_write_tokens)
if (read || write) out.prompt_tokens_details = { cached_tokens: read, ...(write ? { cache_write_tokens: write } : {}) }
else if (!namesCache(u)) out.magpie_cache_unknown = true
else {
const r = n(u.cache_read_input_tokens, u.cacheReadInputTokens)
const w = n(u.cache_creation_input_tokens, u.cacheCreationInputTokens)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-trae-auth",
"version": "0.2.25",
"version": "0.2.26",
"description": "OpenCode provider plugins for Trae: Trae CN (trae.cn) and Trae Global (trae.ai), free tier included, with Trae's own browser sign-in",
"type": "module",
"main": "./index.mjs",
+1 -1
View File
@@ -196,7 +196,7 @@ test("a stream: text, reasoning, a tool block split across events, a native call
expect(calls.map((t) => [t.index, t.function.name, t.function.arguments])).toEqual([[0, "read", '{"path":"a"}'], [1, "read", '{"path":"b"}']])
expect(calls[1].id).toBe("n1")
expect(c.at(-3).choices[0].finish_reason).toBe("tool_calls")
expect(c.at(-2).usage).toEqual({ prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 })
expect(c.at(-2).usage).toEqual({ prompt_tokens: 10, completion_tokens: 5, total_tokens: 15, magpie_cache_unknown: true })
expect(c.at(-1)).toBe("[DONE]")
})