Skip to content

WorkBuddy、CodeBuddy 桌面端增强助手:签到猫猫旅行成长计划查询、多账号独立备份、点切即用;免打扰模式让 AI 无人值守跑长任务;跨账号会话迁移、异常中断自动续接;暂存/快捷提示词;毛玻璃主题与更多实用功能。

fix: refresh all account tokens when panel opens

[email protected]acc9dc15ef14
diff --git a/scripts/daemon.js b/scripts/daemon.js
index 6c60b90..59720d3 100644
--- a/scripts/daemon.js
+++ b/scripts/daemon.js
@@ -487,8 +487,9 @@ const primaryAccountStore = createPrimaryAccountStore(DATA_DIR, (uid) => fs.exis
// 时 launcher 发现不到已在监听的 daemon,误选备用 UI 端口后失败退出。
// launcher 的 httpGet 预算同步对齐到 HTTP_GET_TIMEOUT_MS;Windows app.asar
// 页面归属与 CDP 目标等待窗口也同步修复。
-const DAEMON_VERSION = '1.2.30';
-const DAEMON_BUILD_ID = 'release-1.2.30-20261012-windows-startup-followup';
+// 1.2.31:打开账号面板时按日保活全部备份账号 token,刷新完成后更新账号快照。
+const DAEMON_VERSION = '1.2.31';
+const DAEMON_BUILD_ID = 'release-1.2.31-20261012-panel-token-refresh';
const usageReporter = createUsageReporter({ profile: PROFILE.id, version: DAEMON_VERSION });
configureAutomationRuntime({version: DAEMON_VERSION, profileId: PROFILE.id, platform: process.platform});
const automationDiscovery = createAutomationDiscovery({
@@ -4130,6 +4131,7 @@ function saveCheckinCache(cache) {
// 加密备份的刷新结果只在 daemon 生命周期内缓存,绝不把解密后的 token 写回磁盘。
const backupAuthRuntimeCache = new Map();
+let accountTokenRefreshInFlight = null;
/** 刷新备份账号凭证:临期惰性刷新,或距上次刷新超过一天时执行保活。 */
async function refreshAccountBackupToken(uid, options = {}) {
@@ -4182,6 +4184,44 @@ async function refreshAccountBackupToken(uid, options = {}) {
return { root: nextRoot, refreshed: true, persisted: true };
}
+/**
+ * 打开账号面板时按日保活全部备份账号,避免只有切换到某个账号后它才获得一次刷新机会。
+ * 刷新请求限并发,并复用进行中的批次,避免快速开关面板造成 refresh token 风暴。
+ */
+async function refreshAllAccountTokensOnPanelOpen() {
+ if (accountTokenRefreshInFlight) return accountTokenRefreshInFlight;
+ const task = (async () => {
+ const accounts = listAccounts(DATA_DIR);
+ const results = new Array(accounts.length);
+ let next = 0;
+ const worker = async () => {
+ while (next < accounts.length) {
+ const index = next++;
+ const uid = accounts[index].uid;
+ try {
+ const result = await refreshAccountBackupToken(uid, { dailyKeepalive: true });
+ results[index] = { refreshed: result.refreshed === true, skipped: result.skipped === true, failed: !!result.error };
+ } catch (_) {
+ results[index] = { refreshed: false, skipped: false, failed: true };
+ }
+ }
+ };
+ await Promise.all(Array.from({ length: Math.min(3, accounts.length) }, worker));
+ return {
+ ok: true,
+ total: results.length,
+ refreshed: results.filter((item) => item && item.refreshed).length,
+ skipped: results.filter((item) => item && item.skipped).length,
+ failed: results.filter((item) => item && item.failed).length,
+ };
+ })();
+ const wrapped = task.finally(() => {
+ if (accountTokenRefreshInFlight === wrapped) accountTokenRefreshInFlight = null;
+ });
+ accountTokenRefreshInFlight = wrapped;
+ return wrapped;
+}
+
/**
* 用指定账号 accessToken 调用签到接口(多域名兜底)。
* 只有明确 code=0,或 code=10001 且文案明确表示已签到/已领取,才视为成功。
@@ -10320,6 +10360,15 @@ function handleApi(req, res) {
});
}
+ if (req.method === 'POST' && p === '/api/accounts/refresh-tokens') {
+ return refreshAllAccountTokensOnPanelOpen()
+ .then((result) => json(res, 200, result))
+ .catch((error) => {
+ log('[token-refresh] 打开账号面板时刷新全部账号失败: ' + redactDiagnosticText(error && error.message || error, 300));
+ return json(res, 500, { ok: false, error: '账号 token 刷新失败' });
+ });
+ }
+
if (req.method === 'GET' && p === '/api/accounts') {
const accounts = listAccounts(DATA_DIR);
const checkinAutomationEnabled = readAutomations(DATA_DIR).some(task => task.enabled && stepsContainCheckin(task.steps));
diff --git a/scripts/inject.js b/scripts/inject.js
index f21d6a7..e2325ec 100644
--- a/scripts/inject.js
+++ b/scripts/inject.js
@@ -11572,7 +11572,20 @@ if (typeof window !== 'undefined' && typeof document !== 'undefined') {
else if (newlyOpened && typeof preloadAutomationDiscovery === 'function') preloadAutomationDiscovery().catch(function () {});
if (newlyOpened && !(options && options.automation)) api('/api/automations/events', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ type: 'panelOpened' }) }).catch(function () {});
if (newlyOpened) state.dailyProgressForceNext = true;
- refresh();
+ var panelRefresh = refresh();
+ if (newlyOpened && CAPS.accounts) {
+ // Refresh every backup account after the initial snapshot. The daemon
+ // applies its daily keepalive/near-expiry policy and coalesces repeats;
+ // a second snapshot updates displayed expiry times after rotated tokens.
+ Promise.resolve(panelRefresh).then(function () {
+ return api('/api/accounts/refresh-tokens', {
+ method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}',
+ });
+ }).then(function (result) {
+ if (!alive || !state.open || !result || Number(result.refreshed) <= 0) return;
+ refresh();
+ }).catch(function () {});
+ }
checkForUpdate(); // 打开面板即检测更新(每次打开都强制查一次新版本)
try { acCheckPromptOnOpen(); } catch (e) {} // 每次打开面板:指令块丢失则请求 daemon 补写(无 toast)
try { syncSessionModule(); } catch (e) {} // 每次打开面板刷新会话模块(开关+快捷短语,含外部修改)
diff --git a/test/daemon-status-nonblocking.test.js b/test/daemon-status-nonblocking.test.js
index 4ab6375..5953433 100644
--- a/test/daemon-status-nonblocking.test.js
+++ b/test/daemon-status-nonblocking.test.js
@@ -114,7 +114,7 @@ test('daemon behavior change ships a version and build bump so stale daemons are
const buildId = daemonSource.match(/const DAEMON_BUILD_ID = '([^']+)'/);
assert.ok(version && buildId, 'DAEMON_VERSION/DAEMON_BUILD_ID not found');
assert.notEqual(version[1], '1.2.28', 'DAEMON_VERSION must be incremented for a daemon behavior change');
- assert.match(buildId[1], /^release-1\.2\.30-/, 'DAEMON_BUILD_ID must be rewritten for the new build');
+ assert.match(buildId[1], /^release-1\.2\.31-/, 'DAEMON_BUILD_ID must be rewritten for the new build');
});
test('launcher httpGet uses a budget aligned with the daemon probe cost', () => {
diff --git a/test/token-refresh-panel.test.js b/test/token-refresh-panel.test.js
new file mode 100644
index 0000000..4ec1846
--- /dev/null
+++ b/test/token-refresh-panel.test.js
@@ -0,0 +1,28 @@
+'use strict';
+
+const assert = require('node:assert/strict');
+const fs = require('node:fs');
+const path = require('node:path');
+const test = require('node:test');
+
+const daemon = fs.readFileSync(path.join(__dirname, '..', 'scripts', 'daemon.js'), 'utf8');
+const inject = fs.readFileSync(path.join(__dirname, '..', 'scripts', 'inject.js'), 'utf8');
+
+test('panel token refresh endpoint keeps every backup account alive', () => {
+ assert.match(daemon, /async function refreshAllAccountTokensOnPanelOpen\(\)/);
+ assert.match(daemon, /refreshAccountBackupToken\(uid, \{ dailyKeepalive: true \}\)/);
+ const routeStart = daemon.indexOf("p === '/api/accounts/refresh-tokens'");
+ const routeEnd = daemon.indexOf("if (req.method === 'GET' && p === '/api/accounts')", routeStart);
+ assert.ok(routeStart >= 0 && routeEnd > routeStart);
+ assert.match(daemon.slice(routeStart, routeEnd), /refreshAllAccountTokensOnPanelOpen\(\)/);
+});
+
+test('opening the account panel triggers token refresh and then refreshes its snapshot', () => {
+ const start = inject.indexOf(' function setOpen(open, options)');
+ const end = inject.indexOf(' function setupFabDrag()', start);
+ assert.ok(start >= 0 && end > start);
+ const body = inject.slice(start, end);
+ assert.match(body, /\/api\/accounts\/refresh-tokens/);
+ assert.match(body, /dailyKeepalive|refresh-tokens/);
+ assert.match(body, /\.then\(function \(result\)/);
+});