🪡 fix: Handle Missing Skill File Upsert Metadata (#13520)
Some checks are pending
Docker Dev Branch Images Build / build (Dockerfile, lc-dev, node) (push) Waiting to run
Docker Dev Branch Images Build / build (Dockerfile.multi, lc-dev-api, api-build) (push) Waiting to run
GitNexus Index / index (push) Waiting to run
GitNexus Index / post-index (push) Blocked by required conditions
Docker Dev Images Build / build (Dockerfile, librechat-dev, node) (push) Waiting to run
Docker Dev Images Build / build (Dockerfile.multi, librechat-dev-api, api-build) (push) Waiting to run
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
Sync Helm Chart Tags / Ignore non-main push (push) Waiting to run
Sync Helm Chart Tags / Sync chart tags (push) Waiting to run

This commit is contained in:
Danny Avila 2026-06-04 21:06:12 -04:00 committed by GitHub
parent 44ed7864fb
commit 40ec77e061
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 159 additions and 22 deletions

View file

@ -74,6 +74,11 @@ async function saveSkillFileContent({ req, skillId, relativePath, content, mimeT
author: req.user._id ?? req.user.id,
tenantId,
});
if (!result) {
const error = new Error('Skill file save failed to persist metadata');
error.code = 'SKILL_FILE_UPSERT_NOT_FOUND';
throw error;
}
} catch (error) {
const { deleteFile } = getStrategyFunctions(storage.source);
if (deleteFile) {

View file

@ -0,0 +1,102 @@
const mockSaveBuffer = jest.fn();
const mockDeleteFile = jest.fn();
const mockGetStrategyFunctions = jest.fn();
const mockGetFileStrategy = jest.fn();
const mockGetStorageMetadata = jest.fn();
const mockResolveRequestTenantId = jest.fn();
jest.mock('~/server/services/Files/strategies', () => ({
getStrategyFunctions: (...args) => mockGetStrategyFunctions(...args),
}));
jest.mock('~/server/services/Files/Code/crud', () => ({
batchUploadCodeEnvFiles: jest.fn(),
}));
jest.mock('~/server/services/Files/Code/process', () => ({
getSessionInfo: jest.fn(),
checkIfActive: jest.fn(),
readSandboxFile: jest.fn(),
writeSandboxFile: jest.fn(),
}));
jest.mock('@librechat/api', () => ({
checkAccess: jest.fn(),
enrichWithSkillConfigurable: jest.fn(),
getStorageMetadata: (...args) => mockGetStorageMetadata(...args),
resolveRequestTenantId: (...args) => mockResolveRequestTenantId(...args),
}));
jest.mock('librechat-data-provider', () => ({
AccessRoleIds: { SKILL_OWNER: 'SKILL_OWNER' },
FileContext: { skill_file: 'skill_file' },
PermissionBits: { EDIT: 2 },
Permissions: { USE: 'USE', CREATE: 'CREATE' },
PermissionTypes: { SKILLS: 'SKILLS' },
PrincipalType: { USER: 'USER' },
ResourceType: { SKILL: 'SKILL' },
isEphemeralAgentId: jest.fn(() => false),
}));
jest.mock('~/server/services/PermissionService', () => ({
checkPermission: jest.fn(),
grantPermission: jest.fn(),
}));
jest.mock('~/server/utils/getFileStrategy', () => ({
getFileStrategy: (...args) => mockGetFileStrategy(...args),
}));
const mockDb = {
getSkillFileByPath: jest.fn(),
upsertSkillFile: jest.fn(),
};
jest.mock('~/models', () => mockDb);
const { getSkillToolDeps } = require('./skillDeps');
describe('skillDeps saveSkillFileContent', () => {
beforeEach(() => {
jest.clearAllMocks();
mockGetFileStrategy.mockReturnValue('s3');
mockGetStrategyFunctions.mockReturnValue({
saveBuffer: mockSaveBuffer,
deleteFile: mockDeleteFile,
});
mockSaveBuffer.mockResolvedValue('https://files.example.test/uploads/file.txt');
mockDeleteFile.mockResolvedValue(undefined);
mockGetStorageMetadata.mockReturnValue({
storageKey: 'uploads/file.txt',
storageRegion: 'us-east-2',
});
mockResolveRequestTenantId.mockReturnValue('tenant-1');
mockDb.getSkillFileByPath.mockResolvedValue(null);
});
it('cleans up the uploaded object when metadata upsert returns no row', async () => {
mockDb.upsertSkillFile.mockResolvedValue(null);
await expect(
getSkillToolDeps().saveSkillFileContent({
req: {
user: { id: 'user-1', _id: 'user-1' },
config: {},
},
skillId: 'skill-1',
relativePath: 'references/template.html',
content: '<html></html>',
mimeType: 'text/html',
}),
).rejects.toMatchObject({ code: 'SKILL_FILE_UPSERT_NOT_FOUND' });
expect(mockDeleteFile).toHaveBeenCalledWith(
expect.objectContaining({ user: expect.objectContaining({ id: 'user-1' }) }),
{
filepath: 'https://files.example.test/uploads/file.txt',
user: 'user-1',
tenantId: 'tenant-1',
},
);
});
});