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(plugins): guard namespace recovery identities and host error codes

Zhao Dengfengbb0de73bb720
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/ghostVisibility.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/ghostVisibility.test.ts
index 2eec00a890..1d5155ef17 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/ghostVisibility.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/ghostVisibility.test.ts
@@ -55,6 +55,31 @@ describe('classifyGhostVisibility namespace', () => {
expect(result.message).toContain('JSON null');
});
+ // PR #6434: an explicit wrong address must not disclose an unavailable owner's
+ // identities. Existing namespace cases only exercised available installations.
+ it.each([null, 'null'])('hides unavailable identities for namespace %s', (namespace) => {
+ const unavailableDeps = {
+ ...deps,
+ listGhosts: () => [enterprise],
+ isAvailableForActiveSession: () => false,
+ };
+ const result = classifyGhostVisibility('helper', null, unavailableDeps, namespace);
+ expect(result).toEqual(classifyGhostVisibility('missing', null, unavailableDeps, namespace));
+ expect(result).toMatchObject({ ok: false, errorCode: 'GHOST_NOT_FOUND' });
+ expect(JSON.stringify(result)).not.toContain('acme');
+ });
+
+ it('only includes active-session identities in namespace mismatch guidance', () => {
+ const result = classifyGhostVisibility('helper', null, {
+ ...deps,
+ isAvailableForActiveSession: (id) => id === 'helper',
+ }, 'wrong');
+ expect(result).toMatchObject({ ok: false, errorCode: 'GHOST_NAMESPACE_MISMATCH' });
+ if (result.ok) return;
+ expect(result.message).toContain('{"ghost_id":"helper","namespace":null}');
+ expect(result.message).not.toContain('acme');
+ });
+
it('keeps the literal organization slug null distinct from JSON null', () => {
const namedNull = ghost('helper', 'null');
const stageDeps = { ...deps, listGhosts: () => [root, namedNull] };
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/pipeDispatcher.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/pipeDispatcher.test.ts
index c074248f09..63fec69401 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/pipeDispatcher.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/pipeDispatcher.test.ts
@@ -420,7 +420,9 @@ describe('配对交卷', () => {
await expect(p).resolves.toMatchObject({ ok: false, errorCode: 'INTERNAL', message: '画布爆炸' });
});
- it('透传合法插件业务错误码,但不允许覆盖主机错误码', async () => {
+ // PR #6434: a plugin has already executed when it submits its result, so it
+ // cannot claim the host's pre-dispatch namespace failure and trigger a retry.
+ it.each(['GHOST_CRASHED', 'GHOST_NAMESPACE_MISMATCH'])('不允许插件覆盖主机错误码 %s', async (hostErrorCode) => {
const h = makeHarness();
const business = h.dispatcher.callGhostTool(CALL);
await vi.waitFor(() => expect(h.sent).toHaveLength(1));
@@ -439,7 +441,7 @@ describe('配对交卷', () => {
type: 'tool-result',
callId: h.sent[1].callId,
ok: false,
- errorCode: 'GHOST_CRASHED',
+ errorCode: hostErrorCode,
message: '伪造主机错误',
});
await expect(reserved).resolves.toMatchObject({ ok: false, errorCode: 'INTERNAL' });
diff --git a/apps/desktop/src/main/cindy-brain/forge.ts b/apps/desktop/src/main/cindy-brain/forge.ts
index b560c1b4c6..3d7d2d90bd 100644
--- a/apps/desktop/src/main/cindy-brain/forge.ts
+++ b/apps/desktop/src/main/cindy-brain/forge.ts
@@ -2237,7 +2237,8 @@ cindy.send({ type: 'tool-result', callId: msg.callId, ok: true, result: {
note: "干完了"
}});
// 失败交卷:errorCode 是可选的插件业务错误码;主机保留码
-// (GHOST_NOT_FOUND / GHOST_ASLEEP / GHOST_CRASHED / TIMEOUT / INTERNAL 等)不可由插件写入。
+// (GHOST_NOT_FOUND / GHOST_NAMESPACE_MISMATCH / GHOST_ASLEEP / GHOST_CRASHED / TIMEOUT / INTERNAL 等)
+// 不可由插件写入;GHOST_NAMESPACE_MISMATCH 只由主机在派发前产生,插件交卷时已执行,不能冒用。
// message 必须包含用户可执行的恢复指引;不要把失败对象 JSON.stringify 到 message。
cindy.send({
type: 'tool-result',
diff --git a/apps/desktop/src/main/cindy-brain/ghostVisibility.ts b/apps/desktop/src/main/cindy-brain/ghostVisibility.ts
index 411b9bc51f..4346a89172 100644
--- a/apps/desktop/src/main/cindy-brain/ghostVisibility.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostVisibility.ts
@@ -56,7 +56,11 @@ export function classifyGhostVisibility(
: { status: 'missing' as const };
})();
if (resolved.status === 'missing') {
- const sameName = namespace === undefined ? [] : listed.filter((ghost) => ghost.manifest.id === ghostId);
+ // The outgoing owner projection can remain installed during account teardown.
+ // Match discovery's active-session gate before disclosing recovery identities.
+ const sameName = namespace === undefined ? [] : listed.filter((ghost) =>
+ ghost.manifest.id === ghostId && deps.isAvailableForActiveSession(installedGhostStoragePart(ghost)),
+ );
if (sameName.length > 0) {
// A model may quote JSON null as the valid organization slug "null".
// Preserve exact addressing: explain the mismatch, never redirect a call
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/ghostWorkdirGate.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/ghostWorkdirGate.test.ts
index 150356da49..64f75a0aec 100644
--- a/apps/desktop/src/main/mcp-integrations/__tests__/ghostWorkdirGate.test.ts
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/ghostWorkdirGate.test.ts
@@ -1369,6 +1369,37 @@ describe('connect_account ordinary task entry', () => {
});
describe('花名册 / ghost_list 过滤', () => {
+ // PR #6434: list already hides an unavailable owner projection; wrong-address
+ // info/manual/call/setup must preserve that boundary before any side effect.
+ it.each(['claude-code', 'codex', 'pi'] as const)(
+ '%s hides namespace recovery targets while the active owner is unavailable', async (agentKind) => {
+ listMock.mockReturnValue([{
+ ...(chipGhost('art', ['tool']) as object),
+ namespace: 'outgoing-owner',
+ dir: path.join(tmpUserData, '_ns', 'outgoing-owner', 'art'),
+ }]);
+ activeSessionAvailableMock.mockReturnValue(false);
+ const deps = makeDeps(agentKind);
+ expect(JSON.parse((await handleGhostList(deps)).content[0].text).ghosts).toEqual([]);
+ const target = { ghost_id: 'art', namespace: null };
+ for (const result of [
+ await handleGhostInfo(deps, target),
+ await handleGhostManual(deps, target),
+ await handleGhostCall(deps, { ...target, tool: 'run', attachments: ['cindy-media://blobs/example.png'] }),
+ ]) {
+ expect(result.isError).toBe(true);
+ expect(JSON.parse(result.content[0].text)).toMatchObject({ ok: false, errorCode: 'GHOST_NOT_FOUND' });
+ expect(result.content[0].text).not.toContain('outgoing-owner');
+ }
+ const connection = await deps.connectAccount!({ kind: 'plugin', id: 'art', namespace: null });
+ expect(connection).toMatchObject({ ok: false, errorCode: 'GHOST_NOT_FOUND' });
+ expect(JSON.stringify(connection)).not.toContain('outgoing-owner');
+ expect(ensureReadyMock).not.toHaveBeenCalled();
+ expect(grantAttachmentsMock).not.toHaveBeenCalled();
+ expect(dispatchMock).not.toHaveBeenCalled();
+ },
+ );
+
// Regression: a listed root was repeatedly addressed with the string "null".
// Exercise the real MCP handlers and host resolver with the same session deps;
// a rejected address must never reach setup, attachment grants or dispatch.
diff --git a/apps/desktop/src/shared/ghost.ts b/apps/desktop/src/shared/ghost.ts
index 976e898873..646fa9e990 100644
--- a/apps/desktop/src/shared/ghost.ts
+++ b/apps/desktop/src/shared/ghost.ts
@@ -8743,6 +8743,7 @@ export type GhostPipeFetchResult =
/** 工具调用的结构化失败分类(与 cindy-tools 的 CindyGhostCallErrorCode 同构)。 */
export type GhostToolCallErrorCode =
| 'GHOST_NOT_FOUND'
+ | 'GHOST_NAMESPACE_MISMATCH'
| 'GHOST_ASLEEP'
| 'GHOST_DISABLED_IN_WORKDIR'
| 'TOOL_NOT_FOUND'
@@ -8755,6 +8756,7 @@ export type GhostToolCallErrorCode =
/** 主机错误码不能被插件结果覆盖;其余格式合法的码作为业务错误码透传。 */
const GHOST_HOST_ERROR_CODES: ReadonlySet<string> = new Set([
'GHOST_NOT_FOUND',
+ 'GHOST_NAMESPACE_MISMATCH',
'GHOST_ASLEEP',
'GHOST_DISABLED_IN_WORKDIR',
'TOOL_NOT_FOUND',
diff --git a/docs/ghost-progressive-discovery.md b/docs/ghost-progressive-discovery.md
index b227c14e1a..055d32c9d0 100644
--- a/docs/ghost-progressive-discovery.md
+++ b/docs/ghost-progressive-discovery.md
@@ -79,10 +79,12 @@ frontmatter `name + description` 的召回作用;`manual.items` 只是插件
调用参数必须保留发现结果中的 JSON 类型:`null` 与字符串 `"null"` 不同,后者是合法的
企业 slug,不能自动转成 root。插件名已安装但显式 namespace 不匹配时,共用可见性入口返回
-`GHOST_NAMESPACE_MISMATCH`,消息列出保留 namespace 缺省/null/字符串三态的安装身份,
+`GHOST_NAMESPACE_MISMATCH`,消息仅列出当前账号可用、保留 namespace 缺省/null/字符串三态的安装身份,
供 Agent 核对目标后修正参数;原调用不进入 Setup、文件交接或插件派发,也不自动改选实例。
修正参数后同一任务即可重新查询/调用,无需重新安装或新建任务。完全没有该插件名时仍返回
-`GHOST_NOT_FOUND`。此错误码只增补 MCP 工具结果,不改变安装数据、权限、实例解析或跨设备协议;
+`GHOST_NOT_FOUND`;账号切换期间或同名安装均不对当前账号可用时,也返回该错误且不披露身份。
+`GHOST_NAMESPACE_MISMATCH` 属于宿主保留码,插件交卷不能冒用;管子入口将其归一化为 `INTERNAL`,
+避免已执行的操作被误判为尚未派发。此错误码不改变安装数据、权限、实例解析或跨设备协议;
旧 Host 仍返回原错误,旧查看端可照常显示文本。实现与回归见 `ghostVisibility.ts`、
`ghostVisibility.test.ts` 和 `ghostWorkdirGate.test.ts`。