baoguo on magpie's Discord: every call seemed to be a new Trae conversation, billed and cached as fresh; asked for one Trae session per agent session. The plugin already sends one session_id per conversation (0.2.6, #58): the session magpie names (the agent's own header, Claude Code's x-claude-code-session-id, Codex's session_id, DSH's x-deepseek-harness-session-id, OpenCode's session; else magpie's hash of how the conversation opens), with the account, hashed. But the hash's salt was new in each process, and magpie restarts its plugins on every update of magpie or of any plugin, so every conversation going on then went on under a session_id of its own from that point. Now the salt is made once, in ~/.config/magpie/trae-session-salt (MAGPIE_TRAE_SESSION_SALT names another file), written to a temp name and hard-linked into place so processes starting together all read the one that won. A file that can't be read or made, or holds anything else, is left as it is and the process has a salt of its own, as before. The id is derived, not cached, so there is nothing to bound or expire: the same agent session gives the same id for good. Tests now write their salt in a temp folder (nonet.mjs). Run: HOME=$(mktemp -d) bun test packages/trae on mote, 243 pass, 0 fail; bun scripts/check.mjs trae ok; conversation.test.mjs on Windows (xiaomi-gpu), 10 pass. Broken by hand: a salt new in each process makes "a conversation keeps its session_id when the plugin's process starts again" fail; overwriting the file on a race makes "processes starting at once on no salt yet all take the one that was made" fail. Not tried: a real Trae account (none in this magpie), so whether Trae's prompt cache follows session_id is not measured. dsh-connect-trae's probe (docs, 11-03) saw cache_read 0 on three same-prefix requests since 10-26, which no session id would change. Claude Code subagents still share their lead's session, as magpie names them. Reported-by: baoguo
25 lines
1.2 KiB
JavaScript
25 lines
1.2 KiB
JavaScript
// Imported first by every test here: nothing a test runs reaches Trae.
|
|
// fetch and HTTP/2 go to this machine (a test's fake) or fail, so a test
|
|
// left without its fake, or work still going after it, can't send a token
|
|
// to trae.cn, mchost.guru or any other of Trae's hosts.
|
|
import http2 from "node:http2"
|
|
|
|
const local = (u) => /^https?:\/\/(127\.0\.0\.1|localhost|\[::1\])(:\d+)?(\/|$)/.test(u)
|
|
const realFetch = globalThis.fetch
|
|
globalThis.fetch = async (input, init) => {
|
|
const u = typeof input === "string" ? input : input instanceof URL ? input.href : input.url
|
|
if (!local(u)) throw new Error("a test asked " + u + " with no fake in place")
|
|
return realFetch(input, init)
|
|
}
|
|
const connect = http2.connect
|
|
http2.connect = (origin, ...rest) => {
|
|
if (!local(String(origin))) throw new Error("a test connected to " + origin)
|
|
return connect(origin, ...rest)
|
|
}
|
|
// nor does a test write this machine's session salt (saltOf): the plugin's
|
|
// salt goes in a folder of the test run's own
|
|
import { mkdtempSync } from "node:fs"
|
|
import { tmpdir } from "node:os"
|
|
import { join } from "node:path"
|
|
process.env.MAGPIE_TRAE_SESSION_SALT = join(mkdtempSync(join(tmpdir(), "trae-test-salt-")), "trae-session-salt")
|