diff --git a/.github/workflows/agents-integration-tests.yml b/.github/workflows/agents-integration-tests.yml index 102e7db995..f015ee618a 100644 --- a/.github/workflows/agents-integration-tests.yml +++ b/.github/workflows/agents-integration-tests.yml @@ -1,9 +1,12 @@ -name: Agents Integration Tests +name: Integration Tests -# Runs the packages/api `src/agents/**` integration specs (e.g. the durable HITL -# checkpointer and cross-replica subagent delivery against real MongoDB and Redis). These -# are `*.integration.spec.ts`, which `test:ci` deliberately excludes — without this -# job they run nowhere and their regressions guard nothing. +# Runs every packages/api `*.integration.spec.ts` / `*.integration.test.ts` suite (e.g. the +# durable HITL checkpointer and cross-replica subagent delivery against real MongoDB and +# Redis, the admin config secret registry against a real Config collection, MCP flows +# against in-process servers). `test:ci` deliberately excludes them, and the Redis-backed +# `*.cache_integration` / `*.stream_integration` suites run in cache-integration-tests.yml — +# without this job they run nowhere and their regressions guard nothing. Selection is by +# suffix, not folder, so a suite added anywhere under src is picked up. on: pull_request: branches: @@ -11,9 +14,9 @@ on: - dev - dev-staging - release/* - # The suite builds and consumes data-provider and data-schemas and imports + # The suites build and consume data-provider and data-schemas and import # across packages/api (the build-cache keys below hash all three src trees), - # so it must re-run on any of them — not just src/agents. + # so they must re-run on any of them. paths: - 'packages/api/src/**' - 'packages/api/package.json' @@ -106,9 +109,9 @@ jobs: if: steps.cache-api.outputs.cache-hit != 'true' run: npm run build:api - - name: Run agents integration tests + - name: Run integration tests working-directory: packages/api env: NODE_ENV: test REDIS_URI: redis://127.0.0.1:6379 - run: npm run test:agents-integration + run: npm run test:integration diff --git a/packages/api/package.json b/packages/api/package.json index 51e60b61c2..7306a75a55 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -33,11 +33,12 @@ "build:watch:prod": "tsdown --watch", "test": "jest --coverage --watch --testPathIgnorePatterns=\"\\.*integration\\.|\\.*helper\\.|__tests__/helpers/|\\.*manual\\.spec\\.\"", "test:ci": "jest --coverage --ci --testPathIgnorePatterns=\"\\.*integration\\.|\\.*helper\\.|__tests__/helpers/|\\.*manual\\.spec\\.\"", - "test:cache-integration:core": "jest --testPathPatterns=\"src/cache/.*\\.cache_integration\\.spec\\.ts$\" --coverage=false", - "test:cache-integration:cluster": "jest --testPathPatterns=\"src/cluster/.*\\.cache_integration\\.spec\\.ts$\" --coverage=false --runInBand", - "test:cache-integration:mcp": "jest --testPathPatterns=\"src/mcp/.*\\.cache_integration\\.spec\\.ts$\" --coverage=false", - "test:cache-integration:stream": "jest --testPathPatterns=\"src/stream/.*\\.stream_integration\\.spec\\.ts$\" --coverage=false --runInBand --forceExit", + "test:cache-integration:core": "jest --testPathPatterns=\"src/(cache|middleware)/.*\\.cache_integration\\.(spec|test)\\.ts$\" --coverage=false", + "test:cache-integration:cluster": "jest --testPathPatterns=\"src/cluster/.*\\.cache_integration\\.(spec|test)\\.ts$\" --coverage=false --runInBand", + "test:cache-integration:mcp": "jest --testPathPatterns=\"src/mcp/.*\\.cache_integration\\.(spec|test)\\.ts$\" --coverage=false", + "test:cache-integration:stream": "jest --testPathPatterns=\"\\.stream_integration\\.(spec|test)\\.ts$\" --coverage=false --runInBand --forceExit", "test:cache-integration": "npm run test:cache-integration:core && npm run test:cache-integration:cluster && npm run test:cache-integration:mcp && npm run test:cache-integration:stream", + "test:integration": "jest --testPathPatterns=\"\\.integration\\.(spec|test)\\.ts$\" --coverage=false --runInBand --forceExit", "test:s3-integration": "jest --testPathPatterns=\"src/storage/s3/.*\\.integration\\.spec\\.ts$\" --coverage=false --runInBand", "test:agents-integration": "jest --testPathPatterns=\"src/agents/.*\\.integration\\.spec\\.ts$\" --coverage=false --runInBand --forceExit", "verify": "npm run test:ci", diff --git a/packages/api/src/admin/secrets.integration.spec.ts b/packages/api/src/admin/secrets.integration.spec.ts index 7b4a1ec2c2..b1297bb187 100644 --- a/packages/api/src/admin/secrets.integration.spec.ts +++ b/packages/api/src/admin/secrets.integration.spec.ts @@ -1,5 +1,7 @@ import mongoose from 'mongoose'; import { MongoMemoryServer } from 'mongodb-memory-server'; +import { PrincipalModel, PrincipalType } from 'librechat-data-provider'; +import type { Config } from '@librechat/data-schemas'; import type { Response } from 'express'; import type { ServerRequest } from '~/types/http'; @@ -8,12 +10,16 @@ process.env.CREDS_KEY = process.env.CREDS_IV = process.env.CREDS_IV ?? '0123456789abcdef0123456789abcdef'; type DataSchemas = typeof import('@librechat/data-schemas'); +type ConfigMethods = ReturnType; type AdminConfigHandlers = ReturnType; let mongoServer: MongoMemoryServer; let handlers: AdminConfigHandlers; +let basePrincipalId: string; let decryptV3: DataSchemas['decryptV3']; +let patchConfigFields: ConfigMethods['patchConfigFields']; let getSecretPreview: typeof import('./secrets').getSecretPreview; +let encryptConfigSecretFields: typeof import('./secrets').encryptConfigSecretFields; interface SecretFieldCase { /** Dot-path of the secret field */ @@ -30,15 +36,11 @@ interface SecretFieldCase { const SECRET = 'sk-super-secret-literal'; +/** + * Secrets written through the generic config API. `langfuse.secretKey` is absent because only the + * dedicated Langfuse connection API writes that section; the generic API strips it. + */ const SECRET_FIELD_CASES: SecretFieldCase[] = [ - { - path: 'langfuse.secretKey', - previewPath: 'langfuse.secretKeyPreview', - section: 'langfuse', - object: { publicKey: 'pk-lf-1', secretKey: SECRET }, - siblingPath: 'langfuse.publicKey', - siblingValue: 'pk-lf-2', - }, { path: 'ocr.apiKey', previewPath: 'ocr.apiKeyPreview', @@ -213,11 +215,17 @@ function getAtPath(root: unknown, path: string): unknown { return cursor; } -async function readRawOverrides(principalId: string): Promise> { +type RawConfig = Pick & { overrides: Record }; + +async function readRawConfig(principalId: string): Promise { const doc = await mongoose.models.Config.findOne({ principalId }); expect(doc).not.toBeNull(); expect(doc!.$isNew).toBe(false); - return (doc!.toObject() as { overrides: Record }).overrides; + return doc!.toObject() as RawConfig; +} + +async function readRawOverrides(principalId: string): Promise> { + return (await readRawConfig(principalId)).overrides; } let principalCounter = 0; @@ -229,19 +237,20 @@ function nextPrincipalId(): string { beforeAll(async () => { jest.resetModules(); const dataSchemas = await import('@librechat/data-schemas'); - ({ decryptV3 } = dataSchemas); + ({ decryptV3, BASE_CONFIG_PRINCIPAL_ID: basePrincipalId } = dataSchemas); jest.spyOn(dataSchemas.logger, 'error').mockReturnValue(dataSchemas.logger); jest.spyOn(dataSchemas.logger, 'warn').mockReturnValue(dataSchemas.logger); jest.spyOn(dataSchemas.logger, 'info').mockReturnValue(dataSchemas.logger); jest.spyOn(dataSchemas.logger, 'debug').mockReturnValue(dataSchemas.logger); const { createAdminConfigHandlers } = await import('./config'); - ({ getSecretPreview } = await import('./secrets')); + ({ getSecretPreview, encryptConfigSecretFields } = await import('./secrets')); mongoServer = await MongoMemoryServer.create(); await mongoose.connect(mongoServer.getUri()); dataSchemas.createModels(mongoose); const methods = dataSchemas.createMethods(mongoose); + ({ patchConfigFields } = methods); handlers = createAdminConfigHandlers({ listAllConfigs: methods.listAllConfigs, @@ -491,6 +500,140 @@ describe('config secret registry — real handlers against a real Config collect }, ); + describe('langfuse.secretKey (dedicated Langfuse connection API)', () => { + type RefusedWrite = [ + string, + 'patchConfigField' | 'tombstoneConfigField' | 'deleteConfigField', + Record, + ]; + + const secretPath = 'langfuse.secretKey'; + const previewPath = 'langfuse.secretKeyPreview'; + + function baseReq(overrides: Record = {}): ServerRequest { + return mockReq({ + params: { principalType: 'role', principalId: basePrincipalId }, + ...overrides, + }); + } + + /** Persists what the dedicated connection API writes, through the same shared secret helper. */ + async function seedLangfuseConnection(): Promise { + await mongoose.models.Config.deleteOne({ principalId: basePrincipalId }); + await patchConfigFields( + PrincipalType.ROLE, + basePrincipalId, + PrincipalModel.ROLE, + encryptConfigSecretFields({ + 'langfuse.enabled': true, + 'langfuse.destination': 'eu', + 'langfuse.publicKey': 'pk-lf-1', + [secretPath]: SECRET, + }), + 10, + ); + return readRawConfig(basePrincipalId); + } + + it('keeps the stored secret encrypted at rest and redacts it from generic reads', async () => { + const { overrides: stored } = await seedLangfuseConnection(); + expect(getAtPath(stored, secretPath)).toMatch(/^v3:/); + expect(decryptV3(getAtPath(stored, secretPath) as string)).toBe(SECRET); + expect(getAtPath(stored, previewPath)).toBe(getSecretPreview(SECRET)); + + const getRes = mockRes(); + await handlers.getConfig(baseReq(), getRes); + expect(getRes.statusCode).toBe(200); + expect(JSON.stringify(getRes.body)).not.toContain(SECRET); + expect(JSON.stringify(getRes.body)).not.toContain('v3:'); + const overrides = (getRes.body!.config as { overrides: Record }).overrides; + expect(getAtPath(overrides, secretPath)).toBeUndefined(); + expect(getAtPath(overrides, previewPath)).toBe(getSecretPreview(SECRET)); + expect(getAtPath(overrides, 'langfuse.publicKey')).toBe('pk-lf-1'); + + const listRes = mockRes(); + await handlers.listConfigs(mockReq(), listRes); + expect(listRes.statusCode).toBe(200); + expect(JSON.stringify(listRes.body)).not.toContain(SECRET); + expect(JSON.stringify(listRes.body)).not.toContain('v3:'); + }); + + it.each([ + [ + 'a dotted patch that replaces it', + 'patchConfigField', + { body: { entries: [{ fieldPath: secretPath, value: 'sk-lf-replacement' }] } }, + ], + [ + 'a dotted patch that clears it', + 'patchConfigField', + { body: { entries: [{ fieldPath: secretPath, value: '' }] } }, + ], + ['a field tombstone', 'tombstoneConfigField', { body: { fieldPath: secretPath } }], + ['a field delete', 'deleteConfigField', { query: { fieldPath: secretPath } }], + ])('ignores %s without writing the base config', async (_label, handler, request) => { + const before = await seedLangfuseConnection(); + const res = mockRes(); + await handlers[handler](baseReq(request), res); + expect(res.statusCode).toBe(200); + + const after = await readRawConfig(basePrincipalId); + expect(after.configVersion).toBe(before.configVersion); + expect(after.overrides).toEqual(before.overrides); + }); + + it('carries the stored secret through a full base-config replacement', async () => { + const before = await seedLangfuseConnection(); + const res = mockRes(); + await handlers.upsertConfigOverrides( + baseReq({ + body: { + overrides: { + interface: { modelSelect: false }, + langfuse: { + enabled: false, + publicKey: 'pk-lf-caller', + secretKey: 'sk-lf-replacement', + }, + }, + }, + }), + res, + ); + expect(res.statusCode).toBe(200); + expect(JSON.stringify(res.body)).not.toContain(SECRET); + + const after = await readRawConfig(basePrincipalId); + expect(after.configVersion).toBe(before.configVersion + 1); + expect(getAtPath(after.overrides, 'interface.modelSelect')).toBe(false); + expect(after.overrides.langfuse).toEqual(before.overrides.langfuse); + }); + + it('creates no role config from a Langfuse-only generic write', async () => { + const principalId = nextPrincipalId(); + const patchRes = mockRes(); + await handlers.patchConfigField( + mockReq({ + params: { principalType: 'role', principalId }, + body: { entries: [{ fieldPath: secretPath, value: SECRET }] }, + }), + patchRes, + ); + const upsertRes = mockRes(); + await handlers.upsertConfigOverrides( + mockReq({ + params: { principalType: 'role', principalId }, + body: { overrides: { langfuse: { publicKey: 'pk-lf-1', secretKey: SECRET } } }, + }), + upsertRes, + ); + + expect(patchRes.statusCode).toBe(200); + expect(upsertRes.statusCode).toBe(200); + expect(await mongoose.models.Config.exists({ principalId })).toBeNull(); + }); + }); + describe.each(PLACEHOLDER_CASES)('$path env placeholder', ({ path, placeholder }) => { it('stores and returns `${ENV_VAR}` references without encryption or redaction', async () => { const principalId = nextPrincipalId(); diff --git a/packages/api/src/mcp/registry/__tests__/MCPReinitRecovery.integration.test.ts b/packages/api/src/mcp/registry/__tests__/MCPReinitRecovery.integration.test.ts index cd21502432..6c7cca6393 100644 --- a/packages/api/src/mcp/registry/__tests__/MCPReinitRecovery.integration.test.ts +++ b/packages/api/src/mcp/registry/__tests__/MCPReinitRecovery.integration.test.ts @@ -36,6 +36,7 @@ import { MCPConnection } from '~/mcp/connection'; import { MCPManager } from '~/mcp/MCPManager'; jest.mock('@librechat/data-schemas', () => ({ + ...jest.requireActual('@librechat/data-schemas'), logger: { info: jest.fn(), warn: jest.fn(), @@ -107,6 +108,22 @@ async function safeDisconnect(conn: MCPConnection | null): Promise { await conn.disconnect(); } +function makeUser(): IUser { + return { + _id: new Types.ObjectId(), + id: new Types.ObjectId().toString(), + username: 'testuser', + email: 'test@example.com', + name: 'Test', + avatar: '', + provider: 'email', + role: 'user', + emailVerified: true, + createdAt: new Date(), + updatedAt: new Date(), + } as IUser; +} + function getFreePort(): Promise { return new Promise((resolve, reject) => { const srv = net.createServer(); @@ -137,6 +154,12 @@ interface TestServer { close: () => Promise; } +interface ReinitOutcome { + success: boolean; + /** Tool count from a complete snapshot; `null` when the snapshot was incomplete */ + tools: number | null; +} + async function createMCPServerOnPort(port: number): Promise { const sessions = new Map(); @@ -396,26 +419,15 @@ describe('MCP reinitialize recovery – integration (issue #12143)', () => { server = await createMCPServerOnPort(deadPort); const flowManager = new FlowStateManager(new Keyv(), { ttl: 60_000 }); - const makeUser = (): IUser => - ({ - _id: new Types.ObjectId(), - id: new Types.ObjectId().toString(), - username: 'testuser', - email: 'test@example.com', - name: 'Test', - avatar: '', - provider: 'email', - role: 'user', - emailVerified: true, - createdAt: new Date(), - updatedAt: new Date(), - }) as IUser; /** - * Replicate reinitMCPServer logic: check inspectionFailed → reinspect → getConnection. - * Each call uses a distinct user to simulate concurrent requests from different users. + * Replicate reinitMCPServer logic: check inspectionFailed → reinspect → getConnection → + * tools snapshot. Each call uses a distinct user to simulate concurrent requests from + * different users. Concurrent reinspections can each succeed, and a later one replaces the + * app connection an earlier call was handed; that call's snapshot is incomplete, which + * reinitMCPServer answers by keeping the cached tools, reported here as `tools: null`. */ - async function simulateReinitMCPServer(): Promise<{ success: boolean; tools: number }> { + async function simulateReinitMCPServer(): Promise { const user = makeUser(); const config = await registry.getServerConfig(serverName, user.id); if (config?.inspectionFailed) { @@ -435,8 +447,8 @@ describe('MCP reinitialize recovery – integration (issue #12143)', () => { forceNew: true, }); - const tools = await connection.fetchTools(); - return { success: true, tools: tools.length }; + const snapshot = await connection.fetchToolsSnapshot(); + return { success: true, tools: snapshot.complete ? snapshot.tools.length : null }; } const n = 3 + Math.floor(Math.random() * 5); // 3–7 concurrent calls @@ -449,14 +461,16 @@ describe('MCP reinitialize recovery – integration (issue #12143)', () => { expect(r.status).toBe('fulfilled'); } - const values = (results as PromiseFulfilledResult<{ success: boolean; tools: number }>[]).map( - (r) => r.value, - ); + const values = (results as PromiseFulfilledResult[]).map((r) => r.value); // At least one full reinit must succeed with tools const succeeded = values.filter((v) => v.success); expect(succeeded.length).toBeGreaterThanOrEqual(1); - for (const s of succeeded) { + + // A connection created after the last reinspection is never replaced, so one snapshot completes + const listed = succeeded.filter((v) => v.tools !== null); + expect(listed.length).toBeGreaterThanOrEqual(1); + for (const s of listed) { expect(s.tools).toBe(2); } @@ -471,6 +485,38 @@ describe('MCP reinitialize recovery – integration (issue #12143)', () => { expect(finalConfig!.tools).toContain('echo'); }); + it('reports an incomplete tool snapshot when a newer config replaces the held app connection', async () => { + const deadPort = await getFreePort(); + const serverName = 'replaced-connection'; + (MCPManager as unknown as { instance: null }).instance = null; + await MCPManager.createInstance({ + [serverName]: { type: 'streamable-http', url: `http://127.0.0.1:${deadPort}/` }, + }); + const mcpManager = MCPManager.getInstance(); + server = await createMCPServerOnPort(deadPort); + const flowManager = new FlowStateManager(new Keyv(), { ttl: 60_000 }); + + const firstUser = makeUser(); + await registry.reinspectServer(serverName, 'CACHE', firstUser.id); + const held = await mcpManager.getConnection({ serverName, user: firstUser, flowManager }); + + // What another replica's reinspection or an admin edit does: store a newer config + await registry.updateServer(serverName, { type: 'streamable-http', url: server.url }, 'CACHE'); + const replacement = await mcpManager.getConnection({ + serverName, + user: makeUser(), + flowManager, + }); + expect(replacement).not.toBe(held); + + // reinitMCPServer keeps cached tools for an incomplete snapshot; a complete empty one would + // publish an empty catalog + await expect(held.fetchToolsSnapshot()).resolves.toMatchObject({ complete: false, tools: [] }); + const current = await replacement.fetchToolsSnapshot(); + expect(current.complete).toBe(true); + expect(current.tools.map((tool) => tool.name).sort()).toEqual(['echo', 'greet']); + }); + it('reinspectServer should throw MCPInspectionFailedError when the server is still unreachable', async () => { const deadPort = await getFreePort(); const configs: t.MCPServers = {