diff --git a/packages/trae/README.md b/packages/trae/README.md index 6230224..55f913a 100644 --- a/packages/trae/README.md +++ b/packages/trae/README.md @@ -448,6 +448,18 @@ answered it 9074 even with the client's headers. changed or rotated to get round a 9095 or a 9074, and no account is given another account's device or its own client's device unless a client is signed in as that account. +- **Which device, said (0.2.25):** a 9074 or 9095 message ends with + the device the claim went on: this machine's Trae CN client's, signed in + as the account, or magpie's own for it, with how many client profiles + were found and that none is signed in as the account. A client device + that another profile, signed in as another account, also has is said + too: both accounts check in on one device, which Trae takes once a day. + No uid or device id is named. On plugins#436 two accounts shared one + machine whose two profiles (`Trae CN`, `TRAE SOLO CN`) were both the + first account's; it checked in and the second got 9074, which took + running `clientDevice` by hand to explain. A profile started with its + own `--user-data-dir` is still not read: whether TRAE CN keeps a + separate, registered device there is not known yet (plugins#436). - **A claim's code 0 is not the answer (0.2.22):** Trae answers a claim code 0 for one that registered nothing, so the status is read back after every claim and only a day that is in is reported as `claimed`. diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index 356daf9..605d151 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -871,6 +871,15 @@ function clientStoragePaths(platform, home, env) { // seen (and one unreadable file hid every profile after it). async function clientDevice(uid) { if (!uid) return undefined + return (await clientProfiles()).find((p) => String(p.uid) === String(uid))?.device +} + +// clientProfiles are this machine's Trae CN client profiles that can be +// made out, in ownClient.paths' order: the account each is signed in as +// and its one device. A file that isn't there, can't be read or parsed, +// names no single device or has a sign-in that doesn't decode is left out. +async function clientProfiles() { + const out = [] for (const p of ownClient.paths()) { let text try { @@ -888,12 +897,34 @@ async function clientDevice(uid) { const ids = Object.keys(doc ?? {}).filter((k) => k.startsWith(DC_KEY)).map((k) => k.slice(DC_KEY.length)) if (ids.length !== 1 || !/^\d{8,20}$/.test(ids[0])) continue const who = clientAuth(doc[AUTH_KEY])?.userId - if (who != null && String(who) === String(uid)) return ids[0] + if (who != null && String(who) !== "") out.push({ uid: String(who), device: ids[0] }) } catch { continue // a sign-in that doesn't decode: the next profile may be } } - return undefined + return out +} + +// deviceNote says which device a Trae CN check-in went on, for the message +// of a claim Trae refused (9074) or found already in (9095): the device of +// a client on this machine signed in as uid, or magpie's own for the +// account and why — how many client profiles were found, and that none is +// signed in as it. It names no uid and no device id. A client's device +// that another profile, signed in as another account, also has is said: +// both accounts check in on one device, and Trae takes one a day +// (plugins#436). It only states what was read; it promises no fix. +async function deviceNote(uid) { + const all = await clientProfiles() + const mine = all.find((p) => String(p.uid) === String(uid ?? "")) + if (mine) { + const shared = all.some((p) => p.device === mine.device && String(p.uid) !== String(uid)) + return shared + ? "sent on this machine's Trae CN client's device, signed in as this account — another profile here, signed in as another account, has the same device" + : "sent on this machine's Trae CN client's device, signed in as this account" + } + if (!all.length) return "sent on magpie's own device for this account: no Trae CN client profile was found on this machine" + const n = all.length + return `sent on magpie's own device for this account: ${n} Trae CN client profile${n === 1 ? " is" : "s are"} on this machine, none signed in as this account` } // clientAuth is the client's sign-in envelope, as stored or decoded @@ -3078,7 +3109,7 @@ const makePlugin = (site) => async ({ client }) => { const ce = errorOf(answer) if (claim.status === 401 || lapsedCode(ce.code, site)) return { outcome: "failed", message: ce.message || `${a0.name || a0.uid}: Trae's sign-in has expired` } // 9095: this device is in for today, whoever took it — the day's answer - if (String(ce.code) === "9095") return { outcome: "ineligible", message: ce.message || "this device has checked in today" } + if (String(ce.code) === "9095") return { outcome: "ineligible", message: `${ce.message || "this device has checked in today"} (${await deviceNote(a0.uid)})` } // 9074: Trae did not take this claim for this account's device. Its own // words say the pool is busy, which measurements do not bear out: the // same account, token and headers were taken on a 16-digit id where a @@ -3092,8 +3123,13 @@ const makePlugin = (site) => async ({ client }) => { // it in. Nothing is said of signing one in: it is the one thing seen // to change a 9074, but it hasn't been tried on a real account, and // on a claim that already rode a client's device it is what the user - // did — the advice would only send them back to do it again. - if (String(ce.code) === "9074") return { outcome: "ineligible", message: `${ce.message} (9074) — Trae CN did not take this check-in for this account's device` } + // did — the advice would only send them back to do it again. What it + // does say, after both 9074 and 9095, is which device the claim went + // on and why (deviceNote): moxuanchen (plugins#436) had to run + // clientDevice by hand to learn that the account Trae CN took (A, 100 + // credits) rode the client's device and the one it refused 9074 (B) + // rode magpie's own, since both of that machine's profiles were A's. + if (String(ce.code) === "9074") return { outcome: "ineligible", message: `${ce.message} (9074) — Trae CN did not take this check-in for this account's device (${await deviceNote(a0.uid)})` } if (claim.status !== 200 || (ce.code && String(ce.code) !== "0")) return { outcome: "failed", message: ce.message || statusLine(claim.status) } if (!answer || typeof answer !== "object" || Array.isArray(answer)) return { outcome: "failed", message: `check-in claim: unreadable reply ${claimText.trim().slice(0, 120) || "(empty)"}` } // a claim's own code 0 is not proof the day is in: Trae answers it for @@ -3545,4 +3581,4 @@ const makePlugin = (site) => async ({ client }) => { export const TraeCNAuthPlugin = makePlugin(SITES["trae-cn"]) export const TraeGlobalAuthPlugin = makePlugin(SITES["trae-global"]) -export const _internal = { ownClient, clientStoragePaths, clientDevice, clientDecode, thisMachine, traeHost, accountOf, HOSTS: SITES["trae-cn"].hosts, MODELS: SITES["trae-cn"].models, TextTools, NativeCalls, looseJSON, glmCall, nameCall, toolNamed, unwrapped, traeMessages, chatBody, nativeTools, moonshotSchema, credits, dollarUsageOf, whenOf, newDevice, ugDevice, proofOf, SITES, SESSION, conversationID, LEVELS, levelsOf, variantsOf, effortOf } +export const _internal = { ownClient, clientStoragePaths, clientDevice, clientProfiles, deviceNote, clientDecode, thisMachine, traeHost, accountOf, HOSTS: SITES["trae-cn"].hosts, MODELS: SITES["trae-cn"].models, TextTools, NativeCalls, looseJSON, glmCall, nameCall, toolNamed, unwrapped, traeMessages, chatBody, nativeTools, moonshotSchema, credits, dollarUsageOf, whenOf, newDevice, ugDevice, proofOf, SITES, SESSION, conversationID, LEVELS, levelsOf, variantsOf, effortOf } diff --git a/packages/trae/native-checkin.test.mjs b/packages/trae/native-checkin.test.mjs index cbe70b3..3c3763f 100644 --- a/packages/trae/native-checkin.test.mjs +++ b/packages/trae/native-checkin.test.mjs @@ -93,7 +93,10 @@ test("the device's already-checked answer is ineligible", async () => { f = fakeTrae() f.route("POST " + STATUS, () => json({ code: 0, enable: true, checked_in: false })) f.route("POST " + CLAIM, () => json({ code: 9095, message: "this device has checked in today", credits: 100 })) - expect(await checkin()).toEqual({ outcome: "ineligible", message: "this device has checked in today" }) + // which device it was is said too (plugins#436): no client here, so + // magpie's own + const out = await checkin() + expect(out).toEqual({ outcome: "ineligible", message: "this device has checked in today (sent on magpie's own device for this account: no Trae CN client profile was found on this machine)" }) }) // 9074 is not "the pool is busy": the same account, token and headers were @@ -115,8 +118,9 @@ test("Trae's 9074 on the claim says the device is what Trae refused, and promise expect(out.message).toContain("当前参与用户太多") expect(out.message).toContain("9074") expect(out.message).toContain("device") - expect(out.message).not.toContain("Trae CN client") - expect(out.message).not.toContain("check in in the Trae CN app") + // it says which device it went on, and advises nothing (plugins#436) + expect(out.message).toContain("sent on magpie's own device for this account") + expect(out.message).not.toMatch(/sign (it )?in|log ?in|check in in the Trae CN app/i) // the check-in rides ugDevice like every other ug page: the 19-digit // sign-in device becomes the account's own 16-digit shape (#194) expect(f.seen.map((r) => r.headers.get("x-device-id"))).toEqual(["2866487253502651", "2866487253502651"]) diff --git a/packages/trae/ownclient.test.mjs b/packages/trae/ownclient.test.mjs index c533dc6..6cf8e93 100644 --- a/packages/trae/ownclient.test.mjs +++ b/packages/trae/ownclient.test.mjs @@ -4,7 +4,7 @@ import "./nonet.mjs" import { afterEach, beforeEach, expect, test } from "bun:test" import { createCipheriv, createHash, randomBytes } from "node:crypto" -import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs" +import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { join, sep } from "node:path" import { TraeCNAuthPlugin, _internal } from "./index.mjs" @@ -243,3 +243,63 @@ test("this machine's OS as the client names it", () => { expect(_internal.thisMachine("darwin")).toEqual({ type: "mac", os: expect.stringMatching(/^macOS \d/) }) expect(_internal.thisMachine("linux").type).toBe("linux") }) + +// plugins#436 (moxuanchen, Windows 11, TRAE CN 3.4.4, plugin 0.2.24): two +// accounts, A and B. Both of that machine's profiles, %APPDATA%\Trae CN and +// %APPDATA%\TRAE SOLO CN, were signed in as A, with the same device. A +// checked in (100 credits); B got 9074, and the reporter had to run +// clientDevice by hand to learn that B's claim had gone on magpie's own +// device. The message says which device a refused claim went on, and why. +async function refused(uid, answer = { code: 9074, message: "当前参与用户太多,请稍后再试" }) { + f?.close() + f = fakeTrae() + f.route("POST /trae/api/v2/ug/checkin_credits/status", () => json({ code: 0, enable: true, checked_in: false })) + f.route("POST /trae/api/v2/ug/checkin_credits/claim", () => json(answer)) + const hooks = await TraeCNAuthPlugin({ client: {} }) + return hooks.auth.checkin(async () => signedIn({ uid })) +} + +function reporterProfiles() { + const cn = join(dir, "Trae CN", "storage.json") + const solo = join(dir, "TRAE SOLO CN", "storage.json") + for (const p of [cn, solo]) mkdirSync(join(p, ".."), { recursive: true }) + _internal.ownClient.paths = () => [cn, solo] + clientFile(cn, "uid-a", { dcs: [DC] }) + clientFile(solo, "uid-a", { dcs: [DC] }) +} + +test("B's 9074 says it went on magpie's own device, since both profiles here are A's (#436)", async () => { + reporterProfiles() + const out = await refused("uid-b") + expect(out).toEqual({ + outcome: "ineligible", + message: "当前参与用户太多,请稍后再试 (9074) — Trae CN did not take this check-in for this account's device (sent on magpie's own device for this account: 2 Trae CN client profiles are on this machine, none signed in as this account)", + }) + // it names neither uid nor either device + expect(out.message).not.toContain("uid-a") + expect(out.message).not.toContain(DC) + expect(f.seen.at(-1).headers.get("x-device-id")).toBe(_internal.ugDevice(signedIn({ uid: "uid-b" }))) +}) + +test("A's refusal says it went on the client's device; one account in two profiles is no conflict (#436)", async () => { + reporterProfiles() + const out = await refused("uid-a", { code: 9095, message: "this device has checked in today" }) + expect(out).toEqual({ outcome: "ineligible", message: "this device has checked in today (sent on this machine's Trae CN client's device, signed in as this account)" }) + expect(f.seen.at(-1).headers.get("x-device-id")).toBe(DC) +}) + +test("a client device another account's profile also has is named as shared (#436)", async () => { + const other = join(dir, "other.json") + _internal.ownClient.paths = () => [file, other] + clientFile(file, "uid-a", { dcs: [DC] }) + clientFile(other, "uid-b", { dcs: [DC] }) + const out = await refused("uid-b", { code: 9095, message: "this device has checked in today" }) + expect(out.message).toContain("another profile here, signed in as another account, has the same device") + expect(f.seen.at(-1).headers.get("x-device-id")).toBe(DC) +}) + +test("one profile, signed in as someone else, is counted in the singular (#436)", async () => { + client("uid-a") + const out = await refused("uid-b") + expect(out.message).toContain("(sent on magpie's own device for this account: 1 Trae CN client profile is on this machine, none signed in as this account)") +}) diff --git a/packages/trae/package.json b/packages/trae/package.json index 5930ace..a13610f 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.2.24", + "version": "0.2.25", "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",