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(desktop,maker-cc-manager): RegistryError 类型联合补齐 + token 轮换同步失效远端 CC

yane47ba1145ece
diff --git a/apps/desktop/src/main/maker-host/index.ts b/apps/desktop/src/main/maker-host/index.ts
index 960173c024..875346fdda 100644
--- a/apps/desktop/src/main/maker-host/index.ts
+++ b/apps/desktop/src/main/maker-host/index.ts
@@ -118,6 +118,7 @@ import {
unregisterCodexMcpThreadContext,
} from '../mcp-integrations/codexEnvironment.js';
import type { CodexHttpBridge } from '../mcp-integrations/codexHttpBridge.js';
+import { setRemoteMcpBridgeTokenRotatedHook } from '../mcp-integrations/remoteMcpBridgeToken.js';
import { ensureRemoteMcpForward, setRemoteMcpForwardRearmedHook, stripRemoteCodexMcpConfig } from '../remote-ssh/codex-remote-mcp.js';
import { buildCcRemoteHttpMcpServers, readCcAppliedFingerprint, writeCcAppliedFingerprint } from './cc-remote-mcp.js';
import { getRemoteSessionStartEnsure, getRemoteCodexLiveTurnChecker, setRemoteCcTurnSettledHandler, setRemoteCcStaleQuery } from './remote-session-start-ensure.js';
@@ -537,6 +538,12 @@ export function getMaker(): Maker {
// (codex-connector R22 P1):插件开关 / custom MCP CRUD / contacts /
// Slack provider / 账号切换等所有 shutdown 路径自动覆盖, 不靠逐点调用。
setCodexEnvironmentShutdownHook(handleCodexEnvironmentShutdownForRemote);
+ // bridge token 轮换 (账号切换 secrets 清空) 时同步失效远端 CC query —
+ // 旧 Authorization header 在新 bridge 上持续 401;独立于 shutdown 路径
+ // (本地 turn 忙时 shutdown 会被跳过, codex-connector R24 P2)。
+ setRemoteMcpBridgeTokenRotatedHook(() => {
+ invalidateActiveRemoteCcQueries({ reason: 'bridge-token-rotated' });
+ });
// forward 端口重绑 (SSH 重连 onRearmed) 时, 该 host 上活跃远端 CC
// query 的 mcpServers URL 还指旧端口 — fresh 失效 + detach 促重建
// (codex-connector R19 P2)。
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/remoteMcpBridgeToken.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/remoteMcpBridgeToken.test.ts
index a9fcd5ded1..9ea0b30758 100644
--- a/apps/desktop/src/main/mcp-integrations/__tests__/remoteMcpBridgeToken.test.ts
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/remoteMcpBridgeToken.test.ts
@@ -33,6 +33,7 @@ vi.mock('../../secrets/providerSecretStore.js', () => ({
import {
getRemoteMcpBridgeToken,
resetRemoteMcpBridgeTokenCacheForTests,
+ setRemoteMcpBridgeTokenRotatedHook,
} from '../remoteMcpBridgeToken.js';
describe('remoteMcpBridgeToken', () => {
@@ -42,6 +43,7 @@ describe('remoteMcpBridgeToken', () => {
h.reads.count = 0;
h.writes.count = 0;
resetRemoteMcpBridgeTokenCacheForTests();
+ setRemoteMcpBridgeTokenRotatedHook(null);
});
it('lazily generates and persists the token on first use, then serves from cache', () => {
@@ -54,6 +56,25 @@ describe('remoteMcpBridgeToken', () => {
expect(h.reads.count).toBe(1); // 第二次走进程内缓存,不再读 safeStorage
});
+ it('invokes the rotated hook when secrets are cleared (R24 P2: remote CC must be invalidated)', () => {
+ const hook = vi.fn();
+ setRemoteMcpBridgeTokenRotatedHook(hook);
+ getRemoteMcpBridgeToken();
+ expect(hook).not.toHaveBeenCalled();
+ for (const listener of h.clearListeners) listener();
+ expect(hook).toHaveBeenCalledTimes(1);
+ });
+
+ it('keeps the secrets-cleared flow working when the rotated hook throws', () => {
+ setRemoteMcpBridgeTokenRotatedHook(() => {
+ throw new Error('invalidate exploded');
+ });
+ getRemoteMcpBridgeToken();
+ expect(() => {
+ for (const listener of h.clearListeners) listener();
+ }).not.toThrow();
+ });
+
it('registers the secrets-cleared listener on first use', () => {
getRemoteMcpBridgeToken();
expect(h.clearListeners).toHaveLength(1);
diff --git a/apps/desktop/src/main/mcp-integrations/remoteMcpBridgeToken.ts b/apps/desktop/src/main/mcp-integrations/remoteMcpBridgeToken.ts
index 6da6d7ebc8..b98694776e 100644
--- a/apps/desktop/src/main/mcp-integrations/remoteMcpBridgeToken.ts
+++ b/apps/desktop/src/main/mcp-integrations/remoteMcpBridgeToken.ts
@@ -24,6 +24,20 @@ let cached: string | null | undefined;
let clearListenerRegistered = false;
let unregisterClearListener: (() => void) | null = null;
+/**
+ * token 轮换 (secrets 清空后重新生成) 时的宿主钩子 (maker-host 注入):
+ * 远端 CC query 的 Authorization header 是注入时的旧 token 快照 — 不主动
+ * 失效的话, 旧 query 在新 bridge 上持续 401 直到用户手动重启
+ * (codex-connector R24 P2)。独立于 shutdownCodexEnvironment 路径 —
+ * 账号切换在本地 codex turn 忙时会跳过 shutdown, 本钩子照样生效。
+ * mcp-integrations 不反向依赖 maker-host, setter 注入解耦。
+ */
+let tokenRotatedHook: (() => void) | null = null;
+
+export function setRemoteMcpBridgeTokenRotatedHook(fn: (() => void) | null): void {
+ tokenRotatedHook = fn;
+}
+
/**
* 账号切换 clearAll 删除 safeStorage 后,进程内缓存必须一并失效——否则旧账号
* 远端 daemon env 里的 token 在当前进程内仍可通过 bridge 鉴权(串号)。惰性
@@ -34,6 +48,11 @@ function ensureClearListener(): void {
clearListenerRegistered = true;
unregisterClearListener = addProviderSecretsClearedListener(() => {
cached = undefined;
+ try {
+ tokenRotatedHook?.();
+ } catch {
+ /* 失效失败不阻断 secrets 清理主流程 */
+ }
});
}
diff --git a/packages/maker-cc-manager/__tests__/kill-pending-send.test.ts b/packages/maker-cc-manager/__tests__/kill-pending-send.test.ts
index 055f5e6c83..05f9021e9b 100644
--- a/packages/maker-cc-manager/__tests__/kill-pending-send.test.ts
+++ b/packages/maker-cc-manager/__tests__/kill-pending-send.test.ts
@@ -39,7 +39,7 @@ function buildBlockingFactory(): SdkQueryFactory {
describe('kill 终止窗口的 sendMessage 拒绝 (Greptile P1)', () => {
it('rejects sendMessage with SESSION_KILL_PENDING while a forceful kill is still settling', async () => {
const registry = new SessionRegistry({ sdkQueryFactory: buildBlockingFactory() });
- const session = await registry.create({ sessionId: 's1', cwd: '/repo', model: 'm' });
+ const session = await registry.create({ sessionId: 's1', cwd: '/repo', model: 'm', env: {} });
// kill 发出后 consume loop 仍挂在线上 (inputStream 未消费新消息) —
// alive 为 true、inputQueue 已 end 的窗口期。
@@ -55,7 +55,7 @@ describe('kill 终止窗口的 sendMessage 拒绝 (Greptile P1)', () => {
it('still throws SESSION_NOT_FOUND after the session fully exits (not the kill-pending code)', async () => {
const registry = new SessionRegistry({ sdkQueryFactory: buildBlockingFactory() });
- await registry.create({ sessionId: 's2', cwd: '/repo', model: 'm' });
+ await registry.create({ sessionId: 's2', cwd: '/repo', model: 'm', env: {} });
await registry.close('s2'); // close 路径: alive 立即 false
expect(() => registry.sendMessage('s2', { text: 'hello' })).toThrowError(/SESSION_NOT_FOUND|no longer alive/);
diff --git a/packages/maker-cc-manager/src/session-registry.ts b/packages/maker-cc-manager/src/session-registry.ts
index 32b4c47701..c8f2a2ddbf 100644
--- a/packages/maker-cc-manager/src/session-registry.ts
+++ b/packages/maker-cc-manager/src/session-registry.ts
@@ -718,7 +718,7 @@ export class SessionRegistry {
}
interface RegistryError extends Error {
- code: 'SESSION_NOT_FOUND' | 'SESSION_ALREADY_EXISTS' | 'SDK_ERROR';
+ code: 'SESSION_NOT_FOUND' | 'SESSION_ALREADY_EXISTS' | 'SESSION_KILL_PENDING' | 'SDK_ERROR';
}
function makeRegistryError(code: RegistryError['code'], message: string): RegistryError {