Files
plugins/packages/trae/conversation.test.mjs
T
d3711248aa trae: one conversation keeps one session_id (#58)
* trae: one conversation keeps one session_id

The plugin gave every request a fresh `randomUUID()` for both `request_id`
and `session_id` (chatBody), so each request of one agent turn reached Trae
as a different session. Trae counts a conversation by the session its
requests name: a user send that takes several steps (a tool result sent back,
a retry, another function answering) was counted as several conversations,
each a row of its own in the usage detail with no conversation under it —
which is what showed as "Prompt" where the IDE's own sessions show their
title.

`session_id` is now the conversation's, as the IDE keeps one per
conversation: the session magpie (or OpenCode) names the request by, carried
from a new `chat.headers` hook, else the chat's first user message, which
every later turn repeats. Nothing is made up when neither names one — a
made-up conversation of the request's own is the splitting this ends — and
the caller then makes a fresh id per request, as before. The id is salted, so
it says nothing of the words it was made from, and keeps the UUID shape
`randomUUID()` returned. `request_id` stays the request's own.

The title column is not this fix: the IDE's titles are generated by its agent
(`session_title_message`) and reported through its hub, which has no call on
the path this plugin takes. With the session stable, Trae has one
conversation to hang a title on, once it is sent one.

* trae: a conversation is named with the account answering it too

The id a chat's session_id is made of was the conversation alone, so when
magpie failed over, or rotated, between two Trae accounts inside one
conversation, both accounts were sent the same session_id — an id the fresh
random one before it never linked, and one Trae can read one account's
conversation as another's. The account (its uid, else its name) is now part
of what the id is made of: conversationID(req, session, who), who being the
account already resolved just above the request.

The cost, as the comment says: a conversation answered by two accounts is
counted as two again, which is what it did before it was named at all.

Two tests: two accounts of one conversation (named by magpie's session and
by the chat's own first message, and an account named by its name alone) are
two conversations to Trae, and one account's two conversations stay two.
The first fails with the account left out of the id.

---------

Co-authored-by: tearslee <16331052+tearslee@users.noreply.github.com>
Co-authored-by: 李 <oncwnuKMG1gRkPtw27nJROKWWRUM@git.weixin.qq.com>
2026-10-09 11:48:59 +08:00

121 lines
6.2 KiB
JavaScript

// One conversation keeps one session_id: Trae counts a conversation by the
// session its requests name, so a request naming a new session each time is
// counted as a conversation of its own — one user send that takes several
// steps shows up in the usage detail as several rows, each under no title.
// The session comes from magpie's (or OpenCode's) session for the request
// when it names one, else from the chat's first user message.
import "./nonet.mjs"
import { afterEach, expect, test } from "bun:test"
import { TraeCNAuthPlugin, _internal } from "./index.mjs"
import { fakeTrae, sse, signedIn } from "./fake.mjs"
let f
afterEach(() => f?.close())
const { SESSION } = _internal
// ask sends a chat as magpie's host does: the chat.headers hook is told the
// session, then the loader's fetch with what it added, and the request Trae
// was sent is read back. auth is the sign-in the loader asks for, which is
// what a group of accounts answers with, one of them per request.
async function ask(chat, { session = "", provider = "trae-cn", auth = signedIn() } = {}) {
const hooks = await TraeCNAuthPlugin({ client: {} })
const out = { headers: {} }
await hooks["chat.headers"]({ sessionID: session, model: { providerID: provider, id: "glm-5" }, provider: { info: { id: provider } } }, out)
const opts = await hooks.auth.loader(async () => auth)
const res = await opts.fetch(opts.baseURL + "/chat/completions", {
method: "POST",
headers: new Headers(out.headers),
body: JSON.stringify(chat),
})
expect(res.status).toBe(200)
return { sent: f.seen.at(-1), out }
}
const chat = (...messages) => ({ model: "glm-5", stream: true, messages })
const user = (t) => ({ role: "user", content: [{ type: "text", text: t }] })
const assistant = (t) => ({ role: "assistant", content: [{ type: "text", text: t }] })
const call = { role: "assistant", content: [], tool_calls: [{ id: "c1", type: "function", function: { name: "read", arguments: "{}" } }] }
const result = { role: "tool", tool_call_id: "c1", content: "a.go" }
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
const serve = () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "ok" }], ["done", {}]]))
}
test("one send's steps carry one session_id; each request still has its own", async () => {
serve()
const first = await ask(chat(user("fix the bug")))
const called = await ask(chat(user("fix the bug"), call))
const answered = await ask(chat(user("fix the bug"), call, result))
const ids = [first, called, answered].map((x) => x.sent.json.session_id)
expect(ids[0]).toMatch(UUID)
expect(new Set(ids).size).toBe(1)
// request_id is the request's own: each step has one of its own
const reqs = [first, called, answered].map((x) => x.sent.json.request_id)
expect(new Set(reqs).size).toBe(3)
for (const r of reqs) expect(r).toMatch(UUID)
// the session header itself goes no further
for (const x of [first, called, answered]) expect(x.sent.headers.get(SESSION)).toBe(null)
})
test("the next user message and another conversation are other sessions", async () => {
serve()
const first = await ask(chat(user("fix the bug")))
const next = await ask(chat(user("fix the bug"), assistant("ok"), user("and then?")))
expect(next.sent.json.session_id).toBe(first.sent.json.session_id)
const other = await ask(chat(user("something else")))
expect(other.sent.json.session_id).not.toBe(first.sent.json.session_id)
})
test("the session magpie names wins over the chat's own first message", async () => {
serve()
const named = await ask(chat(user("hi")), { session: "ses_1" })
const again = await ask(chat(user("hi"), assistant("ok"), user("more")), { session: "ses_1" })
expect(again.sent.json.session_id).toBe(named.sent.json.session_id)
// the same words under another session are another conversation
const other = await ask(chat(user("hi")), { session: "ses_2" })
expect(other.sent.json.session_id).not.toBe(named.sent.json.session_id)
// and the session header is not sent to Trae
expect(named.sent.headers.get(SESSION)).toBe(null)
})
test("nothing names a conversation: a fresh session per request, as before", async () => {
serve()
const one = await ask(chat({ role: "system", content: "be brief" }))
const two = await ask(chat({ role: "system", content: "be brief" }))
expect(one.sent.json.session_id).not.toBe(two.sent.json.session_id)
})
test("two accounts of one conversation are two conversations to Trae", async () => {
serve()
const named = await ask(chat(user("hi")), { session: "ses_1", auth: signedIn() })
const other = await ask(chat(user("hi")), { session: "ses_1", auth: signedIn({ uid: "u-2", accountId: "Bob" }) })
expect(other.sent.json.session_id).not.toBe(named.sent.json.session_id)
// each account's own conversation keeps its own id, so failing over
// back mid-conversation doesn't split it
const again = await ask(chat(user("hi"), assistant("ok"), user("more")), { session: "ses_1", auth: signedIn() })
expect(again.sent.json.session_id).toBe(named.sent.json.session_id)
// and the same holds when the conversation is named by the chat itself
const first = await ask(chat(user("fix the bug")), { auth: signedIn() })
const onOther = await ask(chat(user("fix the bug")), { auth: signedIn({ uid: "u-2", accountId: "Bob" }) })
expect(onOther.sent.json.session_id).not.toBe(first.sent.json.session_id)
// an account named by its name alone (no uid) is one of its own too
const namedOnly = await ask(chat(user("hi")), { session: "ses_1", auth: signedIn({ uid: "", accountId: "Bob" }) })
expect(namedOnly.sent.json.session_id).not.toBe(named.sent.json.session_id)
})
test("one account's two conversations stay two", async () => {
serve()
const one = await ask(chat(user("fix the bug")), { session: "ses_1" })
const two = await ask(chat(user("fix the bug")), { session: "ses_2" })
expect(two.sent.json.session_id).not.toBe(one.sent.json.session_id)
})
test("the hook leaves other providers' requests alone", async () => {
const hooks = await TraeCNAuthPlugin({ client: {} })
const out = { headers: {} }
await hooks["chat.headers"]({ sessionID: "ses_1", model: { providerID: "trae-global" }, provider: { info: { id: "trae-global" } } }, out)
expect(out.headers).toEqual({})
})