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: 恢复上游 413 并避免 Pi 失败重试重复输入 #5045 (#5103)

DavidShenXDccf8367b02c7
diff --git a/apps/desktop/src/main/localDb/ipc/messages.ts b/apps/desktop/src/main/localDb/ipc/messages.ts
index d2421295ef..6973773ab6 100644
--- a/apps/desktop/src/main/localDb/ipc/messages.ts
+++ b/apps/desktop/src/main/localDb/ipc/messages.ts
@@ -1865,6 +1865,22 @@ export interface MessageAgentMetaPatchResult {
next: Record<string, unknown>;
}
+/** Stable native identity of a visible user input; do not match retries by text. */
+export async function readPiUserEntry(sessionId: string, clientId: string): Promise<string | undefined> {
+ const [row] = await getDbClient().drizzle
+ .select({ agentMeta: messages.agentMeta })
+ .from(messages)
+ .innerJoin(sessions, eq(messages.sessionId, sessions.id))
+ .where(and(
+ eq(messages.sessionId, sessionId), eq(messages.clientId, clientId),
+ eq(messages.role, 'user'), isNull(messages.rewindAt),
+ or(isNull(sessions.clearedAt), gt(messages.createdAt, sessions.clearedAt)),
+ ))
+ .limit(1);
+ const id = row && parseAgentMetaRecord(row.agentMeta)?.piEntryId;
+ return typeof id === 'string' && id.length > 0 && id.length <= 128 ? id : undefined;
+}
+
/** 与 patchMessageAgentMeta 相同,但返回补丁前后的元数据供幂等账本计算。 */
export async function patchMessageAgentMetaWithResult(
sessionId: string,
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/contextOverflowRollover.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/contextOverflowRollover.test.ts
index b16cecc0a4..26e5085e1e 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/contextOverflowRollover.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/contextOverflowRollover.test.ts
@@ -1097,6 +1097,24 @@ describe('createContextOverflowRollover', () => {
expect(deps.replayUserMessage).not.toHaveBeenCalled();
});
+ it.each(['pi compact failed: 413', 'pi rpc timeout after 1000ms: compact', 'pi compact failed: upstream connection closed'])(
+ 'recovers a persisted Pi byte-limit compaction failure after its runtime closed: %s', async error => {
+ const deps = makeDeps([
+ msg('user', 'Continue', 'u1'),
+ msg('error', JSON.stringify({ message: `PI_REQUEST_BODY_RECOVERY_EXHAUSTED: ${error}` }), 'e1'),
+ ]);
+ deps.getSessionRow.mockResolvedValue({
+ status: 'active', agentKind: 'pi', remoteHostId: null, clearedAt: null,
+ sdkSessionId: '/tmp/pi-session', contextTokens: 0, contextWindow: 1_000_000,
+ model: 'test-model', providerId: 'local-test',
+ });
+ deps.getLiveSession.mockReturnValue(undefined);
+ const rollover = createContextOverflowRollover(deps);
+ await expect(rollover.prepareUnhealthySession('s1')).resolves.toBe(true);
+ expect(deps.commitRebuild).toHaveBeenCalled();
+ expect(deps.replayUserMessage).not.toHaveBeenCalled();
+ });
+
it('rebuilds before send when host auto-compact has latched a deterministic failure', async () => {
const deps = makeDeps([msg('user', '继续', 'u1'), msg('assistant', '好', 'a1')]);
deps.getSessionRow.mockResolvedValue({
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts
index 4ad9156c8d..4b1946beb9 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts
@@ -673,7 +673,7 @@ describe('maker SEND transaction', () => {
.toBeUndefined();
});
- it('links attachment messages to the accepted Pi transcript entry only for Pi attachments', async () => {
+ it('links both attachment and text inputs to accepted Pi entries for exact retries', async () => {
const { deps, session } = createDeps();
session.agentKind = 'pi';
const transaction = createMakerSendTransaction(deps);
@@ -712,7 +712,15 @@ describe('maker SEND transaction', () => {
},
},
);
- expect(deps.linkPiUserEntry).toHaveBeenCalledTimes(1);
+ expect(deps.linkPiUserEntry).toHaveBeenCalledTimes(2);
+ expect(deps.linkPiUserEntry).toHaveBeenLastCalledWith('session-1', 'plain-client', 'pi-user-entry');
+ deps.readPiUserEntry = vi.fn(async () => 'pi-user-entry');
+ await transaction.sendToAgentAccepted('session-1', { type: 'user', content: 'Plain text' }, undefined, {
+ retryUserClientId: 'plain-client',
+ persistUserMessage: { clientId: 'retry-client', content: 'Plain text' },
+ });
+ expect(deps.readPiUserEntry).toHaveBeenCalledWith('session-1', 'plain-client');
+ expect(vi.mocked(session.send).mock.lastCall?.[1]?.retryTranscriptUserEntryId).toBe('pi-user-entry');
});
it('threads scheduler origin into session.send opts and persisted agentMeta', async () => {
diff --git a/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts b/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
index 7fa0e6599b..b1ef17299f 100644
--- a/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
+++ b/apps/desktop/src/main/maker-ipc/agent-input-coordinator.ts
@@ -193,6 +193,8 @@ function hasRetryableQueuedContent(item: AgentInputQueuedMessage): boolean {
}
export interface AgentInputSendOpts {
+ /** Main-owned identity of the zero-output user turn being replaced. */
+ retryUserClientId?: string;
toolsDisabled?: boolean;
readonly [AUTO_REVIEW_SOURCE_CONTENT]?: string;
readonly [AUTO_REVIEW_USER_INTENT]?: string;
@@ -4546,6 +4548,7 @@ export class AgentInputCoordinator {
// post-dispatch acknowledgements must remain older than that marker.
const preVendorDispatchAt = Math.max(0, Date.now() - 1);
const result = await this.deps.sendToAgent(sessionId, makerUserMessage, head.createOpts, {
+ ...(head.supersedesUserClientId ? { retryUserClientId: head.supersedesUserClientId } : {}),
[AUTO_REVIEW_SOURCE_CONTENT]: head.autoReviewUserText ?? '',
messageUuid: active.messageUuid,
userName: head.userName,
diff --git a/apps/desktop/src/main/maker-ipc/cindyContextCompression.ts b/apps/desktop/src/main/maker-ipc/cindyContextCompression.ts
index 7b9133b566..96e70540cd 100644
--- a/apps/desktop/src/main/maker-ipc/cindyContextCompression.ts
+++ b/apps/desktop/src/main/maker-ipc/cindyContextCompression.ts
@@ -6,8 +6,9 @@
* Codex 的索引历史归原生运行时所有,不再尝试原地剥图。
* 不确定 → 不动。
*
- * 字节预算目前只有 Codex 能测量(本地 rollout)。Claude / Pi 没有生产者,
- * bytes 对它们只会是 unknown,决定结果只可能是 rebuild 或 none。
+ * 字节预算目前只有 Codex 能主动测量(本地 rollout)。Pi 仅在真实 HTTP 413
+ * 且原生压缩确定无法恢复时提供持久失败证据;不猜测上游上限或换算 token。
+ * Claude 的 bytes 仍是 unknown。
* 工具输出不作为独立一档;图片恢复交接仅保留有界的文本结果,不携带图片数据。
* 混合型大尾巴(可剥图不足一半)有意不救,等证据再动比例阈值,不加新档。
*
@@ -23,7 +24,8 @@ export type CompressionBudgetState = 'ok' | 'violated' | 'unknown';
* - 本机占用 ≥ 100%
* - 官方 compact 确定性失败(needsRollover:空摘要、compact 路径 invalid-request)
*
- * bytes='violated':Codex 活尾巴可剥超大内联图(>8MB 且可剥 ≥ 一半且剥完 ≤8MB)。
+ * bytes='violated':Codex 活尾巴可剥超大内联图(>8MB 且可剥 ≥ 一半且剥完 ≤8MB),
+ * 或 Pi 的真实 413 经一次原生压缩仍无法恢复。
* unknown = 没测到,不是「预算没破」。
*/
export type CindyCompressionAction = 'rebuild' | 'none';
diff --git a/apps/desktop/src/main/maker-ipc/contextOverflowRollover.ts b/apps/desktop/src/main/maker-ipc/contextOverflowRollover.ts
index 2a29b85337..861a466ce7 100644
--- a/apps/desktop/src/main/maker-ipc/contextOverflowRollover.ts
+++ b/apps/desktop/src/main/maker-ipc/contextOverflowRollover.ts
@@ -8,6 +8,7 @@
import {
CODEX_HISTORY_OVERSIZED_REASON,
CONTEXT_OVERFLOW_REASON,
+ PI_REQUEST_BODY_RECOVERY_EXHAUSTED,
isContextOverflowErrorMessage,
isRemoteCompactEncryptedContentError,
} from '@cindy/maker-core';
@@ -84,9 +85,16 @@ export function isPiPromptRpcTimeoutError(data: unknown): boolean {
);
}
+function isPiRequestBodyRecoveryExhausted(data: unknown): boolean {
+ if (!data || typeof data !== 'object') return false;
+ const rec = data as { message?: unknown; sdkError?: unknown };
+ return [rec.message, rec.sdkError].some(value =>
+ typeof value === 'string' && value.includes(PI_REQUEST_BODY_RECOVERY_EXHAUSTED));
+}
+
/** PI 原生会话已经无法继续:超限,或 prompt RPC 超时(巨大 jsonl resume 卡死)。 */
export function shouldRebuildPiNativeSession(data: unknown): boolean {
- return isContextOverflowErrorData(data) || isPiPromptRpcTimeoutError(data);
+ return isContextOverflowErrorData(data) || isPiPromptRpcTimeoutError(data) || isPiRequestBodyRecoveryExhausted(data);
}
const GROK_4_CONTEXT_CAP = 500_000;
@@ -302,6 +310,7 @@ export function findLatestRebuildableError(
const data = errorContentToData(message.content);
if (isContextOverflowErrorData(data)) return data;
if (allowPiPromptTimeout && isPiPromptRpcTimeoutError(data)) return data;
+ if (allowPiPromptTimeout && isPiRequestBodyRecoveryExhausted(data)) return data;
if (isOversizedHistoryErrorData(data)) return data;
return null;
}
@@ -776,6 +785,7 @@ export function createContextOverflowRollover(deps: ContextOverflowRolloverDeps)
const window = effectiveContextWindow(sessionRow.model, reportedWindow, verified);
const pressure = shouldRebuildForContextPressure(usedTokens, window);
const compactFailed = liveUsage?.needsRollover === true;
+ const piByteRecoveryFailed = sessionRow.agentKind === 'pi' && isPiRequestBodyRecoveryExhausted(lastError);
const tokenViolated =
isContextOverflowErrorData(lastError) ||
isPiPromptRpcTimeoutError(lastError) ||
@@ -787,7 +797,7 @@ export function createContextOverflowRollover(deps: ContextOverflowRolloverDeps)
: undefined;
const action = decideCindyCompression({
local: true,
- bytes: oversized && historyHealth !== 'healthy' ? 'violated' : 'unknown',
+ bytes: piByteRecoveryFailed || (oversized && historyHealth !== 'healthy') ? 'violated' : 'unknown',
tokens: tokenViolated ? 'violated' : 'unknown',
});
if (action !== 'rebuild') return false;
diff --git a/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts b/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts
index 56af7617ae..70ea5577cf 100644
--- a/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts
+++ b/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts
@@ -232,6 +232,7 @@ export function revokeTrustedDesktopQueuedOrigin(item: AgentInputQueuedMessage):
}
type MakerSendOptions = {
+ retryUserClientId?: string;
toolsDisabled?: boolean;
readonly [AUTO_REVIEW_SOURCE_CONTENT]?: UserMessage['content'];
/** Main-only continuation: a restored intent is not an authored user turn. */
@@ -437,6 +438,7 @@ export interface MakerSendTransactionDeps {
) => boolean;
/** 把 Pi 原生 user entry id 补到已落库的 Cindy user 行,供会话树恢复附件。 */
linkPiUserEntry?(sessionId: string, clientId: string, piEntryId: string): Promise<boolean | void>;
+ readPiUserEntry?(sessionId: string, clientId: string): Promise<string | undefined>;
beforeDispatchDirectUserTurn?: (sessionId: string) => void | Promise<void>;
/** Capture product lifecycle state before async preparation; commit only at vendor dispatch. */
prepareProductTurn?: (sessionId: string) => (() => void) | undefined;
@@ -1450,7 +1452,11 @@ export function createMakerSendTransaction(deps: MakerSendTransactionDeps): Make
const interruptedAckAt = so.ackInterruptedTurnOnDispatch
? Math.max(0, Date.now() - 1)
: null;
+ const retryTranscriptUserEntryId = sess.agentKind === 'pi' && so.retryUserClientId
+ ? await deps.readPiUserEntry?.(sessionId, so.retryUserClientId)
+ : undefined;
const sendResult = await sess.send(outgoing as never, {
+ ...(retryTranscriptUserEntryId ? { retryTranscriptUserEntryId } : {}),
...(resolveScheduledIntent ? { resolveAutoReviewUserIntent: resolveScheduledIntent } : {}),
[AUTO_REVIEW_SOURCE_CONTENT]: autoReviewSourceContent,
...(so[INHERITED_CAPABILITY_SELECTION] !== undefined
@@ -1489,7 +1495,6 @@ export function createMakerSendTransaction(deps: MakerSendTransactionDeps): Make
: {}),
...(sess.agentKind === 'pi' &&
persistUserMessage &&
- containsManagedAttachment(persistUserMessage.content) &&
deps.linkPiUserEntry
? {
onTranscriptUserEntry: async (piEntryId: string) => {
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index fc1db14608..94baec600b 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -285,6 +285,7 @@ import {
listMessagesForAgentHandoff,
findLatestUserMessageForRebuild,
patchMessageAgentMeta,
+ readPiUserEntry,
supersedeRetriedUserTurn,
updateMessageContent,
} from '../localDb/ipc/messages.js';
@@ -12598,6 +12599,7 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
}
return coordinator.getClearBoundaryMs(sessionId) === expected;
},
+ readPiUserEntry,
linkPiUserEntry: (sessionId, clientId, piEntryId) =>
enqueueDurableWrite(`pi-entry-link:${sessionId}:${clientId}`, () =>
patchMessageAgentMeta(sessionId, clientId, { piEntryId }),
diff --git a/docs/dev-rules/media-storage-and-protocols.md b/docs/dev-rules/media-storage-and-protocols.md
index b65562597b..d6ce6efcce 100644
--- a/docs/dev-rules/media-storage-and-protocols.md
+++ b/docs/dev-rules/media-storage-and-protocols.md
@@ -104,12 +104,15 @@
## 模型请求超限时的附件恢复
-- 只在本地代理收到的请求实际超过既有字节上限后恢复;正常请求不因图片数量、预估
+- 只在本地代理收到的请求实际超过既有字节上限,或上游明确返回 HTTP 413 后恢复;正常请求不因图片数量、预估
token 或接近上限而丢弃附件。上限本身保持不变。
- 超限请求先分块落 Host 私有临时文件,base64 字符串不论单个大小都参与恢复,并分块解码。
临时写入按实际可用磁盘共享预算,预留 256 MiB,并为解析、解码和保存预留副本空间;
不另设固定附件体积门槛。磁盘不足返回明确的 507,可清理空间后重试,原始历史不变。按历史顺序保留
足够数量的旧附件为本地文件,使请求重新小于上限;无需移出全部附件。
+- 上游 413 不提供可靠的字节上限:保全全部可识别的内联附件,请求确实缩小后最多重发一次,
+ 重发前重新验证原 dispatch gate。不猜测供应商上限,不把字节换算成 token;纯文本、
+ 保全失败或不能缩小时原样返回该 413,客户端停止则取消保全与重发。
- 已识别媒体经 `ingestMedia` 挂 `session-attachment` 持久引用;其他文件复用
`hook-attachments/<sessionId>` 生命周期。必须保存成功后才用真实绝对路径替换内容,
并明确告诉模型当前消息不再包含该附件内容,需要时重新读取。
diff --git a/docs/dev-rules/pi-harness.md b/docs/dev-rules/pi-harness.md
index 65cff37626..20352bc903 100644
--- a/docs/dev-rules/pi-harness.md
+++ b/docs/dev-rules/pi-harness.md
@@ -102,6 +102,16 @@ provider/model/contextWindow,因为 Pi 会用进程初始 CLI route 重建
外部 MCP 专用动态 env、`PI_OFFLINE=1`(关启动期联网)、`NO_PROXY` 兜底 loopback(防全局代理
打穿本地 proxy 与 MCP bridge)。
+用户重试已被 Pi 接受但零产出的失败输入时,用持久化的原生 user entry id 精确定位当前
+分支;确认后续只有空的错误响应及模型/推理档位变更记录,再通过原生树导航回到输入之前重发。
+导航只回退消息,保留当前模型和推理档位;重新打开任务时仍由 Cindy 已保存的选择恢复设置。
+回归见 `session-tree.test.ts` 与 `pi-provider-routing.test.ts`。旧失败分支完整保留,
+不按文本去重,也不重放已有工具结果。若该轮明确遇到 HTTP 413,用户这次重试在重发前
+调用一次原生 compact;压缩失败(包括 RPC 超时、普通错误或无法压缩)时,沿用原请求的
+413 证据,本机锁存下一次发送前换窗,并在持久错误中记录专用恢复失败标记,关闭运行时后
+仍能恢复。用户取消不触发换窗,SSH 保留错误。
+这一恢复由用户重试触发,不改变正常轮次由 Pi 自己管理自动压缩的约定。
+
Pi 同样消费 `AgentRuntimeConfig.behaviorFlags`(静态对象或按来源、凭证形态、执行位置求值)。
Desktop 复用既有工具链并行度设置,向本机 Pi 注入 `VITEST_MAX_FORKS`、`VITEST_MAX_THREADS`、
`CARGO_BUILD_JOBS` 与非 Windows 的 `MAKEFLAGS`;用户已有 env 优先,关闭设置后新进程不注入,
diff --git a/packages/anthropic-compat-proxy/src/oversized-attachments.ts b/packages/anthropic-compat-proxy/src/oversized-attachments.ts
index e109cfca83..7fdab6bc42 100644
--- a/packages/anthropic-compat-proxy/src/oversized-attachments.ts
+++ b/packages/anthropic-compat-proxy/src/oversized-attachments.ts
@@ -3,12 +3,14 @@ import { createReadStream } from 'node:fs';
import { mkdtemp, open, rm, statfs, type FileHandle } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
-import type { IncomingMessage } from 'node:http';
+import type { Readable } from 'node:stream';
export interface OversizedBody {
filePath: string;
bytes: number;
signal: AbortSignal;
+ /** The upstream limit is unknown: externalize all eligible attachments once. */
+ upstreamRejected?: boolean;
/** Account for decoded/published file allocation overhead while recovery is active. */
reserveAttachment?: () => Promise<void>;
}
@@ -46,7 +48,7 @@ async function reserveDisk(bytes: number): Promise<number> {
/** Normal requests remain byte-for-byte unchanged. Only overflow spills to disk. */
export async function collectRecoverableBody(
- req: IncomingMessage,
+ req: Readable,
limit: number,
recover: (body: OversizedBody) => Promise<Buffer | null>,
signal: AbortSignal,
@@ -309,7 +311,7 @@ export async function recoverInlineAttachments(
let estimatedBytes = body.bytes;
let replaced = 0;
for (const candidate of candidates) {
- if (estimatedBytes <= targetBytes) break;
+ if (!body.upstreamRejected && estimatedBytes <= targetBytes) break;
body.signal.throwIfAborted();
const part = parts.get(candidate.marker)!;
await body.reserveAttachment?.();
diff --git a/packages/anthropic-compat-proxy/src/oversized-request-server.test.ts b/packages/anthropic-compat-proxy/src/oversized-request-server.test.ts
index 4cf67d010e..ec1e6adf4a 100644
--- a/packages/anthropic-compat-proxy/src/oversized-request-server.test.ts
+++ b/packages/anthropic-compat-proxy/src/oversized-request-server.test.ts
@@ -11,6 +11,87 @@ const recoverInlineAttachments = (body: Parameters<typeof recover>[0], limit: nu
const cleanup: Array<() => Promise<void>> = [];
afterEach(async () => { for (const close of cleanup.splice(0).reverse()) await close(); });
+it.each(['recover', 'still-413', 'storage-failure', 'text', 'not-smaller'] as const)(
+ 'handles upstream 413 below the local limit without an unbounded retry: %s', async mode => {
+ const bodies: Buffer[] = [];
+ const error = 'Failed to buffer the request body: length limit exceeded';
+ const upstream = createServer(async (req, res) => {
+ const chunks: Buffer[] = [];
+ for await (const chunk of req) chunks.push(chunk);
+ const body = Buffer.concat(chunks);
+ bodies.push(body);
+ res.statusCode = mode === 'recover' && body.length < 1000 ? 200 : 413;
+ res.end(res.statusCode === 200 ? 'ok' : error);
+ });
+ const port = await listenOnAvailableLoopbackPort(upstream);
+ cleanup.push(() => new Promise(resolve => upstream.close(() => resolve())));
+ const original = Buffer.alloc(8000, 2);
+ const keep = vi.fn(async (attachment: RecoveredAttachment) => {
+ if (mode === 'storage-failure') throw new Error('disk full');
+ expect(await readFile(attachment.filePath)).toEqual(original);
+ return '/tmp/kept.png';
+ });
+ const raw = JSON.stringify({ messages: [{ role: 'user', content: mode === 'text'
+ ? 'x'.repeat(12000)
+ : [1, 2].map(() => ({ type: 'image_url', image_url: { url: `data:image/png;base64,${original.toString('base64')}` } })) }] });
+ const proxy = await createAnthropicCompatProxy({
+ upstream: `http://127.0.0.1:${port}`,
+ oversizedRequestRecovery: () => (body, limit) => mode === 'not-smaller'
+ ? Promise.resolve(Buffer.from(raw))
+ : recoverInlineAttachments(body, limit, keep),
+ });
+ cleanup.push(() => proxy.dispose());
+ const response = await fetch(`${proxy.url}/v1/chat/completions`, {
+ method: 'POST', headers: { 'content-type': 'application/json' }, body: raw,
+ });
+ expect(response.status).toBe(mode === 'recover' ? 200 : 413);
+ expect(await response.text()).toBe(mode === 'recover' ? 'ok' : error);
+ expect(bodies).toHaveLength(mode === 'recover' || mode === 'still-413' ? 2 : 1);
+ expect(bodies[0]).toEqual(Buffer.from(raw));
+ if (bodies.length === 2) {
+ expect(keep).toHaveBeenCalledTimes(2);
+ expect(bodies[1].length).toBeLessThan(1000);
+ expect(bodies[1].toString()).not.toContain('base64');
+ }
+ },
+);
+
+it('cancels upstream 413 preservation without dispatching a retry', async () => {
+ let release!: () => void;
+ let entered!: () => void;
+ const started = new Promise<void>(resolve => { entered = resolve; });
+ const gate = new Promise<void>(resolve => { release = resolve; });
+ const upstreamCall = vi.fn();
+ const upstream = createServer(async (req, res) => {
+ for await (const _ of req) { /* consume */ }
+ upstreamCall();
+ res.writeHead(413); res.end('too large');
+ });
+ const port = await listenOnAvailableLoopbackPort(upstream);
+ cleanup.push(() => new Promise(resolve => upstream.close(() => resolve())));
+ let signal: AbortSignal | undefined;
+ const proxy = await createAnthropicCompatProxy({
+ upstream: `http://127.0.0.1:${port}`,
+ oversizedRequestRecovery: () => async body => {
+ signal = body.signal; entered(); await gate;
+ body.signal.throwIfAborted();
+ return Buffer.from('{}');
+ },
+ });
+ cleanup.push(() => proxy.dispose());
+ const abort = new AbortController();
+ const pending = fetch(`${proxy.url}/v1/messages`, {
+ method: 'POST', headers: { 'content-type': 'application/json' },
+ body: '{"messages":[]}', signal: abort.signal,
+ }).catch(() => undefined);
+ await started;
+ abort.abort();
+ await pending;
+ await vi.waitFor(() => expect(signal?.aborted).toBe(true));
+ release();
+ expect(upstreamCall).toHaveBeenCalledOnce();
+});
+
it.each(['/v1/responses', '/v1/responses/compact', '/v1/messages', '/v1/chat/completions'])('recovers declared and chunked oversized requests before dispatch: %s', async endpoint => {
const bodies: string[] = [];
const upstream = createServer(async (req, res) => {
diff --git a/packages/anthropic-compat-proxy/src/server.ts b/packages/anthropic-compat-proxy/src/server.ts
index 42075bd5be..feba88dbc4 100644
--- a/packages/anthropic-compat-proxy/src/server.ts
+++ b/packages/anthropic-compat-proxy/src/server.ts
@@ -19,7 +19,7 @@ import { createHash } from 'node:crypto';
import { createServer, request as httpRequest, type ClientRequest, type IncomingMessage, type RequestOptions, type Server, type ServerResponse } from 'node:http';
import { request as httpsRequest } from 'node:https';
import type { Socket, TcpSocketConnectOpts } from 'node:net';
-import { PassThrough, type Transform } from 'node:stream';
+import { PassThrough, Readable, type Transform } from 'node:stream';
import { URL } from 'node:url';
import { brotliDecompressSync, gunzipSync, inflateRawSync, inflateSync } from 'node:zlib';
@@ -950,6 +950,7 @@ function forward(
beforeRetry?: () => Promise<boolean>,
// Optional request-local operational observer. It never receives raw request/response data.
forwardLifecycle?: ForwardLifecycleObserver | null,
+ recoverUpstreamOversized?: (body: Buffer, signal: AbortSignal) => Promise<Buffer>,
): void {
// Diagnostics are a strict side channel: callback failures must never change forwarding.
const notifyForwardLifecycle = (notify: () => void): void => {
@@ -1135,7 +1136,8 @@ function forward(
const activeRules = (status === 400 || status === 422)
? recoveryRules.filter((r) => r.enabled() && (canRetry || r.unrecoverableCode))
: [];
- if (activeRules.length > 0) {
+ const recover413 = status === 413 && canRetry && recoverUpstreamOversized;
+ if (activeRules.length > 0 || recover413) {
const chunks: Buffer[] = [];
const failBufferedResponse = (
reason: 'error' | 'aborted' | 'close',
@@ -1173,7 +1175,7 @@ function forward(
if (upstreamResponseTerminal !== null) return;
chunks.push(chunk);
});
- upstreamRes.on('end', () => {
+ upstreamRes.on('end', async () => {
if (upstreamResponseTerminal !== null) return;
upstreamResponseTerminal = 'end';
if (clientAborted || clientRes.destroyed || upstreamFailureHandled) return;
@@ -1183,6 +1185,43 @@ function forward(
// (回客户端仍是原始 errBody + content-encoding 头透传, 客户端自己解压, 见下方 clientRes.end)
const decodedErrBody = decodeBodyForLog(errBody, String(upstreamRes.headers['content-encoding'] ?? ''));
const decodedText = decodedErrBody.toString('utf8');
+ if (recover413) {
+ const recoveryAbort = new AbortController();
+ const abortRecovery = (): void => recoveryAbort.abort();
+ clientRes.once('close', abortRecovery);
+ let recovered: Buffer | undefined;
+ try {
+ recovered = await recover413(body, recoveryAbort.signal);
+ } catch {
+ // Storage failure, unsupported content and cancellation must never
+ // replace the original 413 with an unrelated recovery error.
+ logger.info?.('upstream 413 attachment recovery unavailable', { reqId });
+ } finally {
+ clientRes.off('close', abortRecovery);
+ }
+ if (clientRes.destroyed || clientRes.writableEnded) return;
+ if (recovered && recovered.length < body.length) {
+ try {
+ if (beforeRetry && !(await beforeRetry())) return;
+ } catch {
+ recovered = undefined;
+ }
+ if (clientRes.destroyed || clientRes.writableEnded) return;
+ if (recovered) {
+ logger.info?.('upstream 413 attachment recovery retry', {
+ reqId, originalBytes: body.length, recoveredBytes: recovered.length,
+ });
+ forward(
+ target, method, path, headers, recovered, clientRes, logger,
+ recoveryRules, reqId, false, overrideTarget, headerOverride,
+ headerDelete, responseObserver, transformResponse, clientModel,
+ outboundProxy, pathOverride, responseToolUseIds, threadMintedIdCache,
+ requestDeclaredStream, beforeRetry, forwardLifecycle,
+ );
+ return;
+ }
+ }
+ }
// 取第一条 match 命中且 strip 出东西的规则;命中错误文案但没东西可删 → 试下一条。
for (const [matchedIndex, rule] of activeRules.entries()) {
if (!canRetry) break;
@@ -2600,6 +2639,14 @@ export async function createAnthropicCompatProxy(opts: ProxyOptions): Promise<Pr
requestDeclaredStream,
beforeRetry,
forwardLifecycle,
+ recoverOversized
+ ? (body, signal) => collectRecoverableBody(
+ Readable.from([body]),
+ body.length - 1,
+ (spooled) => recoverOversized!({ ...spooled, upstreamRejected: true }, body.length - 1),
+ signal,
+ )
+ : undefined,
);
});
diff --git a/packages/maker-core/src/agents/base-agent.ts b/packages/maker-core/src/agents/base-agent.ts
index 75763c41d1..1caad60746 100644
--- a/packages/maker-core/src/agents/base-agent.ts
+++ b/packages/maker-core/src/agents/base-agent.ts
@@ -1742,6 +1742,9 @@ export class TurnDispatchUnconfirmedError extends Error {
}
}
+/** Persistable evidence that native Pi compaction could not recover an upstream byte limit. */
+export const PI_REQUEST_BODY_RECOVERY_EXHAUSTED = 'PI_REQUEST_BODY_RECOVERY_EXHAUSTED';
+
/** The provider explicitly rejected the turn before accepting any work. */
export class TurnDispatchRejectedError extends Error {
readonly code = 'TURN_DISPATCH_REJECTED';
@@ -1995,6 +1998,8 @@ export interface SendOptions {
* 回调失败不得改变已经接受的 provider dispatch 结果。
*/
onTranscriptUserEntry?: (entryId: string) => void | Promise<void>;
+ /** Exact accepted Pi input replaced by a zero-output retry; never match by text. */
+ retryTranscriptUserEntryId?: string;
/**
* 当前用户的展示名 (host / renderer 在调 send 时提供)。仅用于 turn-start 时
* push status event 的文案 — agent 拼成 "<userName> Just Wait ..." 让 UI 个人化;
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts
index 2889c2371b..cb10581b29 100644
--- a/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-provider-routing.test.ts
@@ -4835,6 +4835,82 @@ describe("Pi provider-aware model routing", () => {
await handle.close();
});
+ it.each(['413', 'settings-changed', 'other-error', 'compaction-413', 'compaction-timeout', 'compaction-error', 'compaction-cancelled', 'cancelled-after-compaction', 'nothing-to-compact', 'cancelled-navigation', 'new-window', 'output'])(
+ 'prepares an identity-bound Pi retry without duplicating accepted input: %s', async mode => {
+ let navigated = false;
+ const controller = new AbortController();
+ captured.requestHandler = async command => {
+ if (command.type === 'get_state') return { success: true, data: {
+ sessionFile: '/mock/s.jsonl', model: { contextWindow: 200_000 },
+ } };
+ if (command.type === 'get_tree') return { success: true, data: {
+ leafId: navigated || mode === 'new-window' ? 'parent' : mode === 'settings-changed' ? 'effort' : 'failure',
+ tree: [{ entry: { id: 'parent', type: 'message', message: { role: 'assistant' } }, children: [
+ { entry: { id: 'accepted', parentId: 'parent', type: 'message', message: { role: 'user' } }, children: [
+ { entry: { id: 'failure', parentId: 'accepted', type: 'message', message: {
+ role: 'assistant', stopReason: 'error',
+ content: mode === 'output' ? [{ type: 'toolCall', name: 'bash' }] : [],
+ errorMessage: mode === 'other-error' ? 'network error' : '413 length limit exceeded',
+ } }, children: mode === 'settings-changed' ? [{
+ entry: { id: 'model', parentId: 'failure', type: 'model_change', provider: 'native-a', modelId: 'local-model' },
+ children: [{ entry: { id: 'effort', parentId: 'model', type: 'thinking_level_change', thinkingLevel: 'high' }, children: [] }],
+ }] : [] },
+ ] },
+ ] }],
+ } };
+ if (command.type === 'prompt' && String(command.message).startsWith('/cindy-branch-switch ')) {
+ navigated = mode !== 'cancelled-navigation';
+ return { success: true };
+ }
+ if (command.type === 'compact' && mode === 'compaction-413') {
+ return { success: false, error: '413 length limit exceeded' };
+ }
+ if (command.type === 'compact' && mode === 'compaction-timeout') {
+ throw new PiRpcRequestTimeoutError('compact', 1_000);
+ }
+ if (command.type === 'compact' && mode === 'compaction-error') {
+ return { success: false, error: 'upstream connection closed' };
+ }
+ if (command.type === 'compact' && mode === 'compaction-cancelled') {
+ controller.abort();
+ throw new Error('Compaction aborted');
+ }
+ if (command.type === 'compact' && mode === 'cancelled-after-compaction') {
+ controller.abort();
+ }
+ if (command.type === 'compact' && mode === 'nothing-to-compact') {
+ return { success: false, error: 'Nothing to compact' };
+ }
+ return { success: true, data: {} };
+ };
+ const agent = new PiAgent(byomDeps(async () => ({ providers: [], env: {} })));
+ const handle = await agent.startSession({ sessionId: 'retry', workingDir: cwd, model: 'local-model' });
+ const retryStart = captured.requests.length;
+ const promise = handle.send({ type: 'user', content: 'same input' }, {
+ retryTranscriptUserEntryId: 'accepted', signal: controller.signal,
+ });
+ const needsRollover = ['compaction-413', 'compaction-timeout', 'compaction-error', 'nothing-to-compact'].includes(mode);
+ const cancelled = ['compaction-cancelled', 'cancelled-after-compaction'].includes(mode);
+ const blocked = needsRollover || cancelled || ['cancelled-navigation', 'output'].includes(mode);
+ if (needsRollover) await expect(promise).rejects.toThrow('PI_REQUEST_BODY_RECOVERY_EXHAUSTED');
+ else if (cancelled) await expect(promise).rejects.toThrow('cancelled before acceptance');
+ else if (blocked) await expect(promise).rejects.toThrow();
+ else await promise;
+ expect(captured.requests.filter(r => r.type === 'prompt' && r.message === 'same input')).toHaveLength(blocked ? 0 : 1);
+ expect(captured.requests.filter(r => r.type === 'compact')).toHaveLength(
+ needsRollover || cancelled || ['413', 'settings-changed'].includes(mode) ? 1 : 0,
+ );
+ expect(handle.getUsageSnapshot?.().needsRollover === true).toBe(
+ needsRollover,
+ );
+ // Tree navigation preserves live settings; retry must not restore the
+ // model/effort that was selected when the failed input was first sent.
+ expect(captured.requests.slice(retryStart).filter(r =>
+ r.type === 'set_model' || r.type === 'set_thinking_level')).toEqual([]);
+ await handle.close();
+ },
+ );
+
it("reports the stable Pi user entry id after prompt acceptance", async () => {
let promptAccepted = false;
captured.requestHandler = async (command) => {
diff --git a/packages/maker-core/src/agents/pi/__tests__/session-tree.test.ts b/packages/maker-core/src/agents/pi/__tests__/session-tree.test.ts
index a43b193783..c60d4c974d 100644
--- a/packages/maker-core/src/agents/pi/__tests__/session-tree.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/session-tree.test.ts
@@ -4,6 +4,7 @@ import {
activePiHistoryFromTree,
normalizePiSessionTree,
piContextTokensFromTree,
+ piRetryBranch,
userDraftTextFromPiEntry,
} from '../session-tree.js';
@@ -56,6 +57,50 @@ const treeData = {
};
describe('pi session tree adapter', () => {
+ function retryTree(tail: Array<Record<string, unknown>>) {
+ const entries = [
+ { type: 'message', message: { role: 'user', content: 'retry me' } },
+ ...tail,
+ ];
+ const nodes = entries.map((entry, i) => ({
+ entry: { ...entry, id: String(i), parentId: i ? String(i - 1) : null },
+ children: [] as unknown[],
+ }));
+ nodes.forEach((node, i) => { if (i) nodes[i - 1].children.push(node); });
+ return { tree: [nodes[0]], leafId: String(nodes.length - 1) };
+ }
+ const emptyFailure = {
+ type: 'message', message: {
+ role: 'assistant', stopReason: 'error', content: [], errorMessage: '413 length limit exceeded',
+ },
+ };
+ const settings = [
+ { type: 'model_change', provider: 'local', modelId: 'replacement' },
+ { type: 'thinking_level_change', thinkingLevel: 'high' },
+ ];
+
+ it('allows model and effort changes around an empty failure without mutating history', () => {
+ const tree = retryTree([...settings, emptyFailure, ...settings]);
+ const before = structuredClone(tree);
+ expect(piRetryBranch(tree, '0')).toEqual({ parentId: null, requestTooLarge: true });
+ expect(tree).toEqual(before);
+ });
+
+ it('does not mistake settings alone for a failed response', () => {
+ expect(() => piRetryBranch(retryTree(settings), '0')).toThrow('no failed response');
+ });
+
+ it.each([
+ { type: 'message', message: { role: 'assistant', stopReason: 'error', content: [{ type: 'text', text: 'partial' }] } },
+ { type: 'message', message: { role: 'assistant', stopReason: 'error', content: [{ type: 'toolCall', name: 'bash' }] } },
+ { type: 'message', message: { role: 'toolResult', content: [] } },
+ { type: 'custom', customType: 'plan-state' },
+ { type: 'compaction', summary: 'progress' },
+ ])('still protects output and other session state after settings changes: %j', entry => {
+ expect(() => piRetryBranch(retryTree([emptyFailure, ...settings, entry]), '0'))
+ .toThrow('discard output or session state');
+ });
+
it('restores gateway calls using the same tool contract as live events', () => {
const data = structuredClone(treeData);
const block = data.tree[0].children[0].entry.message.content[2];
diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.ts
index 8fb669c927..0c90bda355 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -67,6 +67,7 @@ import {
BaseAgent,
MAIN_OWNED_SEND_CONTEXT,
PINNED_SKILL_INVOCATION,
+ PI_REQUEST_BODY_RECOVERY_EXHAUSTED,
PiManagedPackageMutationCancelledError,
PiManagedPackageMutationFailedError,
projectPiPackageCommandDiagnostic,
@@ -232,6 +233,7 @@ import {
activePiHistoryFromTree,
findPiTreeEntry,
normalizePiSessionTree,
+ piRetryBranch,
piContextTokensFromTree,
userDraftTextFromPiEntry,
} from './session-tree.js';
@@ -6724,6 +6726,48 @@ export class PiAgent extends BaseAgent {
if (images.length > 0) command.images = images;
// send 语义 = 排队开新 turn;pi streaming 中裸 prompt 会被拒,补 followUp。
if (ctx.isStreaming) command.streamingBehavior = 'followUp';
+ if (sendOpts?.retryTranscriptUserEntryId && !ctx.isStreaming) {
+ await runExclusivePiRpc(async () => {
+ const before = await proc.request({ type: 'get_tree' });
+ if (!before.success) throw new TurnDispatchRejectedError('Pi retry history unavailable');
+ const branch = piRetryBranch(before.data, sendOpts.retryTranscriptUserEntryId!);
+ if (!branch) return;
+ rejectIfCancelled(sendOpts, 'send');
+ // Native navigation preserves the old failed branch in the append-only
+ // transcript. Only the active model context is replaced by this retry.
+ const payload = encodeURIComponent(JSON.stringify({ entryId: sendOpts.retryTranscriptUserEntryId }));
+ const switched = await proc.request({ type: 'prompt', message: `/cindy-branch-switch ${payload}` });
+ const after = await proc.request({ type: 'get_tree' });
+ if (!switched.success || !after.success || normalizePiSessionTree(after.data).leafId !== branch.parentId) {
+ throw new TurnDispatchRejectedError('Pi retry branch navigation was not confirmed');
+ }
+ rejectIfCancelled(sendOpts, 'send');
+ if (branch.requestTooLarge) {
+ // A user-requested retry after a byte-limit rejection gets one native
+ // compaction attempt. Never invent a provider byte/token threshold.
+ try {
+ const result = await requestPiCompact();
+ rejectIfCancelled(sendOpts, 'send');
+ if (result.noop) {
+ throw new Error('Pi cannot compact the request rejected with HTTP 413');
+ }
+ } catch (error) {
+ rejectIfCancelled(sendOpts, 'send');
+ // The original failed response already proved a byte-limit
+ // rejection. Any non-cancelled compaction failure leaves that
+ // recovery incomplete, even if its own error is a timeout.
+ if (!opts.remoteHostId) {
+ nativeAutoCompactNeedsRollover = true;
+ // A rejected send can close this runtime. Preserve the specific
+ // recovery evidence in the durable error, not just this handle.
+ throw new Error(`${PI_REQUEST_BODY_RECOVERY_EXHAUSTED}: ${String(error)}`);
+ }
+ throw error;
+ }
+ }
+ rejectIfCancelled(sendOpts, 'send');
+ });
+ }
const userEntriesBefore = sendOpts?.onTranscriptUserEntry
? await readPiUserEntryIds()
: null;
diff --git a/packages/maker-core/src/agents/pi/session-tree.ts b/packages/maker-core/src/agents/pi/session-tree.ts
index 986cf99655..786071ec2a 100644
--- a/packages/maker-core/src/agents/pi/session-tree.ts
+++ b/packages/maker-core/src/agents/pi/session-tree.ts
@@ -140,6 +140,39 @@ export function normalizePiSessionTree(data: unknown): SessionTreeSnapshot {
return { roots, leafId, activePathIds: reversed.reverse() };
}
+/** Retry empty failures, allowing native model/effort changes that do not produce output. */
+export function piRetryBranch(data: unknown, entryId: string): {
+ parentId: string | null; requestTooLarge: boolean;
+} | null {
+ const tree = normalizePiSessionTree(data);
+ const index = tree.activePathIds.indexOf(entryId);
+ // A rollover/fork can replace the native window before the host dispatches.
+ if (index < 0) return null;
+ const entries = rawNodeMap(data);
+ const entry = entries.get(entryId);
+ if (entry?.type !== 'message' || recordOf(entry.message)?.role !== 'user') return null;
+ const tail = tree.activePathIds.slice(index + 1).map(id => entries.get(id));
+ if (!tail.length) return null;
+ let requestTooLarge = false;
+ let hasFailedResponse = false;
+ for (const item of tail) {
+ // Native navigateTree replaces messages only; it keeps the currently selected
+ // model and thinking level. Their history entries are not generated output.
+ if (item?.type === 'model_change' || item?.type === 'thinking_level_change') continue;
+ const message = recordOf(item?.message);
+ if (item?.type !== 'message' || message?.role !== 'assistant' ||
+ message.stopReason !== 'error' || !Array.isArray(message.content) || message.content.length !== 0) {
+ throw new Error('Pi retry would discard output or session state');
+ }
+ hasFailedResponse = true;
+ requestTooLarge ||= /(?:\b413\b|Failed to buffer the request body:\s*length limit exceeded)/i.test(
+ typeof message.errorMessage === 'string' ? message.errorMessage : '',
+ );
+ }
+ if (!hasFailedResponse) throw new Error('Pi retry has no failed response');
+ return { parentId: typeof entry.parentId === 'string' ? entry.parentId : null, requestTooLarge };
+}
+
function rawNodeMap(data: unknown): Map<string, UnknownRecord> {
const payload = recordOf(data);
const map = new Map<string, UnknownRecord>();