mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-14 10:39:32 +01:00
feat(limits): make the global session cap configurable with CODEMAN_MAX_SESSIONS
The cap of 50 concurrent sessions was a hard-coded constant, so a machine with the memory for more (or a small box that wants fewer) had to patch the source. CODEMAN_MAX_SESSIONS now sets it at startup. The default stays 50, and anything that is not a positive integer (empty, 0, negative, fractional, 1e3, out of safe-integer range) falls back to it, so a typo never locks session creation. Every site keeps reading MAX_CONCURRENT_SESSIONS, so the session routes, cron launches, the scheduled-run loop and the multi-user per-user cap (half the global cap unless CODEMAN_MAX_SESSIONS_PER_USER is set) all follow the variable. The test goes through sessionCapacityState and sessionCapacityMessage, the check every session-creating route uses. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
39077281d1
commit
04eb1ed158
7 files changed
+112
-8
No files matched your search
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* @fileoverview The global session cap can be set with `CODEMAN_MAX_SESSIONS`.
|
||||
*
|
||||
* Tested through `sessionCapacityState` / `sessionCapacityMessage`, the check every
|
||||
* session-creating route goes through, so a regression in the env parsing OR in
|
||||
* the wiring from `map-limits` to the routes (and to the derived per-user cap in
|
||||
* multi-user mode) fails here.
|
||||
*/
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
type Helpers = typeof import('../../src/web/route-helpers.js');
|
||||
|
||||
/** A sessions map of the given size, every session owned by `owner`. */
|
||||
function sessionsOf(n: number, owner?: string): ReadonlyMap<string, never> {
|
||||
return new Map(Array.from({ length: n }, (_, i) => [`s${i}`, { id: `s${i}`, owner } as never]));
|
||||
}
|
||||
|
||||
/** Fresh module graph, so map-limits re-reads the environment at import. */
|
||||
async function loadWithEnv(env: Record<string, string | undefined>): Promise<Helpers> {
|
||||
vi.resetModules();
|
||||
vi.stubEnv('CODEMAN_MAX_SESSIONS', undefined as unknown as string);
|
||||
vi.stubEnv('CODEMAN_MAX_SESSIONS_PER_USER', undefined as unknown as string);
|
||||
vi.stubEnv('CODEMAN_MULTIUSER', undefined as unknown as string);
|
||||
for (const [key, value] of Object.entries(env)) vi.stubEnv(key, value as string);
|
||||
return import('../../src/web/route-helpers.js');
|
||||
}
|
||||
|
||||
describe('global session cap (CODEMAN_MAX_SESSIONS)', () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
it('stays at 50 when the variable is not set', async () => {
|
||||
const { sessionCapacityState, sessionCapacityMessage } = await loadWithEnv({});
|
||||
expect(sessionCapacityState(sessionsOf(49)).atGlobalCap).toBe(false);
|
||||
expect(sessionCapacityState(sessionsOf(50)).atGlobalCap).toBe(true);
|
||||
expect(sessionCapacityMessage(sessionsOf(50))).toContain('(50)');
|
||||
});
|
||||
|
||||
it('raises the cap', async () => {
|
||||
const { sessionCapacityState, sessionCapacityMessage } = await loadWithEnv({ CODEMAN_MAX_SESSIONS: '120' });
|
||||
expect(sessionCapacityState(sessionsOf(50)).atGlobalCap).toBe(false);
|
||||
expect(sessionCapacityState(sessionsOf(119)).atGlobalCap).toBe(false);
|
||||
expect(sessionCapacityState(sessionsOf(120)).atGlobalCap).toBe(true);
|
||||
expect(sessionCapacityMessage(sessionsOf(120))).toContain('(120)');
|
||||
});
|
||||
|
||||
it('lowers the cap, ignoring surrounding whitespace', async () => {
|
||||
const { sessionCapacityState } = await loadWithEnv({ CODEMAN_MAX_SESSIONS: ' 3 ' });
|
||||
expect(sessionCapacityState(sessionsOf(2)).atGlobalCap).toBe(false);
|
||||
expect(sessionCapacityState(sessionsOf(3)).atGlobalCap).toBe(true);
|
||||
});
|
||||
|
||||
it.each(['', '0', '-5', '2.5', 'abc', '1e3', '99999999999999999999'])(
|
||||
'falls back to 50 for an unusable value %j',
|
||||
async (value) => {
|
||||
const { sessionCapacityState } = await loadWithEnv({ CODEMAN_MAX_SESSIONS: value });
|
||||
expect(sessionCapacityState(sessionsOf(49)).atGlobalCap).toBe(false);
|
||||
expect(sessionCapacityState(sessionsOf(50)).atGlobalCap).toBe(true);
|
||||
}
|
||||
);
|
||||
|
||||
it('derives the multi-user per-user cap (half the global cap) from it', async () => {
|
||||
const { sessionCapacityState, sessionCapacityMessage } = await loadWithEnv({
|
||||
CODEMAN_MAX_SESSIONS: '100',
|
||||
CODEMAN_MULTIUSER: '1',
|
||||
});
|
||||
expect(sessionCapacityState(sessionsOf(49, 'alice'), 'alice').atUserCap).toBe(false);
|
||||
expect(sessionCapacityState(sessionsOf(50, 'alice'), 'alice')).toEqual({ atGlobalCap: false, atUserCap: true });
|
||||
expect(sessionCapacityMessage(sessionsOf(50, 'alice'), 'alice')).toContain('(50)');
|
||||
});
|
||||
|
||||
it('leaves an explicit CODEMAN_MAX_SESSIONS_PER_USER in charge of the per-user cap', async () => {
|
||||
const { sessionCapacityState } = await loadWithEnv({
|
||||
CODEMAN_MAX_SESSIONS: '100',
|
||||
CODEMAN_MAX_SESSIONS_PER_USER: '10',
|
||||
CODEMAN_MULTIUSER: '1',
|
||||
});
|
||||
expect(sessionCapacityState(sessionsOf(9, 'alice'), 'alice').atUserCap).toBe(false);
|
||||
expect(sessionCapacityState(sessionsOf(10, 'alice'), 'alice').atUserCap).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user