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(proxy): bound recovery resources and commit attachments as a batch

zqchrisbd3a9109941a
diff --git a/apps/desktop/src/main/cindy-media/__tests__/ingest.test.ts b/apps/desktop/src/main/cindy-media/__tests__/ingest.test.ts
index 3cde3f347c..3ed7bd52df 100644
--- a/apps/desktop/src/main/cindy-media/__tests__/ingest.test.ts
+++ b/apps/desktop/src/main/cindy-media/__tests__/ingest.test.ts
@@ -13,7 +13,7 @@ import fs from 'node:fs';
import fsp from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
-import { createHash } from 'node:crypto';
+import { createHash, randomUUID } from 'node:crypto';
import type { LedgerDb } from '../ledger';
import type { MediaRefCompensationScope } from '../refCompensationJournal';
import { fileSymlinkFixture } from './fileSymlinkFixture';
@@ -698,3 +698,21 @@ describe('ingestMedia(白名单外拒绝)', () => {
expect(ingest.supportedMime('application/zip')).toBe(false);
});
});
+
+it('compensates an entire recovery larger than one journal shard when its final write fails', async () => {
+ const refIds = Array.from({ length: 300 }, () => randomUUID());
+ const scope = compensationScope();
+ await ingest.ingestMedia({ buffer: PNG_BYTES, mimeType: 'image/png', refs: [] }, db);
+ await expect(refCompensation.withMediaRefCompensation({
+ scope, refIds,
+ perform: async () => {
+ for (const id of refIds) await ledger.addRef({ id, hash: PNG_HASH, refKind: 'session-attachment', refId: 'batch-session' }, db);
+ const markers = fs.readdirSync(scope.journalDir).filter(name => name.endsWith('.pending.json'));
+ expect(markers).toHaveLength(2);
+ throw new Error('last attachment failed');
+ },
+ compensate: id => ledger.removeRefById(id, db),
+ })).rejects.toThrow('last attachment failed');
+ expect(await db.select().from(schema.mediaRefs)).toEqual([]);
+ expect(fs.readdirSync(scope.journalDir).filter(name => name.endsWith('.json'))).toEqual([]);
+});
diff --git a/apps/desktop/src/main/cindy-media/blobStore.ts b/apps/desktop/src/main/cindy-media/blobStore.ts
index ae3a1bd41a..95128c83f2 100644
--- a/apps/desktop/src/main/cindy-media/blobStore.ts
+++ b/apps/desktop/src/main/cindy-media/blobStore.ts
@@ -83,6 +83,11 @@ export function blobUrl(hash: string, ext: string): string {
return `${SCHEME}://${HOST_BLOBS}/${hash}${ext}`;
}
+/** Canonical extension used both when planning and writing a content-addressed file. */
+export function extForMime(mimeType: string): string | null {
+ return EXT_BY_MIME[mimeType] ?? null;
+}
+
export function supportedMime(mimeType: string): boolean {
return Boolean(EXT_BY_MIME[mimeType]);
}
diff --git a/apps/desktop/src/main/cindy-media/refCompensationJournal.ts b/apps/desktop/src/main/cindy-media/refCompensationJournal.ts
index cc17c5172e..a04be896f2 100644
--- a/apps/desktop/src/main/cindy-media/refCompensationJournal.ts
+++ b/apps/desktop/src/main/cindy-media/refCompensationJournal.ts
@@ -255,80 +255,70 @@ export async function withMediaRefCompensation<T>(params: {
perform: () => Promise<T>;
compensate: (refId: string) => Promise<unknown>;
}): Promise<T> {
- if (params.refIds.length === 0 || params.refIds.length > MAX_REF_IDS) {
- throw new Error('cindy-media: invalid reference compensation batch size');
- }
- if (!params.refIds.every((id) => UUID_PATTERN.test(id))) {
+ if (params.refIds.length === 0 || !params.refIds.every((id) => UUID_PATTERN.test(id))) {
throw new Error('cindy-media: invalid staged reference id');
}
if (!OWNER_KEY_PATTERN.test(params.scope.ownerStorageKey)) {
throw new Error('cindy-media: invalid compensation owner namespace');
}
-
params.scope.assertStillValid();
await mkdir(params.scope.journalDir, { recursive: true, mode: 0o700 });
await assertSafeJournalDirectory(params.scope.journalDir);
- const paths = pathsFor(params.scope.journalDir, randomUUID());
-
- return withCrossProcessLock(
- paths.lock,
- { label: 'cindy-media-ref-compensation', waitMs: JOURNAL_LOCK_WAIT_MS },
- async (status) => {
- if (!status.held) {
- throw new Error('cindy-media: reference compensation journal lock unavailable');
+ const shards: Array<{ paths: JournalPaths; refIds: readonly string[]; record?: RefCompensationRecord; committed: boolean }> = [];
+ for (let i = 0; i < params.refIds.length; i += MAX_REF_IDS) {
+ shards.push({ paths: pathsFor(params.scope.journalDir, randomUUID()), refIds: params.refIds.slice(i, i + MAX_REF_IDS), committed: false });
+ }
+ // Keep each bounded journal locked for the entire batch, including rollback.
+ // A large recovery must not commit earlier shards before later attachments save.
+ const locked = async (index: number): Promise<T> => {
+ if (index < shards.length) {
+ return withCrossProcessLock(shards[index].paths.lock,
+ { label: 'cindy-media-ref-compensation', waitMs: JOURNAL_LOCK_WAIT_MS },
+ async status => {
+ if (!status.held) throw new Error('cindy-media: reference compensation journal lock unavailable');
+ return locked(index + 1);
+ });
+ }
+ try {
+ for (const shard of shards) {
+ shard.record = await writePendingRecord(params.scope, shard.paths, shard.refIds);
}
-
- const record = await writePendingRecord(params.scope, paths, params.refIds);
- let recordCommitted = false;
- try {
- params.scope.assertStillValid();
- const result = await params.perform();
+ params.scope.assertStillValid();
+ const result = await params.perform();
+ params.scope.assertStillValid();
+ for (const shard of shards) {
+ await commitRecord(params.scope, shard.paths, shard.record!);
+ shard.committed = true;
params.scope.assertStillValid();
- await commitRecord(params.scope, paths, record);
- recordCommitted = true;
- params.scope.assertStillValid();
- await removeMarker(paths.committed, params.scope.journalDir).catch((error) => {
- log.warn('Failed to collect a committed media reference journal', {
- operationId: paths.operationId,
- error: String(error),
- });
+ }
+ for (const shard of shards) {
+ await removeMarker(shard.paths.committed, params.scope.journalDir).catch(error => {
+ log.warn('Failed to collect a committed media reference journal', { operationId: shard.paths.operationId, error: String(error) });
});
- params.scope.assertStillValid();
- return result;
- } catch (error) {
- if (recordCommitted) {
- await markRollbackRequired(paths, record).catch((markError) => {
- log.warn('Failed to preserve an invalidated media reference commit for rollback', {
- operationId: paths.operationId,
- error: String(markError),
- });
+ }
+ params.scope.assertStillValid();
+ return result;
+ } catch (error) {
+ for (const shard of shards) {
+ if (!shard.record) continue;
+ if (shard.committed) {
+ await markRollbackRequired(shard.paths, shard.record).catch(markError => {
+ log.warn('Failed to preserve an invalidated media reference commit for rollback', { operationId: shard.paths.operationId, error: String(markError) });
});
}
- const rollback = await Promise.allSettled(params.refIds.map(params.compensate));
- const rollbackFailed = rollback.some((result) => result.status === 'rejected');
- rollback.forEach((result, index) => {
- if (result.status === 'rejected') {
- log.warn('Failed to compensate a staged media reference', {
- operationId: paths.operationId,
- refId: params.refIds[index],
- error: String(result.reason),
- });
- }
- });
- if (!rollbackFailed) {
- await removeOperationMarkers(paths, params.scope.journalDir).catch((cleanupError) => {
- // Keeping a pending marker is safe: startup will repeat idempotent
- // exact-id deletes. Never replace the original ingest error.
- log.warn('Failed to collect a compensated media reference journal', {
- operationId: paths.operationId,
- error: String(cleanupError),
- });
+ const rollback = await Promise.allSettled(shard.refIds.map(params.compensate));
+ if (rollback.every(result => result.status === 'fulfilled')) {
+ await removeOperationMarkers(shard.paths, params.scope.journalDir).catch(cleanupError => {
+ log.warn('Failed to collect a compensated media reference journal', { operationId: shard.paths.operationId, error: String(cleanupError) });
});
+ } else {
+ log.warn('Media reference compensation remains pending', { operationId: shard.paths.operationId });
}
- throw error;
}
- },
- );
+ throw error;
+ }
+ };
+ return locked(0);
}
function parseJournalRecord(
diff --git a/apps/desktop/src/main/maker-host/__tests__/oversized-attachment-recovery.test.ts b/apps/desktop/src/main/maker-host/__tests__/oversized-attachment-recovery.test.ts
index 879dc6c960..ec2118565a 100644
--- a/apps/desktop/src/main/maker-host/__tests__/oversized-attachment-recovery.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/oversized-attachment-recovery.test.ts
@@ -1,4 +1,4 @@
-import { mkdtemp, writeFile, readFile, rm } from 'node:fs/promises';
+import { mkdtemp, writeFile, readFile, rm, readdir } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { beforeEach, afterEach, expect, it, vi } from 'vitest';
@@ -6,13 +6,20 @@ import { beforeEach, afterEach, expect, it, vi } from 'vitest';
const state = vi.hoisted(() => ({
root: '', snapshot: null as any, session: { status: 'active', remoteHostId: null } as any,
existing: undefined as any, resolved: 'test-session' as string | null, invalidOwner: false,
- ingest: vi.fn(), query: vi.fn(),
+ ingest: vi.fn(), query: vi.fn(), addRef: vi.fn(), removeRef: vi.fn(),
}));
vi.mock('electron', () => ({ app: { getPath: () => state.root } }));
vi.mock('../../localDb/client/current.js', () => ({ getCurrentDbClientSnapshot: () => state.snapshot }));
-vi.mock('../../cindy-media/refCompensationJournal.js', () => ({ captureMediaRefCompensationScope: () => ({ assertStillValid: () => { if (state.invalidOwner) throw new Error('owner changed'); } }) }));
+vi.mock('../../cindy-media/refCompensationJournal.js', () => ({
+ captureMediaRefCompensationScope: () => ({ assertStillValid: () => { if (state.invalidOwner) throw new Error('owner changed'); } }),
+ withMediaRefCompensation: async ({ perform, compensate, refIds }: any) => {
+ try { return await perform(); }
+ catch (error) { for (const id of refIds) await compensate(id); throw error; }
+ },
+}));
+vi.mock('../../cindy-media/ledger.js', () => ({ addRef: (...args: unknown[]) => state.addRef(...args), removeRefById: (...args: unknown[]) => state.removeRef(...args) }));
vi.mock('../../cindy-media/ingest.js', () => ({ ingestMedia: (...args: unknown[]) => state.ingest(...args) }));
-vi.mock('../../cindy-media/blobStore.js', () => ({ resolveSafe: () => ({ absPath: path.join(state.root, 'saved.png') }) }));
+vi.mock('../../cindy-media/blobStore.js', () => ({ blobUrl: () => 'cindy-media://blobs/test.png', extForMime: () => '.png', resolveSafe: () => ({ absPath: path.join(state.root, 'saved.png') }) }));
import { createAttachmentRecovery } from '../oversized-attachment-recovery.js';
import { sessions } from '../../localDb/schema.js';
@@ -26,6 +33,8 @@ beforeEach(async () => {
state.snapshot = { userId: 'owner', clientEpoch: 1, client: { drizzle: {
select: () => ({ from: (table: unknown) => ({ where: () => ({ get: () => state.query(table) }) }) }),
} } };
+ state.addRef.mockReset();
+ state.removeRef.mockReset();
state.ingest.mockReset();
state.ingest.mockImplementation(async (params: any) => {
params.assertStillValid();
@@ -61,10 +70,12 @@ it('uses durable media ingestion and session refs for recognized media', async (
const result = await factory()!(await body(png, 'image/png'), 1000);
expect(result!.toString()).toContain('saved.png');
expect(await readFile(path.join(state.root, 'saved.png'))).toEqual(png);
- expect(state.ingest.mock.calls[0][0]).toMatchObject({ mimeType: 'image/png', isCache: false, refs: [{ refId: 'test-session', refKind: 'session-attachment' }] });
+ expect(state.ingest.mock.calls[0][0]).toMatchObject({ mimeType: 'image/png', isCache: false, refs: [] });
+ expect(state.addRef).toHaveBeenCalledWith(expect.objectContaining({ refId: 'test-session', refKind: 'session-attachment' }), expect.anything());
state.existing = { id: 'prior-ref' };
await factory()!(await body(png, 'image/png'), 1000);
expect(state.ingest.mock.calls[1][0].refs).toEqual([]);
+ expect(state.addRef).toHaveBeenCalledTimes(1);
});
it.each([null, { status: 'deleted', remoteHostId: null }, { status: 'active', remoteHostId: 'ssh' }])('does not persist for unavailable or remote sessions: %s', async session => {
@@ -91,3 +102,35 @@ it('does not replace an attachment when media persistence fails', async () => {
await expect(factory()!(input, 1000)).rejects.toThrow('disk full');
expect(await readFile(input.filePath)).toEqual(original);
});
+
+async function multiBody(blocks: unknown[], text = '') {
+ const filePath = path.join(state.root, 'request.json');
+ const raw = JSON.stringify({ input: [{ role: 'user', content: [...blocks, { type: 'input_text', text }] }] });
+ await writeFile(filePath, raw);
+ return { filePath, bytes: Buffer.byteLength(raw), signal: new AbortController().signal };
+}
+const pdfBlock = (byte: number) => ({ type: 'input_file', file_data: `data:application/pdf;base64,${Buffer.concat([Buffer.from('%PDF-1.7'), Buffer.alloc(3000, byte)]).toString('base64')}` });
+const pngBlock = () => ({ type: 'input_image', image_url: `data:image/png;base64,${Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), Buffer.alloc(3000)]).toString('base64')}` });
+
+it('publishes nothing if text still cannot fit after removing every attachment', async () => {
+ const input = await multiBody([pdfBlock(1), pngBlock()], 'x'.repeat(100_000));
+ expect(await factory()!(input, 1000)).toBeNull();
+ expect(state.ingest).not.toHaveBeenCalled();
+ expect(await readdir(state.root)).toEqual(['request.json']);
+});
+
+it('rolls back newly created files and refs if a later media save fails, preserving prior files', async () => {
+ await factory()!(await multiBody([pdfBlock(1)]), 1000);
+ const dir = path.join(state.root, 'hook-attachments', 'test-session');
+ const before = await readdir(dir);
+ state.ingest.mockRejectedValue(new Error('disk full'));
+ await expect(factory()!(await multiBody([pdfBlock(1), pdfBlock(2), pngBlock()]), 2000)).rejects.toThrow('disk full');
+ expect(await readdir(dir)).toEqual(before);
+ expect(state.removeRef).toHaveBeenCalledTimes(1);
+});
+
+it('compensates a media reference when a later write fails', async () => {
+ state.addRef.mockImplementation(async () => { throw new Error('lost acknowledgement'); });
+ await expect(factory()!(await multiBody([pngBlock()]), 1000)).rejects.toThrow('lost acknowledgement');
+ expect(state.removeRef.mock.calls[0][0]).toBe(state.addRef.mock.calls[0][0].id);
+});
diff --git a/apps/desktop/src/main/maker-host/oversized-attachment-recovery.ts b/apps/desktop/src/main/maker-host/oversized-attachment-recovery.ts
index 2044d4ef3b..6d55191c97 100644
--- a/apps/desktop/src/main/maker-host/oversized-attachment-recovery.ts
+++ b/apps/desktop/src/main/maker-host/oversized-attachment-recovery.ts
@@ -1,15 +1,16 @@
import { app } from 'electron';
-import { createHash } from 'node:crypto';
+import { createHash, randomUUID } from 'node:crypto';
import { createReadStream, constants } from 'node:fs';
-import { copyFile, lstat, mkdir, open, realpath } from 'node:fs/promises';
+import { copyFile, lstat, mkdir, open, realpath, rm } from 'node:fs/promises';
import path from 'node:path';
import { and, eq } from 'drizzle-orm';
import { recoverInlineAttachments, type OversizedRequestRecovery } from '@cindy/anthropic-compat-proxy';
import { getCurrentDbClientSnapshot } from '../localDb/client/current.js';
import { sessions, mediaRefs } from '../localDb/schema.js';
-import { captureMediaRefCompensationScope } from '../cindy-media/refCompensationJournal.js';
+import { captureMediaRefCompensationScope, withMediaRefCompensation } from '../cindy-media/refCompensationJournal.js';
import { ingestMedia } from '../cindy-media/ingest.js';
-import { resolveSafe } from '../cindy-media/blobStore.js';
+import { blobUrl, extForMime, resolveSafe } from '../cindy-media/blobStore.js';
+import { addRef, removeRefById } from '../cindy-media/ledger.js';
import { sniffMediaMime } from '../cindy-media/sniffMediaMime.js';
async function hashFile(file: string): Promise<string> {
@@ -18,6 +19,16 @@ async function hashFile(file: string): Promise<string> {
return hash.digest('hex');
}
+// A retry must not reuse another recovery's uncommitted file or reference.
+const recoveryLocks = new Map<string, Promise<unknown>>();
+async function serializeRecovery<T>(key: string, run: () => Promise<T>): Promise<T> {
+ const previous = recoveryLocks.get(key) ?? Promise.resolve();
+ const current = previous.catch(() => undefined).then(run);
+ recoveryLocks.set(key, current);
+ try { return await current; }
+ finally { if (recoveryLocks.get(key) === current) recoveryLocks.delete(key); }
+}
+
/** All three local harnesses share this overflow-only recovery and storage lifecycle. */
export function createAttachmentRecovery(
resolveSession: (headers: Record<string, string>) => string | null | undefined,
@@ -36,14 +47,15 @@ export function createAttachmentRecovery(
throw new Error('Attachment recovery owner changed');
}
};
- return async (body, targetBytes) => {
+ return (body, targetBytes) => serializeRecovery(`${scope.ownerStorageKey}/${sessionId}`, async () => {
assertCurrent();
const session = await db.select({ status: sessions.status, remoteHostId: sessions.remoteHostId })
.from(sessions).where(eq(sessions.id, sessionId)).get();
assertCurrent();
// A Desktop path must never be presented as a path on an SSH host.
if (!session || session.status === 'deleted' || session.remoteHostId) return null;
- return recoverInlineAttachments(body, targetBytes, async attachment => {
+ const staged = new Map<string, { source: string; file: string; hash: string; mimeType: string | null; refId?: string }>();
+ return recoverInlineAttachments(body, targetBytes, { prepare: async attachment => {
body.signal.throwIfAborted();
assertCurrent();
const probe = Buffer.alloc(64 * 1024);
@@ -56,43 +68,69 @@ export function createAttachmentRecovery(
const hash = await hashFile(attachment.filePath);
body.signal.throwIfAborted();
assertCurrent();
- if (mimeType) {
- const existing = await db.select({ id: mediaRefs.id }).from(mediaRefs)
- .where(and(eq(mediaRefs.hash, hash), eq(mediaRefs.refKind, 'session-attachment'), eq(mediaRefs.refId, sessionId))).get();
- assertCurrent();
- const media = await ingestMedia({
- filePath: attachment.filePath,
- mimeType,
- isCache: false,
- refs: existing ? [] : [{ refKind: 'session-attachment', refId: sessionId, originSessionId: sessionId, originKind: 'user' }],
- assertStillValid: assertCurrent,
- refCompensationScope: scope,
- }, db);
+ const ext = mimeType ? extForMime(mimeType)! : probe.subarray(0, 5).toString() === '%PDF-' ? '.pdf' : '.bin';
+ const file = mimeType
+ ? resolveSafe(blobUrl(hash, ext)).absPath
+ : path.join(app.getPath('userData'), 'hook-attachments', sessionId, `${hash}-recovered${ext}`);
+ if (!staged.has(file)) {
+ const existing = mimeType ? await db.select({ id: mediaRefs.id }).from(mediaRefs)
+ .where(and(eq(mediaRefs.hash, hash), eq(mediaRefs.refKind, 'session-attachment'), eq(mediaRefs.refId, sessionId))).get() : null;
assertCurrent();
- return resolveSafe(media.url).absPath;
- }
- // Non-media uses the existing session attachment directory and deletion
- // lifecycle. Do not trust filenames or MIME claims to choose executables.
- const ext = probe.subarray(0, 5).toString() === '%PDF-' ? '.pdf' : '.bin';
- const root = path.join(app.getPath('userData'), 'hook-attachments');
- const dir = path.join(root, sessionId);
- await mkdir(dir, { recursive: true, mode: 0o700 });
- assertCurrent();
- if ((await lstat(root)).isSymbolicLink() || (await lstat(dir)).isSymbolicLink() || path.dirname(await realpath(dir)) !== await realpath(root)) {
- throw new Error('Invalid attachment directory');
- }
- const file = path.join(dir, `${hash}-recovered${ext}`);
- try { await copyFile(attachment.filePath, file, constants.COPYFILE_EXCL); }
- catch (error) { if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error; }
- assertCurrent();
- const stat = await lstat(file);
- if (!stat.isFile() || stat.isSymbolicLink() || await hashFile(file) !== hash) {
- throw new Error('Recovered attachment verification failed');
+ staged.set(file, { source: attachment.filePath, file, hash, mimeType, refId: mimeType && !existing ? randomUUID() : undefined });
}
- body.signal.throwIfAborted();
- assertCurrent();
return file;
+ }, commit: async () => {
+ const createdFiles: string[] = [];
+ const persist = async () => {
+ for (const item of staged.values()) {
+ body.signal.throwIfAborted();
+ assertCurrent();
+ if (item.mimeType) {
+ const media = await ingestMedia({ filePath: item.source, mimeType: item.mimeType, isCache: false,
+ refs: [], assertStillValid: assertCurrent, refCompensationScope: scope }, db);
+ assertCurrent();
+ if (resolveSafe(media.url).absPath !== item.file) throw new Error('Recovered attachment path changed');
+ if (item.refId) {
+ await addRef({ id: item.refId, hash: item.hash, refKind: 'session-attachment', refId: sessionId,
+ originSessionId: sessionId, originKind: 'user' }, db);
+ }
+ } else {
+ const dir = path.dirname(item.file);
+ const root = path.dirname(dir);
+ await mkdir(dir, { recursive: true, mode: 0o700 });
+ assertCurrent();
+ if ((await lstat(root)).isSymbolicLink() || (await lstat(dir)).isSymbolicLink() || path.dirname(await realpath(dir)) !== await realpath(root)) {
+ throw new Error('Invalid attachment directory');
+ }
+ try {
+ await copyFile(item.source, item.file, constants.COPYFILE_EXCL);
+ createdFiles.push(item.file);
+ } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error; }
+ const stat = await lstat(item.file);
+ if (!stat.isFile() || stat.isSymbolicLink() || await hashFile(item.file) !== item.hash) {
+ throw new Error('Recovered attachment verification failed');
+ }
+ }
+ body.signal.throwIfAborted();
+ assertCurrent();
+ }
+ };
+ const refIds = [...staged.values()].flatMap(item => item.refId ? [item.refId] : []);
+ try {
+ if (refIds.length) {
+ await withMediaRefCompensation({ scope: { ...scope, assertStillValid: () => {
+ assertCurrent();
+ body.signal.throwIfAborted();
+ } }, refIds, perform: persist,
+ compensate: id => removeRefById(id, db) });
+ } else await persist();
+ } catch (error) {
+ // Only this serialized recovery's new files; never pre-existing/shared blobs.
+ await Promise.all(createdFiles.map(file => rm(file, { force: true })));
+ throw error;
+ }
+ }
});
- };
+ });
};
}
diff --git a/docs/dev-rules/media-storage-and-protocols.md b/docs/dev-rules/media-storage-and-protocols.md
index 8081034d10..b65562597b 100644
--- a/docs/dev-rules/media-storage-and-protocols.md
+++ b/docs/dev-rules/media-storage-and-protocols.md
@@ -106,13 +106,17 @@
- 只在本地代理收到的请求实际超过既有字节上限后恢复;正常请求不因图片数量、预估
token 或接近上限而丢弃附件。上限本身保持不变。
-- 超限请求先分块落 Host 私有临时文件,长 base64 字符串分块解码。按历史顺序保留
+- 超限请求先分块落 Host 私有临时文件,base64 字符串不论单个大小都参与恢复,并分块解码。
+ 临时写入按实际可用磁盘共享预算,预留 256 MiB,并为解析、解码和保存预留副本空间;
+ 不另设固定附件体积门槛。磁盘不足返回明确的 507,可清理空间后重试,原始历史不变。按历史顺序保留
足够数量的旧附件为本地文件,使请求重新小于上限;无需移出全部附件。
- 已识别媒体经 `ingestMedia` 挂 `session-attachment` 持久引用;其他文件复用
`hook-attachments/<sessionId>` 生命周期。必须保存成功后才用真实绝对路径替换内容,
并明确告诉模型当前消息不再包含该附件内容,需要时重新读取。
- 仅修改此次发给模型的请求,不改原生历史文件,不重新执行生成或其他工具操作。
- 保存失败、取消或归属变化时不转发部分恢复结果;临时请求与解码文件在结束时清理。
+ 先计算真实路径及替换后的请求大小,确认可恢复后才提交附件。保存失败、取消或归属变化时
+ 不转发部分结果;整批新增媒体引用经补偿日志回滚,仅删除本次新建的非媒体文件,
+ 不删除已有文件或共享 blob;无引用 blob 仍由统一媒体回收。临时请求与解码文件在结束时清理。
- Codex、Claude、Pi 的本机 HTTP JSON 代理共用恢复;Pi 必须验证原有 session token。
缺少有效任务绑定、SSH 任务、压缩/不透明请求与 WebSocket 不套用本机路径恢复。
diff --git a/packages/anthropic-compat-proxy/src/oversized-attachments.test.ts b/packages/anthropic-compat-proxy/src/oversized-attachments.test.ts
index c67ba6ae05..f3542ae79a 100644
--- a/packages/anthropic-compat-proxy/src/oversized-attachments.test.ts
+++ b/packages/anthropic-compat-proxy/src/oversized-attachments.test.ts
@@ -4,7 +4,10 @@ import path from 'node:path';
import { Readable } from 'node:stream';
import type { IncomingMessage } from 'node:http';
import { afterEach, describe, expect, it, vi } from 'vitest';
-import { collectRecoverableBody, recoverInlineAttachments, type AttachmentKeeper } from './oversized-attachments.js';
+import { collectRecoverableBody, recoverInlineAttachments as recover, type RecoveredAttachment } from './oversized-attachments.js';
+
+const recoverInlineAttachments = (body: Parameters<typeof recover>[0], limit: number, prepare: (a: RecoveredAttachment) => Promise<string>) =>
+ recover(body, limit, { prepare, commit: async () => {} });
const dirs: string[] = [];
afterEach(async () => { await Promise.all(dirs.splice(0).map(dir => rm(dir, { recursive: true, force: true }))); });
@@ -64,7 +67,7 @@ describe('overflow-only attachment recovery', () => {
{ type: 'video_url', video_url: { url: `data:video/mp4;base64,${data.toString('base64')}` } },
])('handles attachment blocks in nested tool results: $type', async block => {
const f = await fixture({ messages: [{ role: 'user', content: [{ type: 'tool_result', tool_use_id: 'one', content: [block] }] }] });
- const keep: AttachmentKeeper = vi.fn(async a => {
+ const keep = vi.fn(async (a: RecoveredAttachment) => {
expect(await readFile(a.filePath)).toEqual(data);
return path.join(f.dir, 'retained');
});
@@ -133,3 +136,31 @@ describe('overflow-only attachment recovery', () => {
expect(await readFile(f.body.filePath)).toEqual(f.raw);
});
});
+
+it('recovers many small attachments only after their aggregate request overflows', async () => {
+ const small = Buffer.alloc(1024, 7);
+ const block = { type: 'input_image', image_url: `data:image/png;base64,${small.toString('base64')}` };
+ const f = await fixture({ input: [{ role: 'user', content: Array.from({ length: 70 }, () => block) }] });
+ const prepare = vi.fn(async (a: RecoveredAttachment) => { expect(await readFile(a.filePath)).toEqual(small); return '/tmp/small.png'; });
+ const commit = vi.fn(async () => {});
+ const result = await recover(f.body, 30_000, { prepare, commit });
+ expect(result!.length).toBeLessThanOrEqual(30_000);
+ const content = JSON.parse(result!.toString()).input[0].content;
+ expect(content[0].type).toBe('input_text');
+ expect(content.at(-1)).toEqual(block);
+ expect(commit).toHaveBeenCalledOnce();
+});
+
+it('keeps URL attachments intact while recovering inline small attachments', async () => {
+ const remote = { type: 'input_image', image_url: 'https://example.test/image.png' };
+ const f = await fixture({ input: [remote, image()] });
+ const result = await recoverInlineAttachments(f.body, 1000, async () => '/tmp/kept');
+ expect(JSON.parse(result!.toString()).input[0]).toEqual(remote);
+});
+
+it('does not commit staged files when the recovered request still cannot fit', async () => {
+ const f = await fixture({ instructions: 'x'.repeat(100_000), input: [image()] });
+ const commit = vi.fn();
+ expect(await recover(f.body, 1000, { prepare: async () => '/tmp/kept', commit })).toBeNull();
+ expect(commit).not.toHaveBeenCalled();
+});
diff --git a/packages/anthropic-compat-proxy/src/oversized-attachments.ts b/packages/anthropic-compat-proxy/src/oversized-attachments.ts
index e31e6da3aa..e109cfca83 100644
--- a/packages/anthropic-compat-proxy/src/oversized-attachments.ts
+++ b/packages/anthropic-compat-proxy/src/oversized-attachments.ts
@@ -1,6 +1,6 @@
import { randomUUID } from 'node:crypto';
import { createReadStream } from 'node:fs';
-import { mkdtemp, open, readFile, rm, type FileHandle } from 'node:fs/promises';
+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';
@@ -9,6 +9,8 @@ export interface OversizedBody {
filePath: string;
bytes: number;
signal: AbortSignal;
+ /** Account for decoded/published file allocation overhead while recovery is active. */
+ reserveAttachment?: () => Promise<void>;
}
export interface RecoveredAttachment {
@@ -17,7 +19,30 @@ export interface RecoveredAttachment {
filename?: string;
}
-export type AttachmentKeeper = (attachment: RecoveredAttachment) => Promise<string>;
+export interface AttachmentKeeper {
+ /** Prepare a path without publishing files or references. */
+ prepare(attachment: RecoveredAttachment): Promise<string>;
+ /** Publish the prepared batch; undo its own partial writes if it fails. */
+ commit(): Promise<void>;
+}
+
+// Reserve room for the request, tokenized strings, decoded files and publication.
+// All concurrent recoveries share this budget; it is based on free disk, not an
+// arbitrary attachment size. Leave 256 MiB available to the rest of the app.
+const DISK_RESERVE_BYTES = 256 * 1024 * 1024;
+let reservedDiskBytes = 0;
+let diskAdmission: Promise<unknown> = Promise.resolve();
+async function reserveDisk(bytes: number): Promise<number> {
+ const operation = diskAdmission.then(async () => {
+ const disk = await statfs(tmpdir());
+ const available = disk.bavail * disk.bsize - DISK_RESERVE_BYTES;
+ if (bytes + reservedDiskBytes > available) throw new Error('RECOVERY_STORAGE_EXHAUSTED');
+ reservedDiskBytes += bytes;
+ return disk.bsize;
+ });
+ diskAdmission = operation.catch(() => undefined);
+ return operation;
+}
/** Normal requests remain byte-for-byte unchanged. Only overflow spills to disk. */
export async function collectRecoverableBody(
@@ -31,11 +56,18 @@ export async function collectRecoverableBody(
let directory: string | undefined;
let file: FileHandle | undefined;
let filePath = '';
+ let reserved = 0;
+ let allocationUnit = 0;
try {
- for await (const chunk of req) {
+ for await (const chunk of req.iterator({ destroyOnReturn: false })) {
signal.throwIfAborted();
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
bytes += buffer.length;
+ if (bytes > limit) {
+ const required = bytes * 4 - reserved;
+ allocationUnit = await reserveDisk(required);
+ reserved += required;
+ }
if (!file && bytes > limit) {
directory = await mkdtemp(path.join(tmpdir(), 'cindy-request-'));
filePath = path.join(directory, 'body.json');
@@ -50,26 +82,34 @@ export async function collectRecoverableBody(
await file.close();
file = undefined;
signal.throwIfAborted();
- const result = await recover({ filePath, bytes, signal });
+ const result = await recover({ filePath, bytes, signal, reserveAttachment: async () => {
+ const overhead = allocationUnit * 2;
+ await reserveDisk(overhead);
+ reserved += overhead;
+ } });
signal.throwIfAborted();
if (!result || result.length > limit) throw new Error('REQUEST_TOO_LARGE');
return result;
} finally {
- await file?.close();
- if (directory) await rm(directory, { recursive: true, force: true });
+ try { await file?.close(); }
+ finally {
+ try { if (directory) await rm(directory, { recursive: true, force: true }); }
+ finally { reservedDiskBytes -= reserved; }
+ }
}
}
-interface StringPart { path: string; bytes: number }
+interface StringPart { path: string; start: number; bytes: number; prefix: string }
interface TokenizedBody { json: string; parts: Map<string, StringPart> }
const STRING_MEMORY_BYTES = 64 * 1024;
/**
- * Spill long JSON strings before JSON.parse. No decoded image or entire oversized
+ * Index attachment and long JSON strings as ranges in the request spool before
+ * JSON.parse (not one temporary file per small string). No image or oversized
* request is materialized in RAM. Random markers cannot collide with input text.
* The original request file is retained until recovery has completely finished.
*/
-async function tokenizeStrings(body: OversizedBody, directory: string, limit: number): Promise<TokenizedBody> {
+async function tokenizeStrings(body: OversizedBody, limit: number): Promise<TokenizedBody> {
const prefix = `cindy-spilled-${randomUUID()}-`;
const parts = new Map<string, StringPart>();
const output: string[] = [];
@@ -78,74 +118,89 @@ async function tokenizeStrings(body: OversizedBody, directory: string, limit: nu
let escaped = false;
let stringChunks: string[] = [];
let stringBytes = 0;
- let part: FileHandle | undefined;
- let partPath = '';
+ let stringPrefix = '';
+ let spilled = false;
+ let stringStart = 0;
+ let chunkStart = 0;
+ let previousString = '';
+ let lastDelimiter = '';
+ let attachmentValue = false;
+ const attachmentKeys = new Set(['image_url', 'url', 'file_data', 'data']);
const emit = (text: string) => {
outputBytes += Buffer.byteLength(text);
if (outputBytes > limit) throw new Error('REQUEST_TOO_LARGE');
output.push(text);
};
- const append = async (text: string) => {
+ const append = (text: string) => {
+ stringPrefix = (stringPrefix + text.slice(0, 16)).slice(0, 16);
stringBytes += Buffer.byteLength(text);
- if (!part && stringBytes > STRING_MEMORY_BYTES) {
- partPath = path.join(directory, `string-${parts.size}`);
- part = await open(partPath, 'wx', 0o600);
- await part.writeFile(stringChunks.join(''));
+ if (attachmentValue || stringBytes > STRING_MEMORY_BYTES) {
+ spilled = true;
stringChunks = [];
}
- if (part) await part.writeFile(text);
- else stringChunks.push(text);
+ if (!spilled) stringChunks.push(text);
};
- try {
- for await (const chunk of createReadStream(body.filePath, { encoding: 'utf8' })) {
- body.signal.throwIfAborted();
- const text = String(chunk);
- let start = 0;
- // Scan only JSON delimiters, not every base64 character.
- const delimiters = /["\\]/g;
- let match: RegExpExecArray | null;
- while ((match = delimiters.exec(text))) {
- const index = match.index;
- if (!quoted) {
- if (match[0] !== '"') continue;
- emit(text.slice(start, index));
- quoted = true;
- escaped = false;
+ for await (const chunk of createReadStream(body.filePath, { encoding: 'utf8' })) {
+ body.signal.throwIfAborted();
+ const text = String(chunk);
+ let start = 0;
+ // Scan only JSON delimiters, not every base64 character.
+ const delimiters = /["\\]/g;
+ let match: RegExpExecArray | null;
+ while ((match = delimiters.exec(text))) {
+ const index = match.index;
+ if (!quoted) {
+ if (match[0] !== '"') continue;
+ const between = text.slice(start, index);
+ emit(between);
+ const delimiter = between.trim().at(-1) ?? lastDelimiter;
+ attachmentValue = delimiter === ':' && attachmentKeys.has(previousString);
+ stringStart = chunkStart + Buffer.byteLength(text.slice(0, index + 1));
+ quoted = true;
+ escaped = false;
+ start = index + 1;
+ } else {
+ if (escaped && index === start) { escaped = false; continue; }
+ // An escape only affects the immediately following character.
+ if (escaped) escaped = false;
+ if (match[0] === '\\') {
+ append(text.slice(start, index + 1));
start = index + 1;
+ escaped = true;
} else {
- if (escaped && index === start) { escaped = false; continue; }
- // An escape only affects the immediately following character.
- if (escaped) escaped = false;
- if (match[0] === '\\') {
- await append(text.slice(start, index + 1));
- start = index + 1;
- escaped = true;
+ append(text.slice(start, index));
+ if (spilled) {
+ spilled = false;
+ const marker = `${prefix}${parts.size}`;
+ parts.set(marker, { path: body.filePath, start: stringStart, bytes: stringBytes, prefix: stringPrefix });
+ emit(JSON.stringify(marker));
+ previousString = '';
} else {
- await append(text.slice(start, index));
- if (part) {
- await part.close();
- part = undefined;
- const marker = `${prefix}${parts.size}`;
- parts.set(marker, { path: partPath, bytes: stringBytes });
- emit(JSON.stringify(marker));
- } else emit(`"${stringChunks.join('')}"`);
- stringChunks = [];
- stringBytes = 0;
- quoted = false;
- start = index + 1;
+ const raw = `"${stringChunks.join('')}"`;
+ emit(raw);
+ previousString = JSON.parse(raw) as string;
}
+ lastDelimiter = '';
+ stringChunks = [];
+ stringBytes = 0;
+ stringPrefix = '';
+ quoted = false;
+ start = index + 1;
}
}
- if (quoted) {
- if (start < text.length) escaped = false;
- await append(text.slice(start));
- } else emit(text.slice(start));
}
- if (quoted) throw new Error('Invalid JSON string');
- return { json: output.join(''), parts };
- } finally {
- await part?.close();
+ if (quoted) {
+ if (start < text.length) escaped = false;
+ append(text.slice(start));
+ } else {
+ const tail = text.slice(start);
+ emit(tail);
+ lastDelimiter = tail.trim().at(-1) ?? lastDelimiter;
+ }
+ chunkStart += Buffer.byteLength(text);
}
+ if (quoted) throw new Error('Invalid JSON string');
+ return { json: output.join(''), parts };
}
type RecordValue = Record<string, unknown>;
@@ -170,7 +225,7 @@ function findCandidates(value: unknown, parts: Map<string, StringPart>, result:
data = child.input_audio.data;
if (typeof child.input_audio.format === 'string') mimeType = `audio/${child.input_audio.format}`;
} else if (child.type === 'video_url' && record(child.video_url)) data = child.video_url.url;
- if (typeof data === 'string' && parts.has(data)) {
+ if (typeof data === 'string' && parts.has(data) && !/^https?:/i.test(parts.get(data)!.prefix)) {
result.push({ parent: value, index, marker: data, mimeType, textType,
filename: typeof child.filename === 'string' ? child.filename : undefined });
continue;
@@ -185,13 +240,14 @@ function findCandidates(value: unknown, parts: Map<string, StringPart>, result:
/** Decode canonical base64 incrementally; never accept Node's permissive truncation. */
async function decodeAttachment(part: StringPart, dest: string, signal: AbortSignal): Promise<string | undefined> {
+ if (!part.bytes) throw new Error('Invalid attachment base64');
const file = await open(dest, 'wx', 0o600);
let prefix = '';
let started = false;
let mime: string | undefined;
let pending = '';
try {
- for await (const raw of createReadStream(part.path, { encoding: 'utf8' })) {
+ for await (const raw of createReadStream(part.path, { encoding: 'utf8', start: part.start, end: part.start + part.bytes - 1 })) {
signal.throwIfAborted();
let chunk = String(raw);
// Native JSON encoders use unescaped base64. Refuse other representations
@@ -242,7 +298,7 @@ export async function recoverInlineAttachments(
if (body.bytes <= targetBytes) return null;
const directory = await mkdtemp(path.join(tmpdir(), 'cindy-attachment-recovery-'));
try {
- const { json, parts } = await tokenizeStrings(body, directory, targetBytes);
+ const { json, parts } = await tokenizeStrings(body, targetBytes);
const value: unknown = JSON.parse(json);
const candidates: Candidate[] = [];
// Only model-visible conversation content; never tool schemas or instructions.
@@ -256,9 +312,10 @@ export async function recoverInlineAttachments(
if (estimatedBytes <= targetBytes) break;
body.signal.throwIfAborted();
const part = parts.get(candidate.marker)!;
+ await body.reserveAttachment?.();
const dest = path.join(directory, `attachment-${replaced}`);
const mime = await decodeAttachment(part, dest, body.signal);
- const localPath = await keep({ filePath: dest, mimeType: mime ?? candidate.mimeType, filename: candidate.filename });
+ const localPath = await keep.prepare({ filePath: dest, mimeType: mime ?? candidate.mimeType, filename: candidate.filename });
body.signal.throwIfAborted();
if (!localPath || !path.isAbsolute(localPath)) throw new Error('Attachment recovery requires a local absolute path');
const text = `Attachment moved out of model context because the request exceeded its size limit. Preserved local file: ${JSON.stringify(localPath)}. Read the file when needed; its contents are not visible in this message.`;
@@ -276,10 +333,16 @@ export async function recoverInlineAttachments(
const needle = JSON.stringify(marker);
if (!output.includes(needle)) continue;
if (Buffer.byteLength(output) - Buffer.byteLength(needle) + part.bytes + 2 > targetBytes) return null;
- const raw = await readFile(part.path, 'utf8');
+ let raw = '';
+ if (part.bytes) {
+ for await (const chunk of createReadStream(part.path, { encoding: 'utf8', start: part.start, end: part.start + part.bytes - 1 })) raw += chunk;
+ }
output = output.replace(needle, () => `"${raw}"`);
}
const result = Buffer.from(output);
- return result.length <= targetBytes ? result : null;
+ if (result.length > targetBytes) return null;
+ body.signal.throwIfAborted();
+ await keep.commit();
+ return result;
} finally { await rm(directory, { recursive: true, force: true }); }
}
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 0ae2002799..4cf67d010e 100644
--- a/packages/anthropic-compat-proxy/src/oversized-request-server.test.ts
+++ b/packages/anthropic-compat-proxy/src/oversized-request-server.test.ts
@@ -3,8 +3,11 @@ import { readFile } from 'node:fs/promises';
import { afterEach, expect, it, vi } from 'vitest';
import { createAnthropicCompatProxy } from './server.js';
import type { LocalRequestHandler } from './types.js';
-import { recoverInlineAttachments } from './oversized-attachments.js';
+import { recoverInlineAttachments as recover, type RecoveredAttachment } from './oversized-attachments.js';
import { listenOnAvailableLoopbackPort } from './test-loopback-server.js';
+const recoverInlineAttachments = (body: Parameters<typeof recover>[0], limit: number, prepare: (a: RecoveredAttachment) => Promise<string>) =>
+ recover(body, limit, { prepare, commit: async () => {} });
+
const cleanup: Array<() => Promise<void>> = [];
afterEach(async () => { for (const close of cleanup.splice(0).reverse()) await close(); });
diff --git a/packages/anthropic-compat-proxy/src/oversized-storage.test.ts b/packages/anthropic-compat-proxy/src/oversized-storage.test.ts
new file mode 100644
index 0000000000..190b3e9acc
--- /dev/null
+++ b/packages/anthropic-compat-proxy/src/oversized-storage.test.ts
@@ -0,0 +1,85 @@
+import { statfs, access } from 'node:fs/promises';
+import { Readable } from 'node:stream';
+import type { IncomingMessage } from 'node:http';
+import { afterEach, expect, it, vi } from 'vitest';
+import { collectRecoverableBody } from './oversized-attachments.js';
+import { createAnthropicCompatProxy } from './server.js';
+
+vi.mock('node:fs/promises', async importOriginal => {
+ const actual = await importOriginal<typeof import('node:fs/promises')>();
+ return { ...actual, statfs: vi.fn(actual.statfs) };
+});
+const disk = vi.mocked(statfs);
+const reserve = 256 * 1024 * 1024;
+const free = (bytes: number) => ({ bavail: reserve + bytes, bsize: 1 }) as Awaited<ReturnType<typeof statfs>>;
+afterEach(() => { disk.mockReset(); });
+const request = (chunks: Buffer[]) => Readable.from(chunks) as IncomingMessage;
+
+it('does not check disk or change normal requests on a full disk', async () => {
+ disk.mockResolvedValue(free(0));
+ const raw = Buffer.alloc(100);
+ const recover = vi.fn();
+ expect(await collectRecoverableBody(request([raw]), 100, recover, new AbortController().signal)).toEqual(raw);
+ expect(disk).not.toHaveBeenCalled();
+ expect(recover).not.toHaveBeenCalled();
+});
+
+it('stops chunked upload as available temporary storage runs out', async () => {
+ disk.mockResolvedValueOnce(free(2000)).mockResolvedValue(free(0));
+ const recover = vi.fn();
+ await expect(collectRecoverableBody(request([Buffer.alloc(200), Buffer.alloc(200)]), 100, recover, new AbortController().signal)).rejects.toThrow('RECOVERY_STORAGE_EXHAUSTED');
+ expect(recover).not.toHaveBeenCalled();
+ // A failed upload releases its reservation for the next attempt.
+ disk.mockResolvedValue(free(800));
+ expect(await collectRecoverableBody(request([Buffer.alloc(200)]), 100, async () => Buffer.from('{}'), new AbortController().signal)).toEqual(Buffer.from('{}'));
+});
+
+it('accounts for simultaneous recovery and releases files and budget on completion', async () => {
+ disk.mockResolvedValue(free(1000));
+ let release!: () => void;
+ const hold = new Promise<void>(resolve => { release = resolve; });
+ let arrived!: () => void;
+ const started = new Promise<void>(resolve => { arrived = resolve; });
+ let spilled = '';
+ const first = collectRecoverableBody(request([Buffer.alloc(200)]), 100, async body => {
+ spilled = body.filePath;
+ arrived();
+ await hold;
+ return Buffer.from('{}');
+ }, new AbortController().signal);
+ await started;
+ try {
+ await expect(collectRecoverableBody(request([Buffer.alloc(200)]), 100, async () => Buffer.from('{}'), new AbortController().signal)).rejects.toThrow('RECOVERY_STORAGE_EXHAUSTED');
+ } finally { release(); await first; }
+ await expect(access(spilled)).rejects.toThrow();
+ expect(await collectRecoverableBody(request([Buffer.alloc(200)]), 100, async () => Buffer.from('{}'), new AbortController().signal)).toEqual(Buffer.from('{}'));
+});
+
+it('returns an actionable HTTP error without forwarding when recovery storage is unavailable', async () => {
+ disk.mockResolvedValue(free(0));
+ const recover = vi.fn();
+ const local = vi.fn();
+ const proxy = await createAnthropicCompatProxy({ upstream: 'http://127.0.0.1:1', maxRequestBodyBytes: 100,
+ oversizedRequestRecovery: () => recover, routingTransform: () => ({ localHandler: local }) });
+ try {
+ const response = await fetch(`${proxy.url}/v1/responses`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ input: 'x'.repeat(1000) }) });
+ expect(response.status).toBe(507);
+ expect(await response.json()).toMatchObject({ error: { reason: 'attachment_recovery_storage_exhausted' } });
+ expect(local).not.toHaveBeenCalled();
+ expect(recover).not.toHaveBeenCalled();
+ } finally { await proxy.dispose(); }
+});
+
+
+it('reserves allocation overhead for decoded files and releases it when recovery fails', async () => {
+ disk.mockResolvedValue(free(800));
+ await expect(collectRecoverableBody(request([Buffer.alloc(200)]), 100, async body => {
+ await body.reserveAttachment!();
+ return Buffer.from('{}');
+ }, new AbortController().signal)).rejects.toThrow('RECOVERY_STORAGE_EXHAUSTED');
+ disk.mockResolvedValue(free(802));
+ expect(await collectRecoverableBody(request([Buffer.alloc(200)]), 100, async body => {
+ await body.reserveAttachment!();
+ return Buffer.from('{}');
+ }, new AbortController().signal)).toEqual(Buffer.from('{}'));
+});
diff --git a/packages/anthropic-compat-proxy/src/server.ts b/packages/anthropic-compat-proxy/src/server.ts
index 60194d1645..5387c5cc0b 100644
--- a/packages/anthropic-compat-proxy/src/server.ts
+++ b/packages/anthropic-compat-proxy/src/server.ts
@@ -668,10 +668,11 @@ function respondRequestTooLarge(opts: {
/** Content-Length 预检命中时的声明字节数;流式守卫命中时为 null。 */
declaredBytes: number | null;
receivedBytes: number;
- reason?: 'request_body_too_large';
+ reason?: 'request_body_too_large' | 'attachment_recovery_storage_exhausted';
}): void {
const { req, res, logger } = opts;
- logger.warn?.('✖ request body exceeds proxy limit → 413', {
+ const storageExhausted = opts.reason === 'attachment_recovery_storage_exhausted';
+ logger.warn?.(storageExhausted ? 'attachment recovery has insufficient temporary disk space → 507' : '✖ request body exceeds proxy limit → 413', {
reqId: opts.reqId,
method: opts.method,
url: opts.url,
@@ -684,10 +685,10 @@ function respondRequestTooLarge(opts: {
error: {
type: 'proxy_error',
reason: opts.reason ?? 'request_body_too_large',
- message: `request body too large: ${opts.declaredBytes ?? `>${opts.receivedBytes}`} bytes exceeds proxy limit of ${opts.limitBytes} bytes`,
+ message: storageExhausted ? 'Not enough free disk space to recover this request. Free disk space and retry; the original conversation is unchanged.' : `request body too large: ${opts.declaredBytes ?? `>${opts.receivedBytes}`} bytes exceeds proxy limit of ${opts.limitBytes} bytes`,
},
}));
- res.writeHead(413, {
+ res.writeHead(storageExhausted ? 507 : 413, {
'content-type': 'application/json',
'content-length': String(payload.length),
connection: 'close',
@@ -2228,7 +2229,8 @@ export async function createAnthropicCompatProxy(opts: ProxyOptions): Promise<Pr
reqId, originalBytes: body.bytes, recoveredBytes: recovered.length,
});
return recovered;
- } catch {
+ } catch (error) {
+ if (error instanceof Error && error.message === 'RECOVERY_STORAGE_EXHAUSTED') throw error;
// Do not leak paths, payloads or credentials in recovery errors.
logger.warn?.('oversized attachment recovery failed', { reqId });
return null;
@@ -2237,10 +2239,11 @@ export async function createAnthropicCompatProxy(opts: ProxyOptions): Promise<Pr
: await collectRequestBody(req, requestIngressBytes);
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
- if (msg === 'REQUEST_TOO_LARGE') {
+ if (msg === 'REQUEST_TOO_LARGE' || msg === 'RECOVERY_STORAGE_EXHAUSTED') {
respondRequestTooLarge({
req, res, logger, reqId, method, url, headers,
limitBytes: maxBodyBytes,
+ reason: msg === 'RECOVERY_STORAGE_EXHAUSTED' ? 'attachment_recovery_storage_exhausted' : 'request_body_too_large',
declaredBytes: null,
receivedBytes: (err as { receivedBytes?: number }).receivedBytes ?? 0,
});