Repository navigation
馃洬 feat: Enable Self-Service Code Worker Pairing #15367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e0861d6
feat: add self-service code worker pairing
danny-avila 4b5295a
fix: enforce code worker principal boundaries
danny-avila cfaf26d
fix: separate pairing control planes from execution
danny-avila 25238d7
fix: preserve principal worker routing
danny-avila ab726fd
fix: pin artifact traffic to code workers
danny-avila 0ea75c5
fix: authenticate code worker selection
danny-avila 0712566
test: mock worker-aware artifact headers
danny-avila 6ea3bab
fix: harden self-service code worker routing
danny-avila a9e255c
fix: fail closed for principal code pairing
danny-avila 1e1a798
merge: update self-service code pairing with dev
danny-avila b49c906
style: format code execution headers
danny-avila b83b296
fix: close self-service pairing gaps
danny-avila 8c207b3
fix: satisfy code environment type checks
danny-avila File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| const rateLimit = require('express-rate-limit'); | ||
| const { limiterCache } = require('@librechat/api'); | ||
|
|
||
| let configuredCodeEnvironmentPairingLimiter; | ||
|
|
||
| function positiveInteger(value, fallback) { | ||
| const parsed = Number(value); | ||
| return Number.isSafeInteger(parsed) && parsed >= 1 ? parsed : fallback; | ||
| } | ||
|
|
||
| const codeEnvironmentPairingLimiter = (req, res, next) => { | ||
| if (configuredCodeEnvironmentPairingLimiter == null) { | ||
| const max = positiveInteger(process.env.CODE_ENVIRONMENT_PAIRING_USER_MAX, 5); | ||
| const windowInMinutes = positiveInteger(process.env.CODE_ENVIRONMENT_PAIRING_USER_WINDOW, 60); | ||
|
|
||
| configuredCodeEnvironmentPairingLimiter = rateLimit({ | ||
| windowMs: windowInMinutes * 60 * 1000, | ||
| max, | ||
| handler: (limitedReq, limitedRes) => { | ||
| const resetAt = limitedReq.rateLimit?.resetTime?.getTime?.(); | ||
| const retryAfterSeconds = Number.isFinite(resetAt) | ||
| ? Math.max(1, Math.ceil((resetAt - Date.now()) / 1000)) | ||
| : Math.max(1, Math.ceil(windowInMinutes * 60)); | ||
| limitedRes.set('Retry-After', String(retryAfterSeconds)); | ||
| return limitedRes.status(429).json({ | ||
| error: { | ||
| code: 'code_environment_pairing_rate_limited', | ||
| message: 'Code environment pairing rate limit exceeded.', | ||
| type: 'rate_limit_error', | ||
| }, | ||
| }); | ||
| }, | ||
| keyGenerator: (limitedReq) => String(limitedReq.user.id), | ||
| store: limiterCache('code_environment_pairing_user_limiter'), | ||
| }); | ||
| } | ||
|
|
||
| return configuredCodeEnvironmentPairingLimiter(req, res, next); | ||
| }; | ||
|
|
||
| module.exports = { codeEnvironmentPairingLimiter }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| const express = require('express'); | ||
| const request = require('supertest'); | ||
|
|
||
| const mockLimiter = jest.fn((_req, _res, next) => next()); | ||
| const mockRateLimit = jest.fn(() => mockLimiter); | ||
|
|
||
| jest.mock('express-rate-limit', () => mockRateLimit); | ||
| jest.mock('@librechat/api', () => ({ | ||
| limiterCache: jest.fn(() => ({})), | ||
| })); | ||
|
|
||
| describe('code environment pairing rate limiter', () => { | ||
| let originalEnv; | ||
|
|
||
| beforeEach(() => { | ||
| originalEnv = { ...process.env }; | ||
| jest.resetModules(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.env = originalEnv; | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('uses a configurable per-user bucket', () => { | ||
| process.env.CODE_ENVIRONMENT_PAIRING_USER_MAX = '3'; | ||
| process.env.CODE_ENVIRONMENT_PAIRING_USER_WINDOW = '15'; | ||
| const { codeEnvironmentPairingLimiter } = require('./code'); | ||
| const { limiterCache } = require('@librechat/api'); | ||
| const req = { user: { id: 'user-1' } }; | ||
| const next = jest.fn(); | ||
|
|
||
| codeEnvironmentPairingLimiter(req, {}, next); | ||
|
|
||
| expect(mockRateLimit).toHaveBeenLastCalledWith( | ||
| expect.objectContaining({ | ||
| max: 3, | ||
| windowMs: 900_000, | ||
| keyGenerator: expect.any(Function), | ||
| }), | ||
| ); | ||
| expect(mockRateLimit.mock.calls.at(-1)[0].keyGenerator(req)).toBe('user-1'); | ||
| expect(limiterCache).toHaveBeenCalledWith('code_environment_pairing_user_limiter'); | ||
| expect(mockLimiter).toHaveBeenCalledWith(req, {}, next); | ||
| }); | ||
|
|
||
| it('falls back to safe limits for invalid environment values', () => { | ||
| process.env.CODE_ENVIRONMENT_PAIRING_USER_MAX = 'NaN'; | ||
| process.env.CODE_ENVIRONMENT_PAIRING_USER_WINDOW = '-1'; | ||
| const { codeEnvironmentPairingLimiter } = require('./code'); | ||
|
|
||
| codeEnvironmentPairingLimiter({ user: { id: 'user-1' } }, {}, jest.fn()); | ||
|
|
||
| expect(mockRateLimit).toHaveBeenLastCalledWith( | ||
| expect.objectContaining({ max: 5, windowMs: 3_600_000 }), | ||
| ); | ||
| }); | ||
|
|
||
| it('returns an actionable JSON response when the limit is exceeded', async () => { | ||
| const { codeEnvironmentPairingLimiter } = require('./code'); | ||
|
|
||
| codeEnvironmentPairingLimiter({ user: { id: 'user-1' } }, {}, jest.fn()); | ||
| const options = mockRateLimit.mock.calls.at(-1)[0]; | ||
| const app = express(); | ||
| app.use((req, _res, next) => { | ||
| req.rateLimit = { resetTime: new Date(Date.now() + 30_000) }; | ||
| next(); | ||
| }); | ||
| app.post('/api/code-environments/pairings', options.handler); | ||
| const response = await request(app).post('/api/code-environments/pairings'); | ||
|
|
||
| expect(response.status).toBe(429); | ||
| expect(response.headers['retry-after']).toBe('30'); | ||
| expect(response.body).toEqual({ | ||
| error: { | ||
| code: 'code_environment_pairing_rate_limited', | ||
| message: 'Code environment pairing rate limit exceeded.', | ||
| type: 'rate_limit_error', | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.