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(plugin): revalidate agent network calls

liugaowei2c392a41ba82
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/networkSlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/networkSlot.test.ts
index 02f6152435..02557a4639 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/networkSlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/networkSlot.test.ts
@@ -444,6 +444,39 @@ describe('networkSlot · 重定向逐跳守门', () => {
expect(release).toHaveBeenCalledTimes(2);
expect(fetchImpl).not.toHaveBeenCalled();
});
+
+ it('Agent 交卷后不再跟进未声明目标的后续重定向', async () => {
+ const liveCall = { ghostId: 'web-search', sessionId: 'session-1', channel: 'session' as const };
+ const inFlightCallInfo = vi.fn()
+ // 入口资格、首跳资格与重定向目标预检仍在途。
+ .mockReturnValueOnce(liveCall)
+ .mockReturnValueOnce(liveCall)
+ .mockReturnValueOnce(liveCall)
+ .mockReturnValueOnce(liveCall)
+ // 真正发起第二跳前已经交卷。
+ .mockReturnValue(null);
+ const release = vi.fn(async () => undefined);
+ const guardedFetch = vi.fn(async () => ({
+ response: redirectTo('https://cdn.example.com/next'),
+ release,
+ }));
+ const { slot, fetchImpl } = makeSlot({
+ getGhost: () => fakeGhost({ network: null }),
+ inFlightCallInfo,
+ fetchPublicImpl: guardedFetch,
+ });
+
+ const result = await slot.handleFetchRequest('web-search', {
+ url: 'https://example.com/start',
+ callId: 'call-agent',
+ });
+
+ expect(result).toMatchObject({ ok: false });
+ if (!result.ok) expect(result.message).toContain('Agent 调用已结束');
+ expect(guardedFetch).toHaveBeenCalledTimes(1);
+ expect(release).toHaveBeenCalledTimes(1);
+ expect(fetchImpl).not.toHaveBeenCalled();
+ });
});
describe('networkSlot · 响应收敛', () => {
diff --git a/apps/desktop/src/main/cindy-brain/networkSlot.ts b/apps/desktop/src/main/cindy-brain/networkSlot.ts
index 13a21b5c32..e3da273c46 100644
--- a/apps/desktop/src/main/cindy-brain/networkSlot.ts
+++ b/apps/desktop/src/main/cindy-brain/networkSlot.ts
@@ -1036,13 +1036,15 @@ export class GhostNetworkSlot {
return { ok: false, message: '意识不在可用状态' };
}
const net = ghost.manifest.network;
- const callInfo = requestCallId
- ? this.deps.inFlightCallInfo?.(requestCallId) ?? null
- : null;
- const agentMediated =
- callInfo?.ghostId === ghostId
- && callInfo.channel === 'session'
- && callInfo.sessionId !== null;
+ const hasLiveAgentAuthorization = (): boolean => {
+ const callInfo = requestCallId
+ ? this.deps.inFlightCallInfo?.(requestCallId) ?? null
+ : null;
+ return callInfo?.ghostId === ghostId
+ && callInfo.channel === 'session'
+ && callInfo.sessionId !== null;
+ };
+ const agentMediated = hasLiveAgentAuthorization();
// 静态 hosts 与动态连接地址(network.connections,用户在设置页添加、
// 逐条过主机受信确认)至少有其一,才算"声明过白名单"。
const connectionDecls = net?.connections ?? [];
@@ -1068,7 +1070,8 @@ export class GhostNetworkSlot {
const hostDeclared = (hostname: string): boolean =>
declaredHosts.some((pattern) => ghostNetworkHostMatches(pattern, hostname)) ||
connectionHosts.includes(hostname);
- const hostAllowed = (hostname: string): boolean => agentMediated || hostDeclared(hostname);
+ const hostAllowed = (hostname: string): boolean =>
+ hostDeclared(hostname) || hasLiveAgentAuthorization();
if (!hostAllowed(url.hostname)) {
const declared = [...declaredHosts, ...connectionHosts];
return {
@@ -1250,7 +1253,12 @@ export class GhostNetworkSlot {
signal: controller.signal,
redirect: 'manual' as const,
};
- if (agentMediated && !hostDeclared(currentUrl.hostname)) {
+ const hopHostDeclared = hostDeclared(currentUrl.hostname);
+ const hopAgentMediated = !hopHostDeclared && hasLiveAgentAuthorization();
+ if (!hopHostDeclared && !hopAgentMediated) {
+ return { ok: false, message: '当前 Agent 调用已结束,未声明目标不再允许访问' };
+ }
+ if (hopAgentMediated) {
const guarded = await this.deps.fetchPublicImpl(currentUrl.toString(), fetchInit);
guardedFetchReleases.push(guarded.release);
response = guarded.response;