From be7b03e090eb1935736cc34a37f569364eba501f Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 16 Oct 2026 18:14:34 +0800 Subject: [PATCH] autoclaw: the refresh test reads the expiry between the clock before and after, not the clock again "refreshTokens orders hosts by the token's source_id and falls back on 400002" compared the expiry refreshTokens computed (Date.now() + 3600s - 10min) with Date.now() read again after it returned. On a millisecond boundary between the two reads it is off by one: publish run 38885546686 (devin 0.1.20's push, 29adee2) went red on ubuntu with expires 1792148517446 against 1792148517447, and no package published. The test now takes the clock before and after the call and checks the expiry lies between them. Run: with a 3ms busy-wait injected after refreshTokens returns, the old test is red (0 pass, 1 fail) and the new one green. bun test in packages/autoclaw under a mktemp HOME: 9 pass. bun scripts/check.mjs autoclaw: ok. Test only; the package's version is unchanged. --- packages/autoclaw/index.test.mjs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/autoclaw/index.test.mjs b/packages/autoclaw/index.test.mjs index d98aa65..ba11b23 100644 --- a/packages/autoclaw/index.test.mjs +++ b/packages/autoclaw/index.test.mjs @@ -132,13 +132,20 @@ test("refreshTokens orders hosts by the token's source_id and falls back on 4000 } try { // An agent (SMS) token tries the CN host first, and its /refresh answers 400002 + // the expiry is read off the clock while refreshTokens runs: it is + // between the clock before and after, not the clock read again later + const before = Date.now() const out = await refreshTokens(jwt({ source_id: "agent", device_id: "dev-1" }), []) + const after = Date.now() expect(requests.map((r) => `${new URL(r.url).origin}${r.url.pathname}`)).toEqual([ "https://autoglm-api.zhipuai.cn/userapi/v1/refresh", "https://autoglm-api.zhipuai.cn/userapi/v1/agent-refresh", ]) expect(requests[0].body.device_id).toBe("dev-1") - expect(out).toEqual({ access: "a2", refresh: "r2", expires: Date.now() + 3600_000 - 600_000 }) + const { expires, ...tokens } = out + expect(tokens).toEqual({ access: "a2", refresh: "r2" }) + expect(expires).toBeGreaterThanOrEqual(before + 3600_000 - 600_000) + expect(expires).toBeLessThanOrEqual(after + 3600_000 - 600_000) } finally { globalThis.fetch = realFetch }