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。

test(maker-shared): cover recursive error redaction; clarify get_chat_history exit redaction

Battleplusff576099d358
diff --git a/packages/lizi-mcps/src/xdt-helper/get_chat_history.ts b/packages/lizi-mcps/src/xdt-helper/get_chat_history.ts
index 79f1b673bb..7ad7986697 100644
--- a/packages/lizi-mcps/src/xdt-helper/get_chat_history.ts
+++ b/packages/lizi-mcps/src/xdt-helper/get_chat_history.ts
@@ -78,8 +78,9 @@ const DESCRIPTION = [
'可以和其他 role 一起传(例如 roles=["user","error"]), 不要求 roles 只含 error 一项。',
'error 行 content 在出口统一过 redactSensitiveTextDeep 递归脱敏(凭据值呈现为 [REDACTED]',
'或 [REDACTED_KEY], Bearer 形态保留前缀如 "Bearer [REDACTED]"); 写入侧只脱敏过 message /',
- 'sdkError, reason 等其余字段是入库原样内容, 由出口这一层兜住。仍只用于判断"这轮是不是',
- '技术性失败", 不应当作对话正文渲染给用户。',
+ 'sdkError, reason 等其余字段是入库原样内容, 但出口这一层会把整条 content 连同它们一起脱敏,',
+ '所以这里拿到的 reason 等字段同样是脱敏后的结果, 不是入库原文, 没有任何路径能取回原始值。',
+ '仍只用于判断"这轮是不是技术性失败", 不应当作对话正文渲染给用户。',
'',
'【返回原始数据】content 是 JSON.parse 后的原始结构 (按 role 形态不同, 调用方自己解析),',
'不做任何 markdown / 文本拼装。',
@@ -135,7 +136,8 @@ export function registerGetChatHistoryTool(
'只返这些 role 的消息。不传 = 默认只返问答类(user/assistant/ask_user/plan_review),' +
'排除 tool_use/tool_result/thinking/error 等技术性 role。需要技术 role 时, 在数组里显式列出。' +
'role="error" 是终端错误诊断行(默认不返), 数组里包含 "error" 即返回, 可与其他 role 混合;' +
- '其 content 在出口统一过 redactSensitiveTextDeep 递归脱敏(写入侧只脱敏过 message / sdkError)。',
+ '其 content 在出口统一过 redactSensitiveTextDeep 递归脱敏(写入侧只脱敏过 message / sdkError),' +
+ '返回的是脱敏结果而非入库原文, 不存在取回原始值的路径。',
),
include_rewound: z
.boolean()
diff --git a/packages/maker-shared/src/__tests__/errorRedaction.test.ts b/packages/maker-shared/src/__tests__/errorRedaction.test.ts
new file mode 100644
index 0000000000..2c55273f94
--- /dev/null
+++ b/packages/maker-shared/src/__tests__/errorRedaction.test.ts
@@ -0,0 +1,103 @@
+import { describe, expect, it } from 'vitest';
+import {
+ redactSensitiveText,
+ redactSensitiveTextDeep,
+} from '../errorRedaction';
+
+/**
+ * Error rows reach other processes and devices as `content` of an `error`
+ * message. Every string leaf of that structure must leave masked, whatever
+ * branch stored it: the write path only masks `message` / `sdkError`, so
+ * `reason` (and any future field) relies on this exit pass alone.
+ */
+describe('redactSensitiveTextDeep', () => {
+ it('masks credentials in nested objects and arrays', () => {
+ const input = {
+ message: 'POST failed',
+ sdkError: { output: 'api_key=sk-live-secretvalue123' },
+ reason: ['upstream said api_key=sk-live-secretvalue123'],
+ };
+
+ const output = redactSensitiveTextDeep(input) as {
+ message: string;
+ sdkError: { output: string };
+ reason: string[];
+ };
+
+ expect(output.sdkError.output).toContain('[REDACTED');
+ expect(output.sdkError.output).not.toContain('sk-live-secretvalue123');
+ expect(output.reason[0]).toContain('[REDACTED');
+ expect(output.reason[0]).not.toContain('sk-live-secretvalue123');
+ // The surrounding diagnostic context survives masking.
+ expect(output.message).toBe('POST failed');
+ });
+
+ it('preserves keys and the shape of every non-string leaf', () => {
+ const input = {
+ status: 401,
+ retriable: false,
+ upstream: null,
+ missing: undefined,
+ attempts: [1, 2],
+ note: 'token=sk-ant-secretvalue123',
+ };
+
+ const output = redactSensitiveTextDeep(input) as Record<string, unknown>;
+
+ expect(Object.keys(output).sort()).toEqual(
+ ['attempts', 'missing', 'note', 'retriable', 'status', 'upstream'].sort(),
+ );
+ expect(output.status).toBe(401);
+ expect(output.retriable).toBe(false);
+ expect(output.upstream).toBeNull();
+ expect(output.missing).toBeUndefined();
+ expect(output.attempts).toEqual([1, 2]);
+ expect(output.note).toContain('[REDACTED');
+ });
+
+ it('does not mutate the input', () => {
+ const input = { reason: 'api_key=sk-live-secretvalue123' };
+ const snapshot = JSON.parse(JSON.stringify(input));
+
+ redactSensitiveTextDeep(input);
+
+ expect(input).toEqual(snapshot);
+ });
+
+ it('is idempotent so a second exit pass cannot grow the placeholder', () => {
+ const input = { reason: 'aigw:user-42 jwt token=sk-live-secretvalue123' };
+
+ const once = redactSensitiveTextDeep(input) as { reason: string };
+ const twice = redactSensitiveTextDeep(once) as { reason: string };
+
+ expect(twice.reason).toBe(once.reason);
+ expect(twice.reason).toContain('[REDACTED');
+ expect(twice.reason).toContain('aigw:[REDACTED]');
+ });
+
+ it('returns string, number, boolean, null and undefined leaves unchanged', () => {
+ expect(redactSensitiveTextDeep('plain')).toBe('plain');
+ expect(redactSensitiveTextDeep(7)).toBe(7);
+ expect(redactSensitiveTextDeep(false)).toBe(false);
+ expect(redactSensitiveTextDeep(null)).toBeNull();
+ expect(redactSensitiveTextDeep(undefined)).toBeUndefined();
+ });
+
+ it('masks every element of a top-level array', () => {
+ const output = redactSensitiveTextDeep([
+ 'api_key=sk-live-secretvalue123',
+ 'clean',
+ { nested: 'api_key=sk-live-secretvalue123' },
+ ]) as [string, string, { nested: string }];
+
+ expect(output[0]).toContain('[REDACTED');
+ expect(output[1]).toBe('clean');
+ expect(output[2].nested).toContain('[REDACTED');
+ });
+
+ it('keeps the flat helper consistent with a single deep pass', () => {
+ const line = 'Authorization: Bearer abcdef123456 and api_key=sk-live-secretvalue123';
+
+ expect(redactSensitiveTextDeep(line)).toBe(redactSensitiveText(line));
+ });
+});