autoclaw 0.2.3: usage-path signing remembers each sign-in, not the provider (#254)

With signinOnUsage on, maybeSignin remembered a day's success under the
provider id, so after the first AutoClaw account signed in, every other
account of that provider was skipped until the next day. #254's reporter
has four sign-ins under two default display names and saw exactly one
extra account signed this way. The key is now the sign-in's uid, or a
hash of its refresh token for an old account that has none, for both the
refresh and the usage path.

magpie's own daily check-in (the native checkin hook) skipping same-name
accounts is the main part of #254 and is fixed in magpie itself.

Run:
- bun test packages/autoclaw: 26 pass, 0 fail. The new test drives usage()
  for four accounts sharing one display name (two with uid, two without);
  on the old index.mjs it fails at the second account ("今日未签(+400)").
- HOME=$(mktemp -d) bun test (whole repo): 1865 pass, 1 fail + errors in
  packages/devin's CLI tests (30s timeouts) and a trae stream connection
  reset, at load average ~168; neither package is touched here. Rerun of
  devin+trae alone: 252 pass, 1 devin timeout.
- bun scripts/check.mjs: all packages ok.
This commit is contained in:
yetone
2026-10-20 16:26:17 +08:00
parent bb688ea81a
commit 57fc069410
3 changed files with 57 additions and 4 deletions
+15 -3
View File
@@ -387,6 +387,18 @@ function checkinOutcome(r) {
/** 本地去重键(成功才记;按小时允许重试)。 */
const signinTriedLocal = new Map(); // key → hour-stamp of last SUCCESSFUL signin
/**
* 补签去重键:每个登录一个(#254)。显示名可以重复(AutoClaw 默认昵称改不了),
* 所以按 uid,没有 uid 的旧账号按 refresh token 的哈希;只用供应商 id 会让
* 同一供应商下的第二个账号当天永远不补签。
*/
function signinAccountKey(providerId, auth) {
const uid = String(auth?.uid ?? "").trim();
if (uid) return `${providerId}|uid:${uid}`;
const tok = bareToken(String(auth?.refresh ?? auth?.access ?? ""));
return `${providerId}|tok:${createHash("sha256").update(tok).digest("hex").slice(0, 16)}`;
}
/**
* 签到配置合并:插件 options(plugins.json 的 options 字段)优先,
* 其次配置文件 ~/.config/magpie/autoclaw.json(热读取,GUI 没有选项入口时的保底通道):
@@ -555,7 +567,7 @@ function makeAutoclawPlugin({ providerId, providerName, homeHost, loginMethod })
async refresh(auth) {
const t = await refreshTokens(bareToken(auth.refresh), [homeHost]);
await maybeSignin(t.access, homeHost, options, "refresh", providerId);
await maybeSignin(t.access, homeHost, options, "refresh", signinAccountKey(providerId, auth));
// 存量账号就地正名:登录响应缺昵称的旧账号(GUI 显示「已登录」),
// 刷新时补拉 user-profile。magpie 的 refresh 返回字段会合并进账号。
const old = String(auth.accountId ?? auth.email ?? "");
@@ -658,7 +670,7 @@ function makeAutoclawPlugin({ providerId, providerName, homeHost, loginMethod })
// 旧链路自动补签(默认关,需 signinOnUsage=true):查额度即触发。
// 放在状态查询之后:未签才补,补完重查一次状态展示。
if (signin && signin.status !== "completed" && (cfg.signin && cfg.signinOnUsage)) {
await maybeSignin(token, hosts[0], options, "usage", providerId);
await maybeSignin(token, hosts[0], options, "usage", signinAccountKey(providerId, auth));
}
const signinFinal = signin?.status === "completed"
? signin
@@ -1209,7 +1221,7 @@ export const AutoclawCnAuthPlugin = makeAutoclawPlugin({
export const _internal = {
makePluginForTest: makeAutoclawPlugin,
md5Hex, jwtClaim, tokenFields, codeValue, bizHeaders, bareToken, newDeviceId,
dailySignin, signinStatus, maybeSignin, signinConfig, checkinOutcome, refreshTokens,
dailySignin, signinStatus, maybeSignin, signinAccountKey, signinConfig, checkinOutcome, refreshTokens,
fetchUserProfile, accountDisplayName, startLocalOAuth,
FLAGGED, unflagged, unflag, autoclawRewriteBody, explained, OFFICIAL_SYSTEM_PROMPT, REFUSED_HINT,
guidePageHtml, callbackPageHtml,
+41
View File
@@ -570,3 +570,44 @@ test("CN passes SSE through and marks 401 expired", async () => {
expect(expired.headers.get("X-Magpie-Sign-In")).toBe("expired")
} finally { globalThis.fetch = realFetch }
})
// #254: two sign-ins can share AutoClaw's default display name. The
// usage-path signing (signinOnUsage) is remembered per sign-in, by uid or
// by the refresh token when an old account has none, never per provider,
// so the second account of a provider is still signed in that day.
test("signinOnUsage signs every account, also two with the same name", async () => {
const realFetch = globalThis.fetch
const signed = []
const done = new Set()
globalThis.fetch = async (input, init) => {
const url = String(input)
const who = new Headers(init?.headers).get("Authorization")
if (url.includes("autoclaw-task-complete")) {
signed.push(who)
done.add(who)
return Response.json({ code: 0, data: { success: true, reward_points: 400 } })
}
if (url.includes("autoclaw-task-list")) {
return Response.json({ code: 0, data: [{ task_id: "daily_signin", status: done.has(who) ? "completed" : "pending", reward_points: 400 }] })
}
return Response.json({ code: 0, data: { total_balance: 1000, wallets: [] } })
}
try {
const hooks = await AutoclawAuthPlugin({ client: { auth: { set: async () => {} } } }, { signin: true, signinOnUsage: true })
const accounts = [
{ type: "oauth", access: "a1", refresh: "r1", accountId: "AutoClaw用户", uid: "u1" },
{ type: "oauth", access: "a2", refresh: "r2", accountId: "AutoClaw用户", uid: "u2" },
{ type: "oauth", access: "a3", refresh: "r3", accountId: "AutoClaw用户" },
{ type: "oauth", access: "a4", refresh: "r4", accountId: "AutoClaw用户" },
]
for (const a of accounts) {
const u = await hooks.auth.usage(async () => a)
expect(u.windows.find((w) => w.name === "每日签到").display).toBe("今日已签 ✓ +400")
}
expect(signed).toEqual(["Bearer a1", "Bearer a2", "Bearer a3", "Bearer a4"])
expect(_internal.signinAccountKey("autoclaw", accounts[0])).toBe("autoclaw|uid:u1")
expect(_internal.signinAccountKey("autoclaw", accounts[2])).not.toBe(_internal.signinAccountKey("autoclaw", accounts[3]))
} finally {
globalThis.fetch = realFetch
}
})
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-autoclaw-auth",
"version": "0.2.2",
"version": "0.2.3",
"description": "OpenCode provider plugin for AutoClaw (Zhipu OpenClaw): oversea web sign-in (Google/z.ai) and CN phone SMS, multi-account pool, credit balance and daily check-in",
"type": "module",
"main": "./index.mjs",