mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-17 21:43:06 +00:00
* fix: Preserve primary files until secondary cleanup succeeds * test: Use a flat deletion-strategy lookup * fix: Preserve erasure retries and reconcile removed embeddings * test: Match deletion regression formatting * fix: Preserve search intent after vector cleanup * fix: Repair retained project search files before tool execution * fix: Normalize Retained Project File Runtime Types * test: Use the Storage Source Enum in Project Repair Coverage * fix: Confirm Secondary Cleanup and Preserve Repair Policy * test: Correct Repair Fixture Types and Cleanup Retry Assertions * fix: Reconcile Code Copies Compatibly and Charge Shared File Budgets * fix: Reserve Admitted File Budgets Before Deferred Screening * fix: Retain Concurrent File Copies and Scope Cleanup by Tenant * fix: Bound Erasure File Checks and Correct Cleanup Fixtures * fix: Preserve Provider-Only Assistant File Deletion * fix: Reconcile Primary Vector Deletions Before Finalization * fix: Finalize Successful Files in Mixed Cleanup Batches * test: Load Real File Finalizer in Cleanup Integration Tests * fix: Preserve Project Routing MIME and Fence File Storage Operations * fix: Validate Provisioning Principal and Type Project Repair Fixtures * fix: Recover Provisioning Claims and Retain Uncertain Storage Outcomes * fix: Normalize File Recovery Results and Reconcile Transport Attempts * test: Preserve Deployment Routes in Host Provisioning Fixtures * fix: Recover Deletion Claims and Fence Generated Output Writes * fix: Preserve Generated Output Commit Snapshots * test: Use Persisted Owner Type in Generated File Fixture * test: Type Generated Output Fixture with Its Commit Contract * fix: Preserve Publication Error Handling and Cleanup Test Boundaries --------- Co-authored-by: Lia <lia@librechat.ai>
75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
import type {
|
|
DeleteFilesOptions,
|
|
DeleteFilesResult,
|
|
FileDeletionSnapshot,
|
|
} from '@librechat/data-schemas';
|
|
import type { FileDeletionOutcome } from './deletion';
|
|
import { getSafeErrorMetadata } from '../utils/errors';
|
|
|
|
/** Finalizes only confirmed metadata deletions, preserving failed snapshots for retry. */
|
|
export async function finalizeFileDeletion(
|
|
{
|
|
deletedFileIds,
|
|
failedFileIds,
|
|
files,
|
|
userId,
|
|
}: {
|
|
deletedFileIds: string[];
|
|
failedFileIds: string[];
|
|
files: FileDeletionSnapshot[];
|
|
userId: string;
|
|
},
|
|
{
|
|
deleteFiles,
|
|
removeAgentResourceFilesFromAllAgents,
|
|
logger,
|
|
}: {
|
|
deleteFiles: (
|
|
ids: string[],
|
|
user?: string,
|
|
options?: DeleteFilesOptions,
|
|
) => Promise<DeleteFilesResult>;
|
|
removeAgentResourceFilesFromAllAgents: (params: { file_ids: string[] }) => Promise<unknown>;
|
|
logger: { error: (message: string, metadata: unknown) => void };
|
|
},
|
|
): Promise<Required<FileDeletionOutcome>> {
|
|
const failed = new Set(failedFileIds);
|
|
const pending = [...new Set(deletedFileIds)].filter((id) => !failed.has(id));
|
|
if (pending.length === 0) return { deletedFileIds: [], failedFileIds: [...failed] };
|
|
|
|
let confirmed: string[];
|
|
try {
|
|
const result = await deleteFiles(pending, undefined, { files, userId });
|
|
if (!Array.isArray(result?.deletedFileIds) || !Array.isArray(result?.failedFileIds)) {
|
|
throw new Error('File metadata deletion was not confirmed');
|
|
}
|
|
const requested = new Set(pending);
|
|
const deleted = new Set(result.deletedFileIds);
|
|
for (const id of result.failedFileIds) {
|
|
if (!requested.has(id) || deleted.has(id)) throw new Error('Invalid file deletion outcome');
|
|
failed.add(id);
|
|
}
|
|
for (const id of deleted) {
|
|
if (!requested.has(id)) throw new Error('Invalid file deletion outcome');
|
|
}
|
|
confirmed = pending.filter((id) => deleted.has(id));
|
|
for (const id of pending) {
|
|
if (!deleted.has(id)) failed.add(id);
|
|
}
|
|
} catch (error) {
|
|
logger.error(
|
|
'Error deleting file metadata after storage deletion',
|
|
getSafeErrorMetadata(error),
|
|
);
|
|
throw error;
|
|
}
|
|
|
|
if (confirmed.length > 0) {
|
|
try {
|
|
await removeAgentResourceFilesFromAllAgents({ file_ids: confirmed });
|
|
} catch (error) {
|
|
logger.error('Error cleaning up orphaned agent file references', getSafeErrorMetadata(error));
|
|
}
|
|
}
|
|
return { deletedFileIds: confirmed, failedFileIds: [...failed] };
|
|
}
|