refactor(chatCore): extrai resolveCompressionSettings (#3501) (#4826)

Integrated into release/v3.8.36 (#3501 chatCore extraction stack 1/13)
This commit is contained in:
Diego Rodrigues de Sa e Souza
2026-06-23 20:00:34 -03:00
committed by GitHub
parent 8d50c9de16
commit e494afebcf
3 changed files with 84 additions and 14 deletions

View File

@@ -165,6 +165,7 @@ import {
restoreClaudePassthroughToolNames,
mergeResponseToolNameMap,
} from "./chatCore/passthroughToolNames.ts";
import { resolveCompressionSettings } from "./chatCore/compressionSettings.ts";
import { recordContextEditingTelemetryHook } from "./chatCore/contextEditingTelemetry.ts";
import { recordCompressionCacheStats } from "./chatCore/compressionCacheStats.ts";
import { writeCavemanOutputAnalytics } from "./chatCore/cavemanOutputAnalytics.ts";
@@ -875,20 +876,10 @@ export async function handleChatCore({
let contextEditingEnabled = false;
if (body && Array.isArray(allMessages) && allMessages.length > 0) {
let estimatedTokens = estimateTokens(allMessages);
let promptCompressionEnabled = false;
let compressionSettings: CompressionConfig | null = null;
try {
const { getCompressionSettings } = await import("../../src/lib/db/compression.ts");
compressionSettings = await getCompressionSettings();
promptCompressionEnabled = compressionSettings.enabled;
contextEditingEnabled = compressionSettings.contextEditing?.enabled === true;
} catch (err) {
log?.warn?.(
"COMPRESSION",
"Compression settings lookup skipped: " + (err instanceof Error ? err.message : String(err))
);
}
const compressionSettingsResult = await resolveCompressionSettings(log);
const compressionSettings: CompressionConfig | null = compressionSettingsResult.settings;
const promptCompressionEnabled = compressionSettingsResult.enabled;
contextEditingEnabled = compressionSettingsResult.contextEditingEnabled;
// --- Modular Compression Pipeline (Phase 1 Lite + Phase 2 Standard/Caveman + Phase 3 Aggressive) ---
// Runs BEFORE the existing reactive compressContext() to proactively reduce tokens.

View File

@@ -0,0 +1,35 @@
/**
* chatCore compression settings resolution (Quality Gate v2 / Fase 9 — chatCore god-file
* decomposition, #3501).
*
* Extracted from handleChatCore's Proactive Context Compression setup: read the canonical
* compression settings and derive the prompt-compression and delegated context-editing flags.
* Best-effort — on a lookup error it logs and falls back to disabled, exactly like the previous
* inline try/catch. Behaviour is byte-identical.
*/
import type { CompressionConfig } from "../../services/compression/types.ts";
type LoggerLike = { warn?: (...args: unknown[]) => void } | null | undefined;
export async function resolveCompressionSettings(log?: LoggerLike): Promise<{
settings: CompressionConfig | null;
enabled: boolean;
contextEditingEnabled: boolean;
}> {
try {
const { getCompressionSettings } = await import("@/lib/db/compression");
const settings = await getCompressionSettings();
return {
settings,
enabled: settings.enabled,
contextEditingEnabled: settings.contextEditing?.enabled === true,
};
} catch (err) {
log?.warn?.(
"COMPRESSION",
"Compression settings lookup skipped: " + (err instanceof Error ? err.message : String(err))
);
return { settings: null, enabled: false, contextEditingEnabled: false };
}
}

View File

@@ -0,0 +1,44 @@
// Characterization of resolveCompressionSettings — the compression settings read extracted from
// handleChatCore's Proactive Context Compression setup (chatCore god-file decomposition, #3501).
// Uses a real temp DB (getCompressionSettings reads/seeds the settings row). Locks: the derived
// enabled / contextEditingEnabled flags and the disabled fallback shape.
import { test, before, after } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const testDataDir = fs.mkdtempSync(path.join(os.tmpdir(), "omni-comp-settings-test-"));
process.env.DATA_DIR = testDataDir;
const coreDb = await import("../../src/lib/db/core.ts");
const { resolveCompressionSettings } = await import(
"../../open-sse/handlers/chatCore/compressionSettings.ts"
);
before(async () => {
await coreDb.ensureDbInitialized();
});
after(() => {
coreDb.resetDbInstance();
try {
fs.rmSync(testDataDir, { recursive: true, force: true });
} catch {
// best-effort cleanup
}
});
test("returns the settings object from the DB", async () => {
const result = await resolveCompressionSettings();
assert.ok(result.settings, "expected a settings object from the seeded DB");
});
test("derives enabled and contextEditingEnabled from the settings", async () => {
const result = await resolveCompressionSettings();
// `enabled` mirrors settings.enabled exactly
assert.equal(result.enabled, result.settings!.enabled);
// `contextEditingEnabled` is the strict === true derivation of the nested flag
assert.equal(result.contextEditingEnabled, result.settings!.contextEditing?.enabled === true);
assert.equal(typeof result.contextEditingEnabled, "boolean");
});