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): guard Ghost media deposit paths on the durable owner

Yuewei-LLcbeabd84ba89
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 656e21f58f..24721dc9aa 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts
@@ -205,4 +205,47 @@ describe('market Ghost session boundary', () => {
source.match(/isOwnerBoundaryPending: \(\) => isGhostBoundaryPending\(\)/g)?.length ?? 0;
expect(injections).toBeGreaterThanOrEqual(2);
});
+
+ it('Ghost 媒体持久化写入守卫(resolveOwnedMedia/saveGhostMedia)也用 durable owner 组合条件', () => {
+ // 这两处是 GhostCindySlot 的 deps,内部 assertStillValid 会在 ingestMedia 的
+ // await 边界反复断言。持久化写入守卫必须和入口/生成守卫一样用 isGhostBoundaryPending,
+ // 否则另一实例在落盘窗口翻转 durable owner 时,字节仍会被登记为 ghost-gallery ref。
+ const resolveStart = source.indexOf('resolveOwnedMedia: async (ghostId, hash, ownerScopeKey)');
+ const saveStart = source.indexOf('saveGhostMedia: async ({ ghostId, buffer, mimeType, ownerScopeKey');
+ expect(resolveStart).toBeGreaterThan(-1);
+ expect(saveStart).toBeGreaterThan(-1);
+ const resolveBody = source.slice(resolveStart, resolveStart + 700);
+ const saveBody = source.slice(saveStart, saveStart + 700);
+ const combinedGuard = 'isGhostBoundaryPending() || activeOwnerScopeKey() !== ownerScopeKey';
+ expect(resolveBody).toContain(combinedGuard);
+ expect(saveBody).toContain(combinedGuard);
+ // 持久化守卫不得退化回只看进程内边界的旧写法。
+ expect(source).not.toContain('isAppSessionBoundaryPending() || activeOwnerScopeKey() !== ownerScopeKey');
+ });
+
+ it('networkSlot 的 saveGhostMedia(as:media fetch 落仓)也用 durable owner 组合条件', () => {
+ // networkSlot 与 cindy 槽是两个独立实现,签名不带 ownerScopeKey(在函数体开头
+ // 捕获)。它的落仓路径(ghost-gallery 作品归属 + recordGhostCallMedia)必须同样
+ // 有 durable owner 守卫 + assertStillValid + 补偿 journal,否则 as:'media' fetch
+ // 读取窗口内 durable owner 翻转时仍会落仓到错误 owner 的画廊。
+ const networkStart = source.indexOf('saveGhostMedia: async ({ ghostId, buffer, mimeType, label, callId }) =>');
+ expect(networkStart).toBeGreaterThan(-1);
+ const networkBody = source.slice(networkStart, networkStart + 1800);
+ expect(networkBody).toContain('const ownerScopeKey = activeOwnerScopeKey();');
+ expect(networkBody).toContain('isGhostBoundaryPending() || activeOwnerScopeKey() !== ownerScopeKey');
+ expect(networkBody).toContain('assertStillValid: assertOwnerScopeCurrent');
+ expect(networkBody).toContain('refCompensationScope: captureMediaRefCompensationScope(ownerScopeKey)');
+ });
+
+ it('depositMedia(ghost-deposit 寄存器落仓)也用 durable owner 组合条件', () => {
+ // 寄存器引用按 ghostId 落到 owner 作用域账本(originKind:'user' 但 refId 仍是意识),
+ // 落仓窗口翻转全局 owner 时必须 fail closed,与 saveGhostMedia 同口径。
+ const depositStart = source.indexOf('depositMedia: async ({ ghostId, buffer, mimeType, label }) =>');
+ expect(depositStart).toBeGreaterThan(-1);
+ const depositBody = source.slice(depositStart, depositStart + 1800);
+ expect(depositBody).toContain('const ownerScopeKey = activeOwnerScopeKey();');
+ expect(depositBody).toContain('isGhostBoundaryPending() || activeOwnerScopeKey() !== ownerScopeKey');
+ expect(depositBody).toContain('assertStillValid: assertOwnerScopeCurrent');
+ expect(depositBody).toContain('refCompensationScope: captureMediaRefCompensationScope(ownerScopeKey)');
+ });
});
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index 559a0a5aaf..4b32376af4 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -3606,7 +3606,7 @@ export function getGhostCindySlot(): GhostCindySlot {
},
resolveOwnedMedia: async (ghostId, hash, ownerScopeKey) => {
const assertOwnerScopeCurrent = (): void => {
- if (isAppSessionBoundaryPending() || activeOwnerScopeKey() !== ownerScopeKey) {
+ if (isGhostBoundaryPending() || activeOwnerScopeKey() !== ownerScopeKey) {
throw new Error('媒体任务期间账号已切换,本次结果已丢弃');
}
};
@@ -3633,7 +3633,7 @@ export function getGhostCindySlot(): GhostCindySlot {
},
saveGhostMedia: async ({ ghostId, buffer, mimeType, ownerScopeKey, label, callId }) => {
const assertOwnerScopeCurrent = (): void => {
- if (isAppSessionBoundaryPending() || activeOwnerScopeKey() !== ownerScopeKey) {
+ if (isGhostBoundaryPending() || activeOwnerScopeKey() !== ownerScopeKey) {
throw new Error('媒体任务期间账号已切换,本次结果已丢弃');
}
};
@@ -3676,19 +3676,34 @@ export function getGhostCindySlot(): GhostCindySlot {
// 管道;记成 'ghost' 会让 ghostCanRead 的 origin 分支把它当作该意识的
// 出生物,与"作品"混为一谈。引用方(refId)才是意识,归属由此成立。
depositMedia: async ({ ghostId, buffer, mimeType, label }) => {
- const r = await ingestMedia({
- buffer,
- mimeType,
- isCache: false,
- refs: [
- {
- refKind: 'ghost-deposit',
- refId: ghostId,
- originKind: 'user',
- ...(label ? { label } : {}),
- },
- ],
- });
+ // 与 saveGhostMedia 同口径的 durable owner 守卫:寄存器引用按 ghostId
+ // 落到 owner 作用域账本,落盘窗口翻转全局 owner 时必须 fail closed。
+ const ownerScopeKey = activeOwnerScopeKey();
+ const assertOwnerScopeCurrent = (): void => {
+ if (isGhostBoundaryPending() || activeOwnerScopeKey() !== ownerScopeKey) {
+ throw new Error('媒体任务期间账号已切换,本次结果已丢弃');
+ }
+ };
+ assertOwnerScopeCurrent();
+ const db = getDbClient().drizzle;
+ const r = await ingestMedia(
+ {
+ buffer,
+ mimeType,
+ isCache: false,
+ refs: [
+ {
+ refKind: 'ghost-deposit',
+ refId: ghostId,
+ originKind: 'user',
+ ...(label ? { label } : {}),
+ },
+ ],
+ assertStillValid: assertOwnerScopeCurrent,
+ refCompensationScope: captureMediaRefCompensationScope(ownerScopeKey),
+ },
+ db,
+ );
return {
url: r.url,
hash: r.hash,
@@ -4069,20 +4084,36 @@ export function getGhostNetworkSlot(): GhostNetworkSlot {
// (规则 25)。mime 白名单同一来源(blobStore),槽内归一化后再判。
isSupportedMediaMime: (mime) => supportedMime(mime),
saveGhostMedia: async ({ ghostId, buffer, mimeType, label, callId }) => {
- const r = await ingestMedia({
- buffer,
- mimeType,
- isCache: false,
- refs: [
- {
- refKind: 'ghost-gallery',
- refId: ghostId,
- originKind: 'ghost',
- originId: ghostId,
- ...(label ? { label } : {}),
- },
- ],
- });
+ // 与 cindy 槽 saveGhostMedia 同口径的 durable owner 守卫:落盘前与
+ // ingestMedia 每个 await 边界都复查,防止兄弟实例在 fetch 读取窗口翻转
+ // 全局 Ghost owner marker 后,字节仍被登记为 ghost-gallery 作品。
+ const ownerScopeKey = activeOwnerScopeKey();
+ const assertOwnerScopeCurrent = (): void => {
+ if (isGhostBoundaryPending() || activeOwnerScopeKey() !== ownerScopeKey) {
+ throw new Error('媒体任务期间账号已切换,本次结果已丢弃');
+ }
+ };
+ assertOwnerScopeCurrent();
+ const db = getDbClient().drizzle;
+ const r = await ingestMedia(
+ {
+ buffer,
+ mimeType,
+ isCache: false,
+ refs: [
+ {
+ refKind: 'ghost-gallery',
+ refId: ghostId,
+ originKind: 'ghost',
+ originId: ghostId,
+ ...(label ? { label } : {}),
+ },
+ ],
+ assertStillValid: assertOwnerScopeCurrent,
+ refCompensationScope: captureMediaRefCompensationScope(ownerScopeKey),
+ },
+ db,
+ );
recordGhostCallMedia(ghostId, callId, r.url);
return { url: r.url, hash: r.hash, ext: r.ext };
},