Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(sharing): use failure signals for file authorization
davide82bca4e29ea
diff --git a/apps/desktop/src/main/device-link/__tests__/sharedTaskMediaAccess.test.ts b/apps/desktop/src/main/device-link/__tests__/sharedTaskMediaAccess.test.tsindex a2fd9080e9..329716f1bb 100644--- a/apps/desktop/src/main/device-link/__tests__/sharedTaskMediaAccess.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/sharedTaskMediaAccess.test.ts@@ -197,6 +197,46 @@ describe('shared task media access', () => {expect(deps.query.mock.calls[1][1]).toEqual(['task', 'run-1', 2]);}});+ // Unit regression via assertSharedTaskMedia: a delivered error-report.pdf+ // must open, while actual failures still deny it. Earlier provenance tests+ // lacked successful payloads containing diagnostic words (review #4311987397).+ it.each([+ { status: { ok: true }, allowed: true },+ { status: { success: true, stderr: 'warning: optional feature unavailable', errors: [] }, allowed: true },+ { status: { exitCode: 0, stdout: 'completed with 0 errors' }, allowed: true },+ { status: { isError: true }, allowed: false },+ { status: { is_error: true }, allowed: false },+ { status: { ok: false }, allowed: false },+ { status: { success: false }, allowed: false },+ { status: { status: 'failed' }, allowed: false },+ { status: { exitCode: 2 }, allowed: false },+ { status: { error: 'generation unavailable' }, allowed: false },+ ])('uses actual MCP failure signals for delivery: $status', async ({ status, allowed }) => {+ const file = path.resolve('deliveries/error-report.pdf');+ const url = 'xdt-file://local?path=' + encodeURIComponent(file);+ const result = { ok: true, result: { ...status, xdt_media_produced: [url] } };+ deps.query.mockResolvedValueOnce([{ rowid: 2, role: 'tool_result', toolUseId: 'generate-1', content: JSON.stringify(result) }])+ .mockResolvedValueOnce([{ content: JSON.stringify({ toolName: 'mcp:reports:generate', input: {} }) }]);+ const access = assertSharedTaskMedia(url, capture);+ if (allowed) await expect(access).resolves.toEqual({ file });+ else await expect(access).rejects.toThrow('PERMISSION_DENIED');+ });+ it.each([+ { result: 'completed with 0 errors', allowed: true },+ { result: { exitCode: 0, stdout: 'error count: 0', stderr: 'warning' }, allowed: true },+ { result: '<tool_use_error>operation stopped</tool_use_error>', allowed: false },+ { result: 'Error: operation stopped', allowed: false },+ { result: { exitCode: 1, stdout: 'done' }, allowed: false },+ ])('uses actual command failure signals before checking output evidence: $result', async ({ result, allowed }) => {+ const file = path.join(path.parse(process.cwd()).root, 'deliveries', 'error-report.pdf');+ deps.query.mockResolvedValueOnce([{ rowid: 1, role: 'tool_use', toolUseId: 'copy-1', createdAt: 100,+ content: JSON.stringify({ toolName: 'Bash', input: { command: `cp /work/source.txt "${markdownPath(file)}"` } }) }])+ .mockResolvedValueOnce([{ content: JSON.stringify(result), createdAt: 200 }]);+ deps.stat.mockResolvedValue({ isFile: () => true, mtimeMs: 150 });+ const access = assertSharedTaskMedia('xdt-file://local?path=' + encodeURIComponent(file), capture);+ if (allowed) await expect(access).resolves.toEqual({ file });+ else await expect(access).rejects.toThrow('PERMISSION_DENIED');+ });// Outer transport success must not mask the producing tool's inner failure.it.each(['tool_use', 'tool_result'])('checks wrapped tool failures through the %s authorization route', async (role) => {const file = path.resolve('deliveries/report.pdf');diff --git a/apps/desktop/src/main/device-link/sharedTaskFileReferences.ts b/apps/desktop/src/main/device-link/sharedTaskFileReferences.tsindex e3f0e40b36..d705b3e87d 100644--- a/apps/desktop/src/main/device-link/sharedTaskFileReferences.ts+++ b/apps/desktop/src/main/device-link/sharedTaskFileReferences.ts@@ -6,7 +6,7 @@ import remarkParse from 'remark-parse';import { visit } from 'unist-util-visit';import { describeToolUse, createdPathsFromDescriptor } from '@cindy/maker-shared/tool-use-descriptor';import { parseMessageToolUse } from '@cindy/maker-shared/message-normalize';-import { isToolResultFailureLike } from '@cindy/maker-shared/message-presentation';+import { isContentToolFailureLike } from '@cindy/maker-shared/message-presentation';import { parseToolResultPayload } from '@cindy/maker-shared/payload-summary';import { extractCommandOutputPathCandidates } from '../../shared/commandOutputPaths.js';import { findPathMatches } from '../../shared/bareFilePathTokens.js';@@ -27,12 +27,31 @@ function decode(value: string): unknown {}/** Transport success does not override a failure inside the tool's result. */-function toolResultFailed(descriptor: ReturnType<typeof describeToolUse>, content: unknown): boolean {+function toolResultFailed(content: unknown): boolean {let value = content;for (;;) {- if (isToolResultFailureLike(descriptor, typeof value === 'string' ? value : JSON.stringify(value))) return true;- const envelope = record(typeof value === 'string' ? decode(value) : value);- if (envelope?.ok !== true || !record(envelope.result)) return false;+ const parsed = typeof value === 'string' ? decode(value) : value;+ const envelope = record(parsed);+ // Main persists actual harness failures with this marker. For legacy plain+ // text use anchored diagnostics, never presentation's whole-body keywords.+ if (!envelope) return typeof parsed === 'string' && isContentToolFailureLike({ body: '', secondaryBody: parsed });+ if (envelope.ok === false || envelope.success === false || envelope.isError === true || envelope.is_error === true) return true;+ const status = typeof envelope.status === 'string' ? envelope.status.toLowerCase() : '';+ if (['error', 'failed', 'failure', 'denied', 'declined'].includes(status)) return true;+ if ([envelope.exitCode, envelope.exit_code].some((code) => typeof code === 'number' && Number.isFinite(code) && code !== 0)) return true;+ if (['error', 'errors', 'exception'].some((key) => {+ const error = envelope[key];+ if (typeof error === 'string') return error.trim().length > 0;+ if (Array.isArray(error)) return error.length > 0;+ return error === true || (typeof error === 'number' && error !== 0) || record(error) !== null;+ })) return true;+ // stderr, stdout and delivery URLs are data. An unqualified legacy message+ // can still carry an error diagnostic, but explicit success takes precedence.+ const success = envelope.ok === true || envelope.success === true || envelope.isError === false || envelope.is_error === false+ || envelope.exitCode === 0 || envelope.exit_code === 0 || ['success', 'completed', 'ok'].includes(status);+ if (!success && typeof envelope.message === 'string'+ && isContentToolFailureLike({ body: '', secondaryBody: envelope.message })) return true;+ if (envelope.ok !== true || !record(envelope.result)) return false;value = envelope.result;}}@@ -163,7 +182,7 @@ export async function sharedTaskReferencesFile(// Their actual outputs are authorized via invocation output paths below;// delivery metadata is a contract of MCP/dynamic tool responses only.if (descriptor.kind !== 'mcp' && descriptor.kind !== 'dynamic') continue;- if (toolResultFailed(descriptor, content)) continue;+ if (toolResultFailed(content)) continue;}if (row.role === 'tool_use') {// A queued/denied tool invocation is not delivery. Only completed output@@ -180,7 +199,7 @@ export async function sharedTaskReferencesFile(if (!results.length) continue;const result = decode(results[0].content);const descriptor = describeToolUse(tool.toolName, tool.input);- if (toolResultFailed(descriptor, result)) continue;+ if (toolResultFailed(result)) continue;if (descriptor.kind === 'command' || descriptor.kind === 'mcp') {// Shell destinations and MCP output arguments are only candidates.// Success must not expose an untouched, pre-existing destination file.diff --git a/docs/product-rules/shared-task-mode.md b/docs/product-rules/shared-task-mode.mdindex b415b36ee0..621c321cbd 100644--- a/docs/product-rules/shared-task-mode.md+++ b/docs/product-rules/shared-task-mode.md@@ -44,6 +44,7 @@- 工具结果中的普通文本链接不构成外部文件交付,包括读取、搜索、网页或 shell 返回的内容。原生工具产出沿用调用中明确声明的输出路径;MCP/动态工具的结果只认既有结构化交付字段 `xdt_media_produced`、`_xdt_model_files`,并继续核对调用配对、成功状态及当前历史边界。原生读取和 shell 输出即使形似这些字段,也不按交付解释。房主/Agent 在消息中明确交付的链接仍按上述规则处理。- 工具结果外层 `ok: true` 仅代表调用包装成功,内层 `result` 的失败状态同样阻止文件授权,调用产出路径与结果交付字段使用相同判定。房主/Agent 消息中可解析为文件链接的正文路径和完整行内代码路径沿用界面已有的链接/文件卡片语义;代码块中的示例、普通代码表达式、链接标签中的路径不提供额外授权。正文路径分词由 Renderer 与房主共用 `shared/bareFilePathTokens.ts`,仍须完整匹配请求文件。- shell 命令中的输出路径及 MCP 输入中的输出路径(如 `outPath`)只是候选,不能仅凭调用成功就授权工作目录外的旧文件。取件时还须确认目标为普通文件,修改时间落在该调用到配对结果的执行时段内;无法读取元数据、缺少有效时间、未修改的旧目标或执行时段外的修改均不能使用该调用的输入路径作为授权依据。显式消息链接及结构化交付仍按各自规则处理。+- 工具失败判定依据持久化失败标记及结构化失败字段/非零退出码,逐层检查已知的结果包装;旧纯文本结果保留首行错误诊断判断。正常交付文件名、stdout 或 stderr 中出现错误词/警告不单独构成失败,不把界面展示用的全文关键词启发式用于文件授权。- 消息按主机现有输入队列顺序处理,保留 clientId 幂等。访客仅编辑/撤回自己未处理消息,房主可处理全部未处理消息;直接更新内容,不显示“已编辑”。- 发言者身份来自经过验证的账号和成员关系,不能信任消息正文或客户端自报的身份。