Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions source/utils/auto-compact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
setAutoCompactStrategy,
setAutoCompactThreshold,
} from './auto-compact';
import {COMPRESSION_CONSTANTS} from './message-compression';

// Reset session overrides before each test
test.beforeEach(() => {
Expand Down Expand Up @@ -51,24 +52,36 @@ test('setAutoCompactThreshold sets threshold value', t => {
t.is(autoCompactSessionOverrides.threshold, 75);
});

test('setAutoCompactThreshold clamps to minimum of 50', t => {
setAutoCompactThreshold(30);
t.is(autoCompactSessionOverrides.threshold, 50);
test('setAutoCompactThreshold clamps to the configured minimum', t => {
setAutoCompactThreshold(COMPRESSION_CONSTANTS.MIN_THRESHOLD_PERCENT - 20);
t.is(
autoCompactSessionOverrides.threshold,
COMPRESSION_CONSTANTS.MIN_THRESHOLD_PERCENT,
);
});

test('setAutoCompactThreshold clamps to maximum of 95', t => {
setAutoCompactThreshold(99);
t.is(autoCompactSessionOverrides.threshold, 95);
test('setAutoCompactThreshold clamps to the configured maximum', t => {
setAutoCompactThreshold(COMPRESSION_CONSTANTS.MAX_THRESHOLD_PERCENT + 4);
t.is(
autoCompactSessionOverrides.threshold,
COMPRESSION_CONSTANTS.MAX_THRESHOLD_PERCENT,
);
});

test('setAutoCompactThreshold handles boundary value 50', t => {
setAutoCompactThreshold(50);
t.is(autoCompactSessionOverrides.threshold, 50);
test('setAutoCompactThreshold handles the configured minimum', t => {
setAutoCompactThreshold(COMPRESSION_CONSTANTS.MIN_THRESHOLD_PERCENT);
t.is(
autoCompactSessionOverrides.threshold,
COMPRESSION_CONSTANTS.MIN_THRESHOLD_PERCENT,
);
});

test('setAutoCompactThreshold handles boundary value 95', t => {
setAutoCompactThreshold(95);
t.is(autoCompactSessionOverrides.threshold, 95);
test('setAutoCompactThreshold handles the configured maximum', t => {
setAutoCompactThreshold(COMPRESSION_CONSTANTS.MAX_THRESHOLD_PERCENT);
t.is(
autoCompactSessionOverrides.threshold,
COMPRESSION_CONSTANTS.MAX_THRESHOLD_PERCENT,
);
});

test('setAutoCompactThreshold sets threshold to null', t => {
Expand Down
11 changes: 8 additions & 3 deletions source/utils/auto-compact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {calculateToolDefinitionsTokensFromDefs} from '@/usage/calculator';
import {getLogger} from '@/utils/logging';
import {compressionBackup} from './compression-backup';
import {summariseWithLLM} from './llm-summariser';
import {compressMessages} from './message-compression';
import {COMPRESSION_CONSTANTS, compressMessages} from './message-compression';
import {filterModelFacing} from './message-visibility';
import {createSessionOverride} from './session-override';

Expand All @@ -19,11 +19,16 @@ export interface AutoCompactSessionOverrides {
strategy: CompressionStrategy | null;
}

// Session overrides for auto-compact. `threshold` is clamped to 50–95.
// Session overrides for auto-compact. `threshold` is clamped to the configured range.
const autoCompactSession = {
enabled: createSessionOverride<boolean>(),
threshold: createSessionOverride<number>(value =>
value !== null ? Math.max(50, Math.min(95, value)) : null,
value !== null
? Math.max(
COMPRESSION_CONSTANTS.MIN_THRESHOLD_PERCENT,
Math.min(COMPRESSION_CONSTANTS.MAX_THRESHOLD_PERCENT, value),
)
: null,
),
mode: createSessionOverride<CompressionMode>(),
strategy: createSessionOverride<CompressionStrategy>(),
Expand Down
Loading