Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(recovery): 缩短离线后的认证与设备链路恢复等待

Dash4b6810406bdb
diff --git a/apps/desktop/src/main/__tests__/authCredentialStoreHealth.contract.test.ts b/apps/desktop/src/main/__tests__/authCredentialStoreHealth.contract.test.ts
index 1109a71dff..42cd2c6201 100644
--- a/apps/desktop/src/main/__tests__/authCredentialStoreHealth.contract.test.ts
+++ b/apps/desktop/src/main/__tests__/authCredentialStoreHealth.contract.test.ts
@@ -46,7 +46,7 @@ describe("authManager credential-store escalation wiring", () => {
it('transient-unreadable 分支喂失败计数并在翻转时广播,但仍保持瞬时语义', () => {
- const start = authSource.indexOf('export async function refresh(): Promise<boolean> {');
+ const start = authSource.indexOf('export async function refresh(');
const end = authSource.indexOf('const diskTokenChangedBeforeRefresh', start);
const body = authSource.slice(start, end);
@@ -68,7 +68,7 @@ describe("authManager credential-store escalation wiring", () => {
it('成功读到持久会话时喂恢复并在翻转时广播', () => {
- const start = authSource.indexOf('export async function refresh(): Promise<boolean> {');
+ const start = authSource.indexOf('export async function refresh(');
const end = authSource.indexOf('const diskTokenChangedBeforeRefresh', start);
const body = authSource.slice(start, end);
expect(body).toContain(
diff --git a/apps/desktop/src/main/__tests__/authLoginFlowReset.contract.test.ts b/apps/desktop/src/main/__tests__/authLoginFlowReset.contract.test.ts
index 6a67124fba..a5583f6482 100644
--- a/apps/desktop/src/main/__tests__/authLoginFlowReset.contract.test.ts
+++ b/apps/desktop/src/main/__tests__/authLoginFlowReset.contract.test.ts
@@ -823,7 +823,7 @@ describe('auth login-flow reset', () => {
);
expect(coldBody).not.toContain('rememberResourceSession(refreshData');
- const refreshStart = source.indexOf('export async function refresh(): Promise<boolean> {');
+ const refreshStart = source.indexOf('export async function refresh(');
const refreshEnd = source.indexOf('\n}\n\nexport async function logout()', refreshStart);
const refreshBody = source.slice(refreshStart, refreshEnd);
const runtimePolicyGuard = refreshBody.indexOf('!canRestoreAuthSessionForMembership(');
@@ -890,7 +890,7 @@ describe('auth login-flow reset', () => {
});
it('drops a runtime refresh result after logout or a newer login changes auth generation', () => {
- const refreshStart = source.indexOf('export async function refresh(): Promise<boolean> {');
+ const refreshStart = source.indexOf('export async function refresh(');
const refreshEnd = source.indexOf('\n}\n\nexport async function logout()', refreshStart);
const refreshBody = source.slice(refreshStart, refreshEnd);
expect(refreshBody).toContain('const refreshEpoch = authStateEpoch;');
@@ -939,7 +939,7 @@ describe('auth login-flow reset', () => {
});
it('reconnects realm-bound main clients after a runtime realm change commits its new token', () => {
- const refreshStart = source.indexOf('export async function refresh(): Promise<boolean> {');
+ const refreshStart = source.indexOf('export async function refresh(');
const refreshEnd = source.indexOf('\n}\n\nexport async function logout()', refreshStart);
const refreshBody = source.slice(refreshStart, refreshEnd);
@@ -985,7 +985,7 @@ describe('auth login-flow reset', () => {
expect(ownerCommitBody).toContain('await authSessionTeardown(opts.reason);');
expect(ownerCommitBody).toContain('notifyAuthListeners();');
- const refreshStart = source.indexOf('export async function refresh(): Promise<boolean> {');
+ const refreshStart = source.indexOf('export async function refresh(');
const refreshEnd = source.indexOf('\n}\n\nexport async function logout()', refreshStart);
const refreshBody = source.slice(refreshStart, refreshEnd);
expect(refreshBody).toContain(
diff --git a/apps/desktop/src/main/__tests__/authPassiveSharedInstance.test.ts b/apps/desktop/src/main/__tests__/authPassiveSharedInstance.test.ts
index 6e294e91c6..55a57e798a 100644
--- a/apps/desktop/src/main/__tests__/authPassiveSharedInstance.test.ts
+++ b/apps/desktop/src/main/__tests__/authPassiveSharedInstance.test.ts
@@ -117,7 +117,7 @@ describe('passive shared-userData instance auth isolation', () => {
expect(scheduleBody).toContain('refreshTimer = setTimeout(');
const retryBody = sliceBody(
- 'function scheduleRefreshRetryAfterTransientFailure(epoch: number, retryAt?: number): void {',
+ 'function scheduleRefreshRetryAfterTransientFailure(',
'\n}\n',
);
expect(retryBody).not.toContain('isPassiveSharedUserDataInstance');
@@ -126,7 +126,7 @@ describe('passive shared-userData instance auth isolation', () => {
it('refresh 本身不被 passive 拦:冷启动仍能换 access token,凭证缺席仍走过期出口', () => {
const body = sliceBody(
- 'export async function refresh(): Promise<boolean> {',
+ 'export async function refresh(',
'const diskTokenChangedBeforeRefresh',
);
diff --git a/apps/desktop/src/main/__tests__/authRefreshBackoff.test.ts b/apps/desktop/src/main/__tests__/authRefreshBackoff.test.ts
index 13ebdc4e5e..a8bcd8f6c6 100644
--- a/apps/desktop/src/main/__tests__/authRefreshBackoff.test.ts
+++ b/apps/desktop/src/main/__tests__/authRefreshBackoff.test.ts
@@ -2,6 +2,25 @@ import { describe, expect, it } from 'vitest';
import { AuthRefreshBackoff } from '../authRefreshBackoff';
describe('runtime auth refresh backoff', () => {
+ it('releases only a transport failure after a real response from the same login generation', () => {
+ const backoff = new AuthRefreshBackoff(() => 1_000_000);
+ backoff.defer(1, undefined, true);
+ backoff.networkRecovered(2);
+ expect(backoff.remaining(1)).toBe(60_000);
+ backoff.networkRecovered(1);
+ expect(backoff.remaining(1)).toBe(0);
+ backoff.defer(1);
+ backoff.networkRecovered(1);
+ expect(backoff.remaining(1)).toBe(60_000);
+ });
+
+ it.each([undefined, 1_030_000, 1_120_000])('never bypasses server failure or rate limiting (%s)', retryAt => {
+ const backoff = new AuthRefreshBackoff(() => 1_000_000);
+ const delay = backoff.defer(1, retryAt);
+ backoff.networkRecovered(1);
+ expect(backoff.remaining(1)).toBe(delay);
+ });
+
it('keeps an absolute deadline across two-second explicit calls and resumes at expiry', () => {
let now = 1_000_000;
const backoff = new AuthRefreshBackoff(() => now);
diff --git a/apps/desktop/src/main/__tests__/authSessionExpiredDetection.test.ts b/apps/desktop/src/main/__tests__/authSessionExpiredDetection.test.ts
index 675ee0f76e..e70b93ad1d 100644
--- a/apps/desktop/src/main/__tests__/authSessionExpiredDetection.test.ts
+++ b/apps/desktop/src/main/__tests__/authSessionExpiredDetection.test.ts
@@ -22,7 +22,7 @@ describe('desktop auth session-expiry detection', () => {
).replace(/\r\n/g, '\n');
it('runtime refresh 发现磁盘 token 消失但会话活着时,按确定性失效走会话过期出口', () => {
- const start = authSource.indexOf('export async function refresh(): Promise<boolean> {');
+ const start = authSource.indexOf('export async function refresh(');
const end = authSource.indexOf('const diskTokenChangedBeforeRefresh', start);
const body = authSource.slice(start, end);
@@ -151,11 +151,13 @@ describe('desktop auth session-expiry detection', () => {
expect(recoverBody).toContain('relayAuthRecoveryRetryTimer = setTimeout(');
expect(recoverBody).toContain("client?.getConnectionIssue()?.kind !== 'auth-failed'");
expect(recoverBody).toContain('authManager');
- expect(recoverBody).toContain('.refresh()');
+ expect(recoverBody).toContain('.refresh({ networkRecovered: true })');
expect(recoverBody).toContain("client?.connectNow('relay-auth-recovered');");
// refresh 在途期间持有权可能已被别的实例夺走(teardown 已停 client):connectNow
// 会把已停的 client 拉活绕过仲裁,重连前必须重新确认 isOwner。
expect(recoverBody).toContain('arbiter?.isOwner()');
+ expect(recoverBody).toContain('client === recoveryClient');
+ expect(recoverBody).toContain('activeOwnerScopeKey() === recoveryScope');
// teardown 必须清掉延迟自救 timer,避免登出/掉持有权后迟到触发。
const teardownStart = deviceLinkSource.indexOf('function teardownActiveLink(): void {');
@@ -165,7 +167,7 @@ describe('desktop auth session-expiry detection', () => {
});
it('routes replacement refresh through the durable Ghost projection owner boundary', () => {
- const refreshStart = authSource.indexOf('export async function refresh(): Promise<boolean> {');
+ const refreshStart = authSource.indexOf('export async function refresh(');
const refreshEnd = authSource.indexOf('\n}\n\nexport async function logout()', refreshStart);
const refreshBody = authSource.slice(refreshStart, refreshEnd);
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index 26dddebfd9..b1476ed69b 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -3318,9 +3318,9 @@ function scheduleNonXdOrgBetaDefault(input: {
*/
let coldStartAuthInFlight: Promise<AuthState> | null = null;
-function scheduleRefreshRetryAfterTransientFailure(epoch: number, retryAt?: number): void {
+function scheduleRefreshRetryAfterTransientFailure(epoch: number, retryAt?: number, networkFailure = false): void {
if (epoch !== authStateEpoch) return;
- const delay = refreshBackoff.defer(epoch, retryAt);
+ const delay = refreshBackoff.defer(epoch, retryAt, networkFailure);
if (refreshTimer !== null) {
clearTimeout(refreshTimer);
refreshTimer = null;
@@ -5723,12 +5723,13 @@ export async function dispatchLoginAction(action: unknown): Promise<DesktopLogin
}
}
-export async function refresh(): Promise<boolean> {
+export async function refresh(options?: { networkRecovered?: boolean }): Promise<boolean> {
if (getActiveAppSession().mode === 'local') {
log.debug('runtime refresh skipped in local mode');
return false;
}
if (refreshPromise !== null) return refreshPromise;
+ if (options?.networkRecovered) refreshBackoff.networkRecovered(authStateEpoch);
// Explicit callers (including chat polling and resume) share the same wait
// as the retry timer; skipped calls must not move the deadline forward.
if (refreshBackoff.remaining(authStateEpoch) > 0) return false;
@@ -5854,12 +5855,12 @@ export async function refresh(): Promise<boolean> {
log.warn(
`runtime refresh failed for a stale token after replacement retries status=${result.status} code=${code ?? '<none>'} — retrying no sooner than ${RUNTIME_REFRESH_RETRY_MS / 1000}s`,
);
- scheduleRefreshRetryAfterTransientFailure(refreshEpoch, result.retryAt);
+ scheduleRefreshRetryAfterTransientFailure(refreshEpoch, result.retryAt, result.status === 0);
} else {
log.warn(
`runtime refresh failed transiently status=${result.status} code=${code ?? '<none>'} — retrying no sooner than ${RUNTIME_REFRESH_RETRY_MS / 1000}s`,
);
- scheduleRefreshRetryAfterTransientFailure(refreshEpoch, result.retryAt);
+ scheduleRefreshRetryAfterTransientFailure(refreshEpoch, result.retryAt, result.status === 0);
}
return false;
}
diff --git a/apps/desktop/src/main/authRefreshBackoff.ts b/apps/desktop/src/main/authRefreshBackoff.ts
index c03512f45e..45359c9ff7 100644
--- a/apps/desktop/src/main/authRefreshBackoff.ts
+++ b/apps/desktop/src/main/authRefreshBackoff.ts
@@ -2,6 +2,7 @@
export class AuthRefreshBackoff {
private epoch = -1;
private retryAt = 0;
+ private networkFailure = false;
constructor(private readonly now: () => number = () => Date.now()) {}
@@ -9,14 +10,21 @@ export class AuthRefreshBackoff {
return this.epoch === epoch ? Math.max(0, this.retryAt - this.now()) : 0;
}
- defer(epoch: number, serverRetryAt?: number): number {
+ defer(epoch: number, serverRetryAt?: number, networkFailure = false): number {
this.epoch = epoch;
this.retryAt = Math.max(this.now() + 60_000, Number.isFinite(serverRetryAt) ? serverRetryAt! : 0);
+ this.networkFailure = networkFailure && serverRetryAt === undefined;
return this.remaining(epoch);
}
+ /** A real server response proves reachability; interface hints do not. */
+ networkRecovered(epoch: number): void {
+ if (this.epoch === epoch && this.networkFailure) this.clear();
+ }
+
clear(): void {
this.epoch = -1;
this.retryAt = 0;
+ this.networkFailure = false;
}
}
diff --git a/apps/desktop/src/main/device-link/__tests__/linkRecovery.test.ts b/apps/desktop/src/main/device-link/__tests__/linkRecovery.test.ts
index e8e8a58631..16a52303c0 100644
--- a/apps/desktop/src/main/device-link/__tests__/linkRecovery.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/linkRecovery.test.ts
@@ -1,11 +1,43 @@
import { describe, expect, it, vi } from 'vitest';
import { DeviceLinkError } from '@cindy/device-link';
import {
+ ensureInvokeLinkReady,
invokeWithClosedLinkRecovery,
requiresSessionLink,
} from '../linkRecovery.js';
describe('device-link closed link recovery', () => {
+ it('waits for the reliable link before sending a new read, while another peer proceeds', async () => {
+ let finish!: () => void;
+ const reopen = vi.fn(() => new Promise<void>(resolve => { finish = resolve; }));
+ const send = vi.fn();
+ const waiting = ensureInvokeLinkReady(() => true, reopen, () => {}).then(send);
+ const healthySend = vi.fn();
+ await ensureInvokeLinkReady(() => false, reopen, () => {}).then(healthySend);
+ expect(healthySend).toHaveBeenCalledOnce();
+ expect(send).not.toHaveBeenCalled();
+ expect(reopen).toHaveBeenCalledOnce();
+ finish();
+ await waiting;
+ expect(send).toHaveBeenCalledOnce();
+ });
+
+ it('does not send or reopen after cancellation, and rechecks cancellation after a pending handshake', async () => {
+ let cancelled = true;
+ const assertCurrent = () => { if (cancelled) throw new Error('cancelled'); };
+ let finish!: () => void;
+ const reopen = vi.fn(() => new Promise<void>(resolve => { finish = resolve; }));
+ await expect(ensureInvokeLinkReady(() => true, reopen, assertCurrent)).rejects.toThrow('cancelled');
+ expect(reopen).not.toHaveBeenCalled();
+ cancelled = false;
+ const send = vi.fn();
+ const waiting = ensureInvokeLinkReady(() => true, reopen, assertCurrent).then(send);
+ cancelled = true;
+ finish();
+ await expect(waiting).rejects.toThrow('cancelled');
+ expect(send).not.toHaveBeenCalled();
+ });
+
it('正常成功或不可重试错误不会触发 reopen', async () => {
const successInvoke = vi.fn().mockResolvedValue({ ok: true });
const reopen = vi.fn();
diff --git a/apps/desktop/src/main/device-link/__tests__/offlineRecovery.test.ts b/apps/desktop/src/main/device-link/__tests__/offlineRecovery.test.ts
new file mode 100644
index 0000000000..54dff8b40a
--- /dev/null
+++ b/apps/desktop/src/main/device-link/__tests__/offlineRecovery.test.ts
@@ -0,0 +1,80 @@
+import { readFileSync } from 'node:fs';
+import ts from 'typescript';
+import { expect, it, vi } from 'vitest';
+import { DeviceLinkError } from '@cindy/device-link';
+import { ensureInvokeLinkReady } from '../linkRecovery';
+
+// Execute the production probe adapter without starting Electron or a real relay.
+const source = ts.createSourceFile('index.ts', readFileSync(new URL('../index.ts', import.meta.url), 'utf8'), ts.ScriptTarget.Latest, true);
+let probe!: ts.PropertyAssignment;
+function findProbe(node: ts.Node): void {
+ if (ts.isPropertyAssignment(node) && node.name.getText(source) === 'probeInvoke') probe = node;
+ ts.forEachChild(node, findProbe);
+}
+findProbe(source);
+const compiled = ts.transpileModule(`const probeInvoke = ${probe.initializer.getText(source)};`, {
+ compilerOptions: { target: ts.ScriptTarget.ES2022 },
+}).outputText;
+
+function harness() {
+ let release!: () => void;
+ let scope = 'owner-a';
+ let disabled = false;
+ const revoked = new Set<string>();
+ const epochs = new Map<string, number>();
+ const reopen = vi.fn(() => new Promise<void>(resolve => { release = resolve; }));
+ const invoke = vi.fn(async (_peer, _payload, _timeout, options) => {
+ options.preSend();
+ return { ok: true, result: [] };
+ });
+ const dependencies = {
+ DeviceLinkError, ensureInvokeLinkReady,
+ client: { needsLinkRecovery: (peer: string) => peer === 'recovering', invoke },
+ parseSharedTaskPeer: () => null,
+ activeOwnerScopeKey: () => scope,
+ linkTornDown: false,
+ isAppSessionBoundaryPending: () => false,
+ arbiter: { isOwner: () => true },
+ openLinkCloseEpochs: epochs,
+ revokedByRemote: revoked,
+ assertRemoteControlTargetEnabled: () => { if (disabled) throw new Error('disabled'); },
+ openRemoteLink: reopen,
+ resolveRemoteInvokeTimeoutMs: () => 15_000,
+ };
+ const run = new Function(...Object.keys(dependencies), compiled + '; return probeInvoke;')(...Object.values(dependencies));
+ return { run, reopen, invoke, revoked, epochs, release: () => release(),
+ changeOwner: () => { scope = 'owner-b'; }, disable: () => { disabled = true; } };
+}
+
+it('repairs a suspended reliable link before probing while a healthy peer keeps responding', async () => {
+ const h = harness();
+ const pending = h.run('recovering', 'local-db:sessions:list', []);
+ expect(h.invoke).not.toHaveBeenCalled();
+ await expect(h.run('healthy', 'local-db:sessions:list', [])).resolves.toEqual({ ok: true, result: [] });
+ expect(h.reopen).toHaveBeenCalledExactlyOnceWith('recovering', { observed: false });
+ h.release();
+ await expect(pending).resolves.toEqual({ ok: true, result: [] });
+ expect(h.invoke.mock.calls.map(call => call[0])).toEqual(['healthy', 'recovering']);
+});
+
+it.each(['closed', 'revoked', 'owner changed', 'disabled'])(
+ 'does not send a probe after its pending recovery is %s', async change => {
+ const h = harness();
+ const pending = h.run('recovering', 'local-db:sessions:list', []);
+ if (change === 'closed') h.epochs.set('recovering', 1);
+ if (change === 'revoked') h.revoked.add('recovering');
+ if (change === 'owner changed') h.changeOwner();
+ if (change === 'disabled') h.disable();
+ h.release();
+ await expect(pending).rejects.toThrow();
+ expect(h.invoke).not.toHaveBeenCalled();
+ },
+);
+
+it('does not reopen a peer whose access was already revoked', async () => {
+ const h = harness();
+ h.revoked.add('recovering');
+ await expect(h.run('recovering', 'local-db:sessions:list', [])).rejects.toMatchObject({ code: 'ACCESS_REVOKED' });
+ expect(h.reopen).not.toHaveBeenCalled();
+ expect(h.invoke).not.toHaveBeenCalled();
+});
diff --git a/apps/desktop/src/main/device-link/__tests__/remoteInvokeSendGuard.test.ts b/apps/desktop/src/main/device-link/__tests__/remoteInvokeSendGuard.test.ts
index 20169b5fd7..2f008be8c7 100644
--- a/apps/desktop/src/main/device-link/__tests__/remoteInvokeSendGuard.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/remoteInvokeSendGuard.test.ts
@@ -2,7 +2,7 @@ import { readFileSync } from 'node:fs';
import ts from 'typescript';
import { expect, it, vi } from 'vitest';
import { DeviceLinkError, FILE_PEER_CHANNEL, type DeviceLinkClient } from '@cindy/device-link';
-import { invokeWithClosedLinkRecovery } from '../linkRecovery';
+import { ensureInvokeLinkReady, invokeWithClosedLinkRecovery } from '../linkRecovery';
// Run the production adapter with only its host dependencies injected, without
// importing Electron startup. The shared-client tests cover actual queue admission.
@@ -17,12 +17,11 @@ type PeerInvoke = (peer: string, channel: string, args: unknown[]) => Promise<un
function loadRemoteInvoke(overrides: Record<string, unknown>) {
const dependencies = {
- DeviceLinkError, FILE_PEER_CHANNEL, invokeWithClosedLinkRecovery,
+ DeviceLinkError, FILE_PEER_CHANNEL, ensureInvokeLinkReady, invokeWithClosedLinkRecovery,
assertNotStandby: () => {},
assertRemoteControlTargetEnabled: () => {},
openLinkCloseEpochs: new Map<string, number>(),
ensureOnlineForRequest: async () => {},
- client: { invoke: vi.fn() },
isScopedPeer: () => false,
tryPeerInvoke: async () => null,
resolveRemoteInvokeTimeoutMs: () => undefined,
@@ -30,6 +29,7 @@ function loadRemoteInvoke(overrides: Record<string, unknown>) {
closeRemoteLink: vi.fn(),
responsivenessTracker: null,
...overrides,
+ client: { invoke: vi.fn(), needsLinkRecovery: () => false, ...(overrides.client as object) },
};
return new Function(...Object.keys(dependencies), compiled + '; return remoteInvoke;')(...Object.values(dependencies));
}
@@ -53,7 +53,7 @@ function loadBackgroundInvoke(overrides: Record<string, unknown>) {
return new Function(...Object.keys(dependencies), backgroundCode + '; return remoteBackgroundInvoke;')(...Object.values(dependencies));
}
-it.each(['target resolution', 'link opening', 'online wait'])(
+it.each(['target resolution', 'link opening', 'online wait', 'reliable link recovery'])(
'does not send an old account’s background request after account changes during %s', async (stage) => {
let current = true;
let release!: () => void;
@@ -65,7 +65,8 @@ it.each(['target resolution', 'link opening', 'online wait'])(
return { ok: true, result: null };
});
const remoteInvoke = loadRemoteInvoke({
- client: { invoke },
+ client: { invoke, needsLinkRecovery: () => stage === 'reliable link recovery' },
+ openRemoteLink: pause,
ensureOnlineForRequest: stage === 'online wait' ? pause : async () => {},
});
const remoteBackgroundInvoke = loadBackgroundInvoke({
diff --git a/apps/desktop/src/main/device-link/index.ts b/apps/desktop/src/main/device-link/index.ts
index 5252dc5b64..88524363ec 100644
--- a/apps/desktop/src/main/device-link/index.ts
+++ b/apps/desktop/src/main/device-link/index.ts
@@ -172,7 +172,7 @@ import {
pollContactsDeviceSyncSettingChange,
setContactsDeviceLinkOwnerActive,
} from '../contacts-sync/driver';
-import { invokeWithClosedLinkRecovery, requiresSessionLink } from './linkRecovery';
+import { ensureInvokeLinkReady, invokeWithClosedLinkRecovery, requiresSessionLink } from './linkRecovery';
import { invalidatePluginOauth } from '../plugin-oauth/runtime.js';
import {
createResponsivenessTracker,
@@ -612,14 +612,20 @@ function recoverFromRelayAuthFailure(): void {
return;
}
lastRelayAuthRecoveryAt = now;
+ const recoveryClient = client;
+ const recoveryScope = activeOwnerScopeKey();
void authManager
- .refresh()
+ // A relay 401 proves reachability; an interface-change hint does not.
+ .refresh({ networkRecovered: true })
.then((ok) => {
// refresh 在途期间持有权可能已被另一个共享 userData 实例夺走
// (onDemote → teardownActiveLink 已停掉 client):此时 connectNow 会把
// 已停的 client 拉活、绕过仲裁重连,重新制造双连接 / last-wins 互踢。
// 重连前重新确认本实例仍是持有者。
- if (ok && arbiter?.isOwner()) client?.connectNow('relay-auth-recovered');
+ if (ok && client === recoveryClient && activeOwnerScopeKey() === recoveryScope
+ && !linkTornDown && !isAppSessionBoundaryPending() && arbiter?.isOwner()) {
+ client?.connectNow('relay-auth-recovered');
+ }
})
.catch((err) => {
log.warn('relay auth recovery refresh threw (non-fatal)', err);
@@ -732,8 +738,9 @@ export function initDeviceLinkService(options: DeviceLinkServiceOptions = {}): v
responsivenessTracker = createResponsivenessTracker({
// 探测直接走 client.invoke(guardInvoke 已在 tracker 内持有探测席位,不能再套
- // remoteInvoke 的外层门禁,否则自旋)。sessions:list 属 UNLINKED legacy 通道,无需 link。
- probeInvoke: (deviceId, channel, args) => {
+ // remoteInvoke 的外层门禁,否则自旋)。曾协商可靠传输的 peer 必须先恢复 link,
+ // 否则 sessions:list 也只会滞留发送队列,反复得到 BACKPRESSURE。
+ probeInvoke: async (deviceId, channel, args) => {
if (!client)
throw new Error('[DEVICE_LINK_NOT_CONNECTED] device-link client not initialized');
if (parseSharedTaskPeer(deviceId)) {
@@ -747,7 +754,27 @@ export function initDeviceLinkService(options: DeviceLinkServiceOptions = {}): v
invoke: (probeChannel, probeArgs) => probeClient.invoke(deviceId, { channel: probeChannel, args: probeArgs }),
});
}
- return client.invoke(deviceId, { channel, args }, resolveRemoteInvokeTimeoutMs(channel, args, 'desktop'));
+ const probeClient = client;
+ const scope = activeOwnerScopeKey();
+ const closeEpoch = openLinkCloseEpochs.get(deviceId) ?? 0;
+ const assertCurrent = (): void => {
+ if (revokedByRemote.has(deviceId)) {
+ throw new DeviceLinkError('ACCESS_REVOKED', 'access revoked by target device');
+ }
+ if (client !== probeClient || linkTornDown || activeOwnerScopeKey() !== scope
+ || isAppSessionBoundaryPending() || (openLinkCloseEpochs.get(deviceId) ?? 0) !== closeEpoch
+ || !arbiter?.isOwner()) {
+ throw new DeviceLinkError('NOT_CONNECTED', 'responsiveness probe superseded');
+ }
+ assertRemoteControlTargetEnabled(deviceId);
+ };
+ await ensureInvokeLinkReady(
+ () => probeClient.needsLinkRecovery(deviceId),
+ () => openRemoteLink(deviceId, { observed: false }),
+ assertCurrent,
+ );
+ return probeClient.invoke(deviceId, { channel, args }, resolveRemoteInvokeTimeoutMs(channel, args, 'desktop'),
+ { preSend: assertCurrent });
},
onUnresponsiveChanged: (deviceId, unresponsive, recovered) => {
broadcast(DEVICE_LINK_PUSH.RESPONSIVENESS_CHANGED, { deviceId, unresponsive, recovered });
@@ -1857,7 +1884,13 @@ export async function remoteInvoke(
preSend();
if (accelerated) return accelerated;
}
- return client.invoke(deviceId, { channel, args }, resolveRemoteInvokeTimeoutMs(channel, args, 'desktop'), { preSend });
+ const invokeClient = client;
+ await ensureInvokeLinkReady(
+ () => invokeClient.needsLinkRecovery(deviceId),
+ () => openRemoteLink(deviceId, { observed: false }),
+ preSend,
+ );
+ return invokeClient.invoke(deviceId, { channel, args }, resolveRemoteInvokeTimeoutMs(channel, args, 'desktop'), { preSend });
};
const run = (): Promise<InvokeResultPayload> =>
invokeWithClosedLinkRecovery(
@@ -1968,7 +2001,7 @@ export async function remoteSubscribe(
let liveTopics = liveTopicsNow();
if (liveTopics.length === 0) return { ok: true, result: null };
if (!client) throw new Error('[DEVICE_LINK_NOT_CONNECTED] device-link client not initialized');
- if (requiresSessionLink(liveTopics) && !client.isLinkReady(deviceId)) {
+ if (client.needsLinkRecovery(deviceId) || (requiresSessionLink(liveTopics) && !client.isLinkReady(deviceId))) {
if ((openLinkCloseEpochs.get(deviceId) ?? 0) !== closeEpoch) {
throw new DeviceLinkError('LINK_NOT_OPEN', 'link closed while waiting to reconnect');
}
@@ -1979,6 +2012,9 @@ export async function remoteSubscribe(
if (liveTopics.length === 0) return { ok: true, result: null };
}
assertRemoteControlTargetEnabledAfterReopen(deviceId);
+ if ((openLinkCloseEpochs.get(deviceId) ?? 0) !== closeEpoch) {
+ throw new DeviceLinkError('LINK_NOT_OPEN', 'link closed while restoring subscriptions');
+ }
if (!client) throw new Error('[DEVICE_LINK_NOT_CONNECTED] device-link client not initialized');
return client.invoke(deviceId, {
channel: DL_SUBSCRIBE_CHANNEL,
diff --git a/apps/desktop/src/main/device-link/linkRecovery.ts b/apps/desktop/src/main/device-link/linkRecovery.ts
index 3f028ea02a..c21b42475a 100644
--- a/apps/desktop/src/main/device-link/linkRecovery.ts
+++ b/apps/desktop/src/main/device-link/linkRecovery.ts
@@ -1,5 +1,17 @@
import { DeviceLinkError, parseFsWatchTopic } from '@cindy/device-link';
+/** Recover before admitting a new request; never replay an already sent operation. */
+export async function ensureInvokeLinkReady(
+ needsRecovery: () => boolean,
+ reopen: () => Promise<unknown>,
+ assertCurrent: () => void,
+): Promise<void> {
+ assertCurrent();
+ if (!needsRecovery()) return;
+ await reopen();
+ assertCurrent();
+}
+
/** 打开具体远程会话或文件树监听时必须先建立 streaming link;轻量 sessions 列表订阅不依赖。 */
export function requiresSessionLink(topics: readonly string[]): boolean {
return topics.some((topic) => (
diff --git a/docs/dev-rules/credentials-and-local-storage.md b/docs/dev-rules/credentials-and-local-storage.md
index e3cc5cc9cc..5e804dd71e 100644
--- a/docs/dev-rules/credentials-and-local-storage.md
+++ b/docs/dev-rules/credentials-and-local-storage.md
@@ -182,6 +182,10 @@ Claude.ai 登录,也不得收集、存储或中转订阅凭证。Cindy 因此
Desktop 的运行时 refresh 遇到瞬时失败后,主动调用与定时重试共用至少 60 秒的等待;
429 的有效 `Retry-After` 更长时按服务器等待时间执行。等待期间的调用不发请求、不延长
原期限,也不清除凭证。成功续期/登录清除等待,登录代次改变后旧等待不约束新账号。
+唯一提前结束等待的情形是:此前失败为无服务端响应的网络错误(status 0),且 relay
+随后明确返回鉴权拒绝,证明网络路径已可达。同登录代次的自救可提前续期,仍复用在途
+refresh 与 relay 自救节流;网卡变化提示不能触发该例外,429、Retry-After 和服务端错误
+仍遵守原期限。续期完成后的立即重连须复验 client、owner 与持有权,不能唤醒旧实例。
冷启动、确定性失效和同机 token 替换恢复继续沿用原有判定。状态只留在进程内存中。
实现见 `main/authRefreshBackoff.ts`、`main/authManager.ts`;回归见
`main/__tests__/authRefreshBackoff.test.ts`。
diff --git a/docs/dev-rules/maker-core-and-agent-behavior.md b/docs/dev-rules/maker-core-and-agent-behavior.md
index 1f5fcac726..639c3b7c69 100644
--- a/docs/dev-rules/maker-core-and-agent-behavior.md
+++ b/docs/dev-rules/maker-core-and-agent-behavior.md
@@ -169,6 +169,10 @@ SSH 或暂时失败留待重试;共享原生 ID 的活动任务优先,不关
配置;每次重连重新认证,刷新必须匹配冻结的 owner、host 代次及账号,且不能复用刚被
拒绝的 token。owner 切换 pending 期间,即使 owner key 尚未提交变化,也必须在异步认证
读取前后拒绝提供 token。刷新有超时,失败走正常错误路径,不切回历史所属账号。
+Codex 用量读取若收到原生 `account/rateLimits/read` 的明确 `401 token_expired`,
+在同一个账号控制 host 上调用一次 `account/read { refreshToken: true }`,成功后仅重试
+该只读 RPC 一次。其它错误、明确撤销与刷新失败直接返回;不清凭证、不重发模型 turn。
+
同 owner 的 Ghost 投影修复会更新账号代次但保留 Maker 与活跃任务;凭证 reader 固定
owner 身份、认证 realm 和 CodexAgent 实例,每次读取单独捕获代次,不能将创建时的代次永久锁在
reader 上。跨修复的在途读取仍拒绝,修复完成后的新读取可正常刷新;真正切账号或
diff --git a/docs/dev-rules/remote-and-mobile-adaptation.md b/docs/dev-rules/remote-and-mobile-adaptation.md
index 614c5fed72..d08d5c4dc6 100644
--- a/docs/dev-rules/remote-and-mobile-adaptation.md
+++ b/docs/dev-rules/remote-and-mobile-adaptation.md
@@ -184,6 +184,12 @@ relay 以 close 1013 `inbound backpressure` 主动断连,此时任何「立即
## 共享恢复与请求策略
+Desktop 向曾协商可靠传输、当前发送链路未就绪的设备发起新 invoke、订阅或响应性探测时,
+先复用该设备既有的单飞建链入口,再发送请求。轻量列表也适用,避免请求尚未写出就反复
+耗尽排队预算;从未建链或显式关闭后回退到 legacy 的设备继续使用原路径。等待前后复验
+取消与禁用状态,自动探测另查撤权和 owner;建链成功本身不算业务恢复。故障与动作都限于
+单 peer,不重启共享 relay、不重放已发出的写操作;回归覆盖另一 peer 与 legacy 请求继续工作。
+
普通出站 invoke 在共享 `InvokeScheduler` 中按目标设备排队:每台最多 12 个在途,
其中 PR 引用/状态、定时任务侧栏和用量同步等后台查询最多 4 个。
同一真实设备的普通远控与多个共享任务共用并发和等待配额;取消仍按各自 scoped peer
diff --git a/packages/device-link/src/__tests__/client.test.ts b/packages/device-link/src/__tests__/client.test.ts
index 259dd06d65..2cf3c1b30e 100644
--- a/packages/device-link/src/__tests__/client.test.ts
+++ b/packages/device-link/src/__tests__/client.test.ts
@@ -130,6 +130,36 @@ function makeHarness(opts?: {
const tick = (ms = 0): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
describe('outbound invoke admission', () => {
+ it('distinguishes a suspended reliable peer from legacy reads and another healthy controller', async () => {
+ const h = makeHarness({ timing: { pingIntervalMs: 60_000 } });
+ try {
+ h.client.start(); await tick(); h.current().ack();
+ expect(h.client.needsLinkRecovery('legacy')).toBe(false);
+ await establishInboundReliableLink(h, 'stream-a', 1, 'peer-a');
+ await establishInboundReliableLink(h, 'stream-b', 1, 'peer-b');
+ expect(h.client.needsLinkRecovery('peer-a')).toBe(false);
+ h.client.restartConnection('system-resume');
+ await tick(); h.current().ack();
+ expect(h.client.needsLinkRecovery('peer-a')).toBe(true);
+ expect(h.client.needsLinkRecovery('peer-b')).toBe(true);
+ expect(h.client.needsLinkRecovery('legacy')).toBe(false);
+ await establishInboundReliableLink(h, 'stream-b', 1, 'peer-b');
+ expect(h.client.needsLinkRecovery('peer-b')).toBe(false);
+ const socket = h.current();
+ const response = h.client.invoke('legacy', { channel: 'local-db:sessions:list', args: [] });
+ const frame = socket.sent.find(env => env.kind === 'invoke' && env.dst === 'legacy')!;
+ socket.push({ v: 1, kind: 'invoke-result', src: 'legacy', id: frame.id, payload: { ok: true, result: [] } });
+ await expect(response).resolves.toEqual({ ok: true, result: [] });
+ await establishInboundReliableLink(h, 'stream-a', 1, 'peer-a');
+ expect(h.client.needsLinkRecovery('peer-a')).toBe(false);
+ expect(h.client.needsLinkRecovery('peer-b')).toBe(false);
+ expect(h.current()).toBe(socket);
+ h.client.closeLink('peer-a', 'user');
+ expect(h.client.needsLinkRecovery('peer-a')).toBe(false);
+ expect(h.client.needsLinkRecovery('peer-b')).toBe(false);
+ } finally { h.client.stop(); }
+ });
+
it('waits for a 20-second mobile history recovery without widening other requests or replaying it', async () => {
vi.useFakeTimers();
const h = makeHarness({ timing: { pingIntervalMs: 60_000, requestTimeoutMs: 15_000 } });
diff --git a/packages/device-link/src/client.ts b/packages/device-link/src/client.ts
index a3f8b828bd..d9e3be0580 100644
--- a/packages/device-link/src/client.ts
+++ b/packages/device-link/src/client.ts
@@ -1091,6 +1091,12 @@ export class DeviceLinkClient {
return !!peer && this.isPeerSendReady(peer) && peer.receiveReady;
}
+ /** Even unlinked reads queue behind a previously negotiated reliable link. */
+ needsLinkRecovery(dst: string): boolean {
+ const peer = this.peerTransport.get(dst);
+ return !!peer?.reliable && !this.isPeerSendReady(peer);
+ }
+
/** Mirror admission only: listing-only/legacy peers need no bidirectional link. */
canSendPush(dst: string): boolean {
if (this.status !== 'online') return false;
diff --git a/packages/maker-core/src/agents/codex/app-server/account-rate-limits.test.ts b/packages/maker-core/src/agents/codex/app-server/account-rate-limits.test.ts
new file mode 100644
index 0000000000..09355b35af
--- /dev/null
+++ b/packages/maker-core/src/agents/codex/app-server/account-rate-limits.test.ts
@@ -0,0 +1,42 @@
+import { describe, expect, it, vi } from 'vitest';
+import { readAccountRateLimitsWithRefresh } from './account-rate-limits.js';
+import { Method } from './protocol.js';
+
+const expired = new Error('codex app-server account/rateLimits/read error -32603: 401 Unauthorized; {"code":"token_expired"}');
+
+describe('native account rate limit renewal', () => {
+ it('refreshes a recoverable expired access token before retrying the read once', async () => {
+ const result = { rateLimits: { planType: 'pro' } };
+ const request = vi.fn().mockRejectedValueOnce(expired).mockResolvedValueOnce({}).mockResolvedValueOnce(result);
+ await expect(readAccountRateLimitsWithRefresh(request)).resolves.toEqual(result);
+ expect(request.mock.calls).toEqual([
+ [Method.AccountRateLimitsRead],
+ ['account/read', { refreshToken: true }, { timeoutMs: 8_000 }],
+ [Method.AccountRateLimitsRead],
+ ]);
+ });
+
+ it.each([
+ '401 token_expired',
+ 'codex app-server model/list error: 401 token_expired',
+ 'codex app-server account/rateLimits/read error: 401 token_revoked',
+ 'codex app-server account/rateLimits/read error: 429',
+ 'codex app-server account/rateLimits/read error: 502 upstream unreachable',
+ ])('does not refresh unrelated or permanent errors: %s', message => {
+ const error = new Error(message);
+ const request = vi.fn().mockRejectedValue(error);
+ return expect(readAccountRateLimitsWithRefresh(request)).rejects.toBe(error).then(() => {
+ expect(request).toHaveBeenCalledOnce();
+ });
+ });
+
+ it('does not loop when renewal fails or the renewed token is still rejected', async () => {
+ const refreshFailed = new Error('network unavailable');
+ const failed = vi.fn().mockRejectedValueOnce(expired).mockRejectedValueOnce(refreshFailed);
+ await expect(readAccountRateLimitsWithRefresh(failed)).rejects.toBe(refreshFailed);
+ expect(failed).toHaveBeenCalledTimes(2);
+ const rejected = vi.fn().mockRejectedValueOnce(expired).mockResolvedValueOnce({}).mockRejectedValueOnce(expired);
+ await expect(readAccountRateLimitsWithRefresh(rejected)).rejects.toBe(expired);
+ expect(rejected).toHaveBeenCalledTimes(3);
+ });
+});
diff --git a/packages/maker-core/src/agents/codex/app-server/account-rate-limits.ts b/packages/maker-core/src/agents/codex/app-server/account-rate-limits.ts
new file mode 100644
index 0000000000..fbe275202f
--- /dev/null
+++ b/packages/maker-core/src/agents/codex/app-server/account-rate-limits.ts
@@ -0,0 +1,20 @@
+import type { AccountRateLimitsResponse } from '../../../types/account-rate-limits.js';
+import { Method } from './protocol.js';
+
+/** Only the read-only account RPC may be retried after native OAuth renewal. */
+export async function readAccountRateLimitsWithRefresh(
+ request: <T>(method: string, params?: unknown, options?: { timeoutMs: number }) => Promise<T>,
+): Promise<AccountRateLimitsResponse> {
+ try {
+ return await request<AccountRateLimitsResponse>(Method.AccountRateLimitsRead);
+ } catch (error) {
+ // Correlated app-server error, not stderr, model text, or an arbitrary 401.
+ if (!(error instanceof Error)
+ || !error.message.includes(`${Method.AccountRateLimitsRead} error`)
+ || !/\b401\b/.test(error.message)
+ || !/\btoken_expired\b/.test(error.message)
+ || /token_revoked|token_invalidated|refresh_token_reused/.test(error.message)) throw error;
+ }
+ await request('account/read', { refreshToken: true }, { timeoutMs: 8_000 });
+ return request<AccountRateLimitsResponse>(Method.AccountRateLimitsRead);
+}
diff --git a/packages/maker-core/src/agents/codex/index.ts b/packages/maker-core/src/agents/codex/index.ts
index b2f2445c00..fd66c0b987 100644
--- a/packages/maker-core/src/agents/codex/index.ts
+++ b/packages/maker-core/src/agents/codex/index.ts
@@ -42,6 +42,7 @@ import { promises as fs } from 'node:fs';
import { structuredPatch } from 'diff';
import { syncCodexArchiveState } from './archive-state.js';
import { readCodexThreadMcpServerTools } from './mcp-server-tools.js';
+import { readAccountRateLimitsWithRefresh } from './app-server/account-rate-limits.js';
import {
BaseAgent,
@@ -2995,7 +2996,7 @@ assertRouteCurrent();
// oauth-bearer prevents a gateway/provider host from reading or mutating the wrong
// account context; getHost refuses to replace a differently-authenticated active host.
return this.withStartedAccountHost(providerId,
- host => host.request<AccountRateLimitsResponse>(Method.AccountRateLimitsRead, undefined));
+ host => readAccountRateLimitsWithRefresh((method, params, options) => host.request(method, params, options)));
}
/** Consume one reset credit on the non-model app-server control plane. */