Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(pi): retain MCP failures on older runtimes
xuzini-xzn484ed484adfe
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-mcp-bridge.integration.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-mcp-bridge.integration.test.tsindex 6e47278237..f95531246d 100644--- a/packages/maker-core/src/agents/pi/__tests__/pi-mcp-bridge.integration.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/pi-mcp-bridge.integration.test.ts@@ -37,7 +37,7 @@ import type { Logger } from '../../../interfaces/logger.js';const __dirname = path.dirname(fileURLToPath(import.meta.url));const REPO_ROOT = path.resolve(__dirname, '../../../../../..');-const PI_BINARY = path.join(+const PI_BINARY = process.env.CINDY_PI_MCP_TEST_BINARY || path.join(REPO_ROOT,'apps','pi-bin',@@ -1161,7 +1161,7 @@ describe.skipIf(!piAvailable)('PiAgent × cindy-bridge (real pi + MCP bridge + p'preserves MCP structured %s data in the real Pi model request',{ timeout: 90_000 },async (scenario) => {- const { requests } = await runOneTurn('bypassPermissions',+ const { requests, events } = await runOneTurn('bypassPermissions',async () => ({ kind: 'permission', behavior: 'allow' }), 'local-multi','verify MCP structured result ' + (scenario === 'failure' ? 'structured failure': scenario === 'rich-failure' ? 'rich failure' : 'success'));@@ -1170,6 +1170,8 @@ describe.skipIf(!piAvailable)('PiAgent × cindy-bridge (real pi + MCP bridge + pconst result = messages.flatMap(message => message.content).filter(block => block.type === 'tool_result').at(-1)!;expect(JSON.stringify(result.content)).toContain('MCP_STRUCTURED_MODEL_CANARY');expect(Boolean(result.is_error)).toBe(scenario !== 'success');+ const toolResult = events.filter(event => event.type === 'tool_result_full').at(-1)!;+ expect((toolResult.data as { isError: boolean }).isError).toBe(scenario !== 'success');if (scenario === 'rich-failure') {const blocks = result.content as Array<Record<string, unknown>>;expect(blocks.map(block => block.type)).toEqual(['text', 'image', 'text', 'text']);diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-mcp-client.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-mcp-client.test.tsindex 1376df0f04..dba2cf0491 100644--- a/packages/maker-core/src/agents/pi/__tests__/pi-mcp-client.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/pi-mcp-client.test.ts@@ -28,11 +28,16 @@ function gatewayWithFetch(fetchImpl: typeof fetch) {type: 'object', properties: { ghost_id: { type: 'string' } }, required: ['ghost_id'],} }]);const registered: any[] = [];- gateway.register({ registerTool: (tool: unknown) => registered.push(tool) });+ const resultHooks: Array<(event: unknown) => unknown> = [];+ gateway.register({ registerTool: (tool: unknown) => registered.push(tool),+ on: (event: string, handler: (event: unknown) => unknown) => {+ if (event === 'tool_result') resultHooks.push(handler);+ },+ });registered.find(t => t.name === 'cindy_mcp_list_tools').execute('list', { server: 'cindy', tool: 'ghost_call' });const call = (signal?: AbortSignal) => registered.find(t => t.name === 'cindy_mcp_call_tool').execute('call', { server: 'cindy', tool: 'ghost_call', args: { ghost_id: 'demo' } }, signal);- return { call, gateway, client, list: (params: unknown = {}) => registered.find(t => t.name === 'cindy_mcp_list_tools').execute('list', params) };+ return { call, gateway, client, resultHook: (event: unknown) => resultHooks[0]?.(event), list: (params: unknown = {}) => registered.find(t => t.name === 'cindy_mcp_list_tools').execute('list', params) };}afterEach(() => vi.useRealTimers());@@ -91,6 +96,23 @@ describe('Pi MCP request lifecycle', () => {});});+ it('marks only the failed call through the pre-0.99 tool_result hook, even after content projection', async () => {+ let calls = 0;+ const { call, resultHook } = gatewayWithFetch(vi.fn(async () => Response.json({ result: {+ content: [{ type: 'text', text: 'same content' }], isError: calls++ === 0,+ } })));+ const [failed, succeeded] = await Promise.all([call(), call()]);+ // Old Pi ignores execute-result isError; earlier result hooks may copy content.+ expect(resultHook({ content: [...failed.content], details: failed.details, isError: false }))+ .toEqual({ isError: true });+ // A native wrapper may handle the inner failure and return those same blocks.+ expect(resultHook({ content: failed.content, details: failed.details, isError: false }))+ .toBeUndefined();+ expect(resultHook({ content: succeeded.content, details: succeeded.details, isError: false }))+ .toBeUndefined();+ expect(resultHook({ content: [], details: {}, isError: true })).toBeUndefined();+ });+it('does not duplicate structured data already serialized in a text block', async () => {const structuredContent = { answer: 42 };const content = [{ type: 'text', text: JSON.stringify(structuredContent, null, 2) }];diff --git a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts b/packages/maker-core/src/agents/pi/cindy-bridge-source.tsindex 28a7210c76..5d1c077d20 100644--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts@@ -2967,6 +2967,9 @@ function schemaHint(schema: Record<string, unknown>): string {}class CindyMcpGateway {+ // Older Pi ignores execute-result isError but supports tool_result overrides.+ // Weak identities keep the status private without retaining completed calls.+ private readonly failedResults = new WeakMap<object, { content: object; details: unknown }>();private readonly tools = new Map<string, ConnectedMcpTool>();private readonly unavailableServers = new Map<string, string>();/** 已披露工具:键为 server + NUL + tool,值为该工具当前 schema 的指纹。 */@@ -3441,11 +3444,14 @@ class CindyMcpGateway {? '. Expected args schema: ' + schemaHint(resolved.tool.inputSchema) : ''),);}- return {- content: mcpContentToPi(result?.content, result?.structuredContent),- details: result?.structuredContent ?? {},- ...(result?.isError ? { isError: true } : {}),- };+ const content = mcpContentToPi(result?.content, result?.structuredContent);+ const details = result?.structuredContent ?? {};+ if (result?.isError) {+ const failure = { content, details };+ this.failedResults.set(content, failure);+ if (details !== null && typeof details === 'object') this.failedResults.set(details, failure);+ }+ return { content, details, ...(result?.isError ? { isError: true } : {}) };}private async executeCall(params: unknown, signal?: AbortSignal, toolCallId?: string): Promise<{@@ -3533,6 +3539,16 @@ class CindyMcpGateway {}register(pi: any, options: { botMemoryFacade?: boolean } = {}): void {+ pi.on?.('tool_result', (event: any) => {+ const failure = this.failedResults.get(event.content) ?? this.failedResults.get(event.details);+ if (failure) {+ this.failedResults.delete(failure.content);+ if (failure.details !== null && typeof failure.details === 'object') {+ this.failedResults.delete(failure.details);+ }+ return { isError: true };+ }+ });// Host-owned Bot identity enables native shortcuts; execution uses the scoped helper entry.this.botHelperFacadeEnabled = options.botMemoryFacade === true;this.botMemoryFacadeEnabled = options.botMemoryFacade === true