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): keep Ghost media in-flight guards on the durable owner

Yuewei-LL955e2c071200
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/cindySlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/cindySlot.test.ts
index fe195b8994..99857220b9 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/cindySlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/cindySlot.test.ts
@@ -1015,6 +1015,28 @@ describe('代办链路', () => {
expect(saveGhostMedia).not.toHaveBeenCalled();
});
+ it('生成期间 Ghost durable owner 变为 mismatch 时不保存产物', async () => {
+ // 模拟另一实例改写全局 durable Ghost projection owner:进程内 boundaryDepth
+ // 与 owner scope key 都不变,但 Ghost 专属边界(isOwnerBoundaryPending 已含
+ // durable owner 检查)在任务执行中变 pending,在途任务必须收口。
+ let boundaryPending = false;
+ const saveGhostMedia = vi.fn();
+ const { slot } = makeSlot({
+ isOwnerBoundaryPending: () => boundaryPending,
+ generateImage: vi.fn(async () => {
+ boundaryPending = true;
+ return { buffer: new Uint8Array([1, 2, 3]), mimeType: 'image/png' };
+ }),
+ saveGhostMedia,
+ });
+
+ const result = await slot.handleModelRequest('art', REQ);
+
+ expect(result).toMatchObject({ ok: false });
+ expect((result as { message: string }).message).toContain('账号已切换');
+ expect(saveGhostMedia).not.toHaveBeenCalled();
+ });
+
it('保存期间切换账号时不向新作用域返回旧产物', async () => {
let ownerScopeKey = 'cloud:owner-a:1';
const saveGhostMedia = vi.fn(async (params: { ownerScopeKey: string }) => {
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts
index 9fdcae8de1..656e21f58f 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts
@@ -191,4 +191,18 @@ describe('market Ghost session boundary', () => {
expect(body).toContain("throwIpcError('INTERNAL', 'Unable to verify the installed Plugin source');");
expect(body).toContain("throwIpcError('INTERNAL', 'Unable to detach the installed Plugin source');");
});
+
+ it('Ghost 媒体在途守卫注入 durable owner 组合条件,而不是退化成只看进程内边界', () => {
+ const helperStart = source.indexOf('function isGhostBoundaryPending(): boolean {');
+ expect(helperStart).toBeGreaterThan(-1);
+ const helperEnd = source.indexOf('\n}\n', helperStart);
+ const helperBody = source.slice(helperStart, helperEnd);
+ // helper 必须是组合条件:进程内 App boundary + durable projection owner 稳定性。
+ expect(helperBody).toContain('isAppSessionBoundaryPending()');
+ expect(helperBody).toContain('isGhostSkillProjectionBoundaryStableForOwner');
+ // 两处 Ghost 专属消费点(xAI 通道与 GhostCindySlot)都必须走 helper。
+ const injections =
+ source.match(/isOwnerBoundaryPending: \(\) => isGhostBoundaryPending\(\)/g)?.length ?? 0;
+ expect(injections).toBeGreaterThanOrEqual(2);
+ });
});
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/xaiImageClient.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/xaiImageClient.test.ts
index f464f076ec..df7e8f36d1 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/xaiImageClient.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/xaiImageClient.test.ts
@@ -214,6 +214,25 @@ describe('xaiImageClient', () => {
expect(responseFetch).toHaveBeenCalledTimes(1);
});
+ it('响应返回途中 Ghost durable owner 变为 mismatch 时中止且不返回结果', async () => {
+ // 另一实例改写全局 durable Ghost projection owner:进程内 boundaryDepth 与
+ // owner scope key 都不变,但 Ghost 专属边界(isOwnerBoundaryPending 已含
+ // durable owner 检查)在响应返回途中变 pending,必须中止且不返回结果。
+ let boundaryPending = false;
+ const doFetch = vi.fn<typeof fetch>(async () => {
+ boundaryPending = true;
+ return new Response(
+ JSON.stringify({ data: [{ b64_json: 'aW1hZ2U=', mime_type: 'image/jpeg' }] }),
+ { status: 200 },
+ );
+ });
+ const ch = channel(doFetch, { isOwnerBoundaryPending: () => boundaryPending });
+ await expect(
+ ch.generateImage({ model: 'xai/grok-imagine-image', prompt: 'p' }),
+ ).rejects.toThrow('账号已切换');
+ expect(doFetch).toHaveBeenCalledTimes(1);
+ });
+
it('登录态决定 ready;派发拦截、源图上限与 OAuth 拒绝均在出网边界处理', async () => {
const doFetch = vi.fn<typeof fetch>();
const unavailable = channel(doFetch, { hasOAuthLogin: () => false });
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index 24617b228a..559a0a5aaf 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -3268,7 +3268,7 @@ function getImageChannelRegistry(): ImageChannelRegistry {
hasOAuthLogin: () => hasGrokOAuthLogin(),
getAccessToken: () => getGrokAccessToken(),
getOwnerScopeKey: () => activeOwnerScopeKey(),
- isOwnerBoundaryPending: () => isAppSessionBoundaryPending(),
+ isOwnerBoundaryPending: () => isGhostBoundaryPending(),
fetchImplementation: ((url, init) => outboundFetch(url as string, init)) as typeof fetch,
beforeDispatch: (model) => assertMediaModelStillEnabled('image', model),
onAuthRejected: (failure) => invalidateXaiBridgeAuth(failure),
@@ -3367,12 +3367,26 @@ function resolveImageChannelForModel(model: string, operation: 'generate' | 'edi
return getImageChannelRegistry().resolve(entry.providerId);
}
+/**
+ * Combined Ghost-owner boundary gate for Ghost-only paths (media generation,
+ * cindy-slot work). Unlike the app-wide isAppSessionBoundaryPending(), this
+ * also fails closed while the durable projection owner (shared global marker)
+ * differs, so a sibling instance flipping the owner cannot let an in-flight
+ * Ghost task keep calling upstream or saving media.
+ */
+function isGhostBoundaryPending(): boolean {
+ return (
+ isAppSessionBoundaryPending() ||
+ !isGhostSkillProjectionBoundaryStableForOwner(getActiveAppSession().dataOwnerId)
+ );
+}
+
export function getGhostCindySlot(): GhostCindySlot {
if (!cindySlotSingleton) {
cindySlotSingleton = new GhostCindySlot({
getGhost: (id) => findAvailableGhost(id),
getOwnerScopeKey: () => activeOwnerScopeKey(),
- isOwnerBoundaryPending: () => isAppSessionBoundaryPending(),
+ isOwnerBoundaryPending: () => isGhostBoundaryPending(),
// model 已在 modelSlot 按白名单校验;归属来源(providerId)按白名单条目
// 定位,经 imageChannelRegistry 取对应执行通道(2026-07 图像多来源)。
generateImage: async ({ prompt, model, aspectRatio }) => {