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(codex): xAI 回放加密 reasoning 时剥掉 codex 多序列化的键 (#1345)

wang22ab9f5e4983f
diff --git a/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts b/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
index 3e63db7509..472c2129ea 100644
--- a/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/codexProxyHost.test.ts
@@ -2326,7 +2326,9 @@ describe('codex proxy host', () => {
],
input: [
{ type: 'message', role: 'system', content: 'BASE_PROMPT\n\nPRODUCT_PROMPT' },
- { type: 'reasoning', id: 'rs_1', encrypted_content: 'gAAA' },
+ // summary 恒定补齐(缺省 []):xAI 要求回放的 reasoning 始终带 summary,
+ // 与 anthropic-responses-bridge 的回放形状同口径。
+ { type: 'reasoning', id: 'rs_1', summary: [], encrypted_content: 'gAAA' },
{
type: 'function_call',
id: 'ctc_1',
@@ -2464,6 +2466,93 @@ describe('codex proxy host', () => {
);
});
+ // codex 的结构体会把自己没用上的 Option 字段一并序列化(实测 `content: null`),
+ // 那是 xAI 从没发过的键;带着它回放,上游判定「blob 被改过」→ 整轮 400
+ // "Could not decode the compaction blob. Ensure it is unmodified from the compact response."
+ // (2026-08-02 实测:新会话里首个把 reasoning 回放进 input[] 的请求必挂,重试同样挂。)
+ describe('xAI 加密 reasoning 回放形状', () => {
+ async function runXaiReasoningTransforms(
+ suffix: string,
+ body: Record<string, unknown>,
+ ): Promise<Record<string, unknown>> {
+ const host = await freshCodexProxyHost();
+ const { setSessionProvider, clearSessionProvider } = await import('../session-provider-store.js');
+ mockState.createAnthropicCompatProxy.mockResolvedValueOnce({
+ url: 'http://127.0.0.1:43210',
+ dispose: vi.fn(async () => undefined),
+ });
+ await host.ensureCodexProxyReady();
+ const sessionId = `session-reasoning-shape-${suffix}`;
+ const threadId = `thread-reasoning-shape-${suffix}`;
+ host.registerComposed(sessionId, threadId, 'PRODUCT_PROMPT');
+ setSessionProvider(sessionId, 'xai');
+
+ const transforms = mockState.createAnthropicCompatProxy.mock.calls[0]?.[0]?.transformRequest ?? [];
+ const ctx = { method: 'POST', url: '/responses', headers: { 'thread-id': threadId } };
+ let current: unknown = body;
+ for (const transform of transforms) {
+ const next = transform(current, ctx);
+ if (next !== null && next !== undefined) current = next;
+ }
+ clearSessionProvider(sessionId);
+ return current as Record<string, unknown>;
+ }
+
+ const reasoningItemFrom = (input: unknown[]): Record<string, unknown> =>
+ input.find(
+ (item): item is Record<string, unknown> =>
+ typeof item === 'object' && item !== null && (item as { type?: unknown }).type === 'reasoning',
+ ) ?? {};
+
+ it('剥掉 codex 多序列化出来的键,只留 Responses 契约里的四个', async () => {
+ const out = await runXaiReasoningTransforms('strips-extra-keys', {
+ model: 'xai/grok-4.5',
+ input: [
+ {
+ type: 'reasoning',
+ id: 'rs_keep_me',
+ summary: [{ type: 'summary_text', text: 'thinking' }],
+ // codex 实际发出来的形态:自己没用上的 Option 字段照样序列化。
+ content: null,
+ internal_chat_message_metadata_passthrough: { turn_id: 't1' },
+ encrypted_content: 'BLOB-KEEP',
+ },
+ ],
+ });
+
+ const reasoning = reasoningItemFrom(out.input as unknown[]);
+ expect(Object.keys(reasoning).sort()).toEqual(['encrypted_content', 'id', 'summary', 'type']);
+ // blob 必须逐字不动 —— 改一个字节上游就解不开。
+ expect(reasoning.encrypted_content).toBe('BLOB-KEEP');
+ expect(reasoning.summary).toEqual([{ type: 'summary_text', text: 'thinking' }]);
+ expect(reasoning.id).toBe('rs_keep_me');
+ });
+
+ // 键名都在允许列表里、但 id 的值是空串:只数键名会判定「没变」,把原对象原样
+ // 发出去 —— 等于算出了规范形状又扔掉。
+ it('id 是空串时也要真的剥掉,而不是当作“没变”原样透传', async () => {
+ const out = await runXaiReasoningTransforms('empty-id', {
+ model: 'xai/grok-4.5',
+ input: [{ type: 'reasoning', id: '', summary: [], encrypted_content: 'BLOB-EMPTY-ID' }],
+ });
+
+ const reasoning = reasoningItemFrom(out.input as unknown[]);
+ expect(Object.keys(reasoning).sort()).toEqual(['encrypted_content', 'summary', 'type']);
+ expect(reasoning.encrypted_content).toBe('BLOB-EMPTY-ID');
+ });
+
+ it('codex 不发 id 时不编造一个(实测 xAI 不需要 id 也能解开 blob)', async () => {
+ const out = await runXaiReasoningTransforms('no-id', {
+ model: 'xai/grok-4.5',
+ input: [{ type: 'reasoning', summary: [], content: null, encrypted_content: 'BLOB-NO-ID' }],
+ });
+
+ const reasoning = reasoningItemFrom(out.input as unknown[]);
+ expect(Object.keys(reasoning).sort()).toEqual(['encrypted_content', 'summary', 'type']);
+ expect(reasoning.encrypted_content).toBe('BLOB-NO-ID');
+ });
+ });
+
it('leaves custom_tool_call history untouched for non-xAI requests', async () => {
const host = await freshCodexProxyHost();
const { setSessionProvider, clearSessionProvider } = await import('../session-provider-store.js');
diff --git a/apps/desktop/src/main/maker-host/codex-proxy-host.ts b/apps/desktop/src/main/maker-host/codex-proxy-host.ts
index 50648baaed..0ff38ea53f 100644
--- a/apps/desktop/src/main/maker-host/codex-proxy-host.ts
+++ b/apps/desktop/src/main/maker-host/codex-proxy-host.ts
@@ -1165,6 +1165,30 @@ function normalizeXaiInputItem(item: unknown): { item: unknown; changed: boolean
return changed ? { item: next, changed: true } : { item: base, changed: false };
}
+ // 回放的 reasoning 项必须逐字回到上游签发时的形状 —— xAI 校验不过就整轮 400
+ // "Could not decode the compaction blob. Ensure it is unmodified from the compact response."
+ // codex 的结构体会把自己没用上的 `Option` 字段一并序列化(实测 `content: null`),
+ // 那是 xAI 从没发过的键;带着它回放等于「被改过」。这里收敛成 Responses 契约里
+ // reasoning 该有的四个键 —— 与 anthropic-responses-bridge 回放的形状同口径
+ // (那条路上的 grok-4.5 一直是通的)。
+ // encrypted_content / summary / id 原样搬,一个字节都不改写。
+ if (type === 'reasoning') {
+ const next: Record<string, unknown> = { type: 'reasoning' };
+ if (typeof base.id === 'string' && base.id.length > 0) next.id = base.id;
+ next.summary = Array.isArray(base.summary) ? base.summary : [];
+ if (typeof base.encrypted_content === 'string') next.encrypted_content = base.encrypted_content;
+ // changed 必须覆盖「键在允许列表里、但值被上面丢掉了」的情况(如 id 是空串
+ // 或非 string):只数键名会让这些项拿着原值原样发出去 —— 等于算出了规范形状
+ // 又扔掉。逐字段比对 next 与 base,判定和构造才是同一套口径。
+ const changed =
+ typedFromEasy ||
+ !Array.isArray(base.summary) ||
+ ('id' in base && next.id !== base.id) ||
+ ('encrypted_content' in base && next.encrypted_content !== base.encrypted_content) ||
+ Object.keys(base).some((key) => !['type', 'id', 'summary', 'encrypted_content'].includes(key));
+ return changed ? { item: next, changed: true } : { item: base, changed: false };
+ }
+
if (type === 'message') {
let changed = typedFromEasy;
const next: Record<string, unknown> = { type: 'message' };