import { t as createSubsystemLogger } from "./subsystem-CT-8ly4J.js";
import { y as normalizeOptionalAgentRuntimeId } from "./openai-routing-WLjNjydC.js";
import { f as loadSessionEntryReadOnly } from "./session-accessor.sqlite-entry-CoLie3L_.js";
import { n as withPluginRuntimeGenerationScope } from "./generation-scope-BpIwE4y6.js";
import { o as resolveFreshSessionTotalTokens } from "./types-ukra4D97.js";
import "./session-accessor-Dwfzn6Ej.js";
import { l as resolveContextEngine } from "./registry-oBJBHZ1n.js";
import { t as SessionManager } from "./session-manager-DFVGmI34.js";
import { i as resolveLiveToolResultMaxChars } from "./tool-result-limits-Bw5uKdjF.js";
import { i as resolveCliBackendConfig } from "./cli-backends-CK4gEOp8.js";
import { t as acquireAgentRunPreparedModelRuntime } from "./prepared-model-runtime-Buvc7POq.js";
import { x as resolveCompactionTimeoutMs, y as compactContextEngineWithSafetyTimeout } from "./diagnostic-DSnchdd7.js";
import { mt as isBenignCompactionSkipResult, pt as isBenignCompactionSkipReason, v as createPreparedEmbeddedAgentSettingsManager } from "./builtin-openclaw-BX61aB1y.js";
import { i as buildGenericCliContextEngineHostSupport } from "./host-compat-xESS3bi6.js";
import { c as runContextEngineMaintenance, u as buildContextEngineRuntimeSettings } from "./context-engine-lifecycle-KOH0Joem.js";
import "./tool-result-truncation-BcE3MiG5.js";
import { M as shouldPreemptivelyCompactBeforePrompt } from "./helpers-CM-ts_zn.js";
import { f as buildEmbeddedCompactionRuntimeContext } from "./attempt-prompt-helpers-BlTUKLwp.js";
import { t as ensureContextEnginesInitialized } from "./init-D5uXd8iS.js";
import { a as resolveEffectiveCompactionMode, n as applyAgentAutoCompactionGuard } from "./agent-settings-BEoZ5tkM.js";
import { i as isRecoverableNativeHarnessBindingFailure, r as maybeCompactAgentHarnessSession, t as acceptCompactionSuccessor } from "./compaction-successor-B_p0vG4p.js";
import { t as ensureSelectedAgentHarnessPlugin } from "./runtime-plugin-BPs8dFIL.js";
import { a as recordCliCompactionInStore, r as normalizeSessionTokenCount, t as clearCliSessionInStore } from "./session-store-CD8XdcDX.js";
//#region src/agents/command/cli-compaction.ts
/**
* CLI turn compaction lifecycle.
*
* This module decides when CLI-backed sessions need context compaction, chooses
* native harness or context-engine compaction, and records resulting session state.
*/
const CODEX_APP_SERVER_OWNS_AUTO_COMPACTION_REASON = "codex app-server owns automatic compaction";
const log = createSubsystemLogger("agents/cli-compaction");
const cliCompactionDeps = {
	openSessionManager: (target) => SessionManager.open(target),
	ensureContextEnginesInitialized,
	resolveContextEngine,
	createPreparedEmbeddedAgentSettingsManager,
	applyAgentAutoCompactionGuard,
	shouldPreemptivelyCompactBeforePrompt,
	resolveLiveToolResultMaxChars,
	runContextEngineMaintenance,
	acquirePreparedModelRuntime: acquireAgentRunPreparedModelRuntime,
	ensureSelectedAgentHarnessPlugin,
	maybeCompactAgentHarnessSession,
	clearCliSessionInStore,
	resolveCliBackendConfig,
	recordCliCompactionInStore
};
/** Overrides CLI compaction dependencies for focused tests. */
function setCliCompactionTestDeps(overrides) {
	Object.assign(cliCompactionDeps, overrides);
}
/** Restores production CLI compaction dependencies after tests. */
function resetCliCompactionTestDeps() {
	Object.assign(cliCompactionDeps, {
		openSessionManager: (target) => SessionManager.open(target),
		ensureContextEnginesInitialized,
		resolveContextEngine,
		createPreparedEmbeddedAgentSettingsManager,
		applyAgentAutoCompactionGuard,
		shouldPreemptivelyCompactBeforePrompt,
		resolveLiveToolResultMaxChars,
		runContextEngineMaintenance,
		acquirePreparedModelRuntime: acquireAgentRunPreparedModelRuntime,
		ensureSelectedAgentHarnessPlugin,
		maybeCompactAgentHarnessSession,
		clearCliSessionInStore,
		resolveCliBackendConfig,
		recordCliCompactionInStore
	});
}
function resolveSessionTokenSnapshot(sessionEntry) {
	return normalizeSessionTokenCount(resolveFreshSessionTotalTokens(sessionEntry));
}
function isNativeHarnessCompactionSession(sessionEntry, provider) {
	const harnessId = sessionEntry?.agentHarnessId?.trim().toLowerCase();
	if (!harnessId || normalizeOptionalAgentRuntimeId(harnessId) === "openclaw") return false;
	const providerId = provider.trim().toLowerCase();
	return harnessId === providerId || harnessId === "copilot" && providerId === "github-copilot" || harnessId === "codex" && (providerId === "codex" || providerId === "openai");
}
function isUnsupportedNativeHarnessCompaction(result) {
	return result?.ok === false && result.failure?.reason === "unsupported_harness_compaction";
}
function isIntentionalNativeAutoCompactionSkip(result) {
	return result?.ok === true && !result.compacted && result.reason === CODEX_APP_SERVER_OWNS_AUTO_COMPACTION_REASON;
}
function readAgentIdFromSessionKey(sessionKey) {
	const parts = sessionKey.trim().split(":");
	return parts[0] === "agent" && parts[1]?.trim() ? parts[1].trim() : void 0;
}
function buildCliCompactionRuntimeContext(params) {
	return {
		...buildEmbeddedCompactionRuntimeContext({
			sessionKey: params.sessionKey,
			messageChannel: params.messageChannel,
			messageProvider: params.messageChannel,
			agentAccountId: params.agentAccountId,
			authProfileId: params.authProfileId,
			workspaceDir: params.workspaceDir,
			cwd: params.cwd,
			agentDir: params.agentDir,
			config: params.cfg,
			skillsSnapshot: params.skillsSnapshot,
			senderIsOwner: params.senderIsOwner,
			provider: params.provider,
			modelId: params.model,
			harnessRuntime: params.harnessRuntime,
			modelSelectionLocked: params.modelSelectionLocked,
			thinkLevel: params.thinkLevel,
			extraSystemPrompt: params.extraSystemPrompt
		}),
		currentTokenCount: params.currentTokenCount,
		tokenBudget: params.contextTokenBudget,
		trigger: params.trigger
	};
}
async function compactCliTranscript(params) {
	const runtimeContext = buildCliCompactionRuntimeContext({
		sessionKey: params.sessionKey,
		messageChannel: params.messageChannel,
		agentAccountId: params.agentAccountId,
		authProfileId: params.authProfileId,
		workspaceDir: params.workspaceDir,
		cwd: params.cwd,
		agentDir: params.agentDir,
		cfg: params.cfg,
		skillsSnapshot: params.skillsSnapshot,
		senderIsOwner: params.senderIsOwner,
		provider: params.provider,
		model: params.model,
		harnessRuntime: params.harnessRuntime,
		modelSelectionLocked: params.modelSelectionLocked,
		thinkLevel: params.thinkLevel,
		extraSystemPrompt: params.extraSystemPrompt,
		currentTokenCount: params.currentTokenCount,
		contextTokenBudget: params.contextTokenBudget,
		trigger: "cli_budget"
	});
	const runtimeSettings = buildContextEngineRuntimeSettings({
		contextEngineHost: buildGenericCliContextEngineHostSupport({
			backendId: params.provider,
			capabilities: ["compact", "maintain"]
		}),
		provider: params.provider,
		requestedModel: params.model,
		resolvedModel: params.model,
		selectedContextEngineId: params.contextEngine.info.id,
		contextEngineSelectionSource: "configured",
		promptTokenBudget: params.contextTokenBudget
	});
	let compactResult;
	params.assertActive();
	try {
		compactResult = await compactContextEngineWithSafetyTimeout(params.contextEngine, {
			sessionId: params.sessionId,
			sessionKey: params.sessionKey,
			sessionTarget: {
				sessionId: params.sessionId,
				sessionKey: params.sessionKey,
				...params.storePath ? { storePath: params.storePath } : {}
			},
			tokenBudget: params.contextTokenBudget,
			currentTokenCount: params.currentTokenCount,
			force: true,
			compactionTarget: "budget",
			runtimeContext,
			runtimeSettings
		}, resolveCompactionTimeoutMs(params.cfg), params.abortSignal);
	} catch (error) {
		const reason = error instanceof Error ? error.message : String(error);
		if (isBenignCompactionSkipReason(reason)) {
			log.info(`CLI transcript compaction skipped for ${params.provider}/${params.model}: ${reason}`);
			return { compacted: false };
		}
		log.warn(`CLI transcript compaction failed for ${params.provider}/${params.model}: ${reason}`);
		return {
			compacted: false,
			failureReason: reason
		};
	}
	if (!compactResult.ok || !compactResult.compacted) {
		const reason = compactResult.reason;
		if (isBenignCompactionSkipResult(compactResult)) {
			log.info(`CLI transcript compaction skipped for ${params.provider}/${params.model}: ${reason}`);
			return { compacted: false };
		}
		log.warn(`CLI transcript compaction did not reduce context for ${params.provider}/${params.model}: ${reason ?? "compaction did not reduce context"}`);
		return {
			compacted: false,
			failureReason: compactResult.reason ?? "compaction did not reduce context"
		};
	}
	const result = compactResult.result;
	const successor = await acceptCompactionSuccessor({
		expectedEntry: params.expectedEntry,
		assertActive: params.assertActive,
		onCommitted: params.onCommitted,
		config: params.cfg,
		currentSessionFile: params.sessionFile,
		currentTarget: {
			agentId: params.agentId,
			sessionId: params.sessionId,
			sessionKey: params.sessionKey,
			storePath: params.storePath
		},
		result: compactResult
	});
	const outcome = {
		compacted: true,
		accepted: successor,
		...result?.tokensAfter !== void 0 ? { tokensAfter: result.tokensAfter } : {}
	};
	try {
		params.assertActive();
		await cliCompactionDeps.runContextEngineMaintenance({
			contextEngine: params.contextEngine,
			sessionId: successor.sessionId,
			sessionKey: params.sessionKey,
			sessionFile: successor.sessionFile,
			sessionTarget: successor.sessionTarget,
			reason: "compaction",
			...successor.previousSessionId ? {} : { sessionManager: params.sessionManager },
			runtimeContext,
			runtimeSettings,
			config: params.cfg,
			assertActive: params.assertActive
		});
	} catch (error) {
		try {
			params.assertActive();
		} catch {
			return outcome;
		}
		if (!params.bestEffortMaintenance) throw error;
		log.warn(`CLI transcript compaction maintenance failed after fallback for ${params.provider}/${params.model}: ${error instanceof Error ? error.message : String(error)}`);
	}
	return outcome;
}
async function compactNativeHarnessCliTranscript(params) {
	let result;
	try {
		const sessionAgentId = readAgentIdFromSessionKey(params.sessionKey);
		const nativeHarnessId = params.sessionEntry.agentHarnessId?.trim();
		const modelSelectionLocked = params.sessionEntry.modelSelectionLocked === true;
		const authProfileId = params.sessionEntry.authProfileOverride?.trim() || void 0;
		const preparedRuntimeLease = await cliCompactionDeps.acquirePreparedModelRuntime({
			config: params.cfg,
			...sessionAgentId ? { agentId: sessionAgentId } : {},
			agentDir: params.agentDir,
			workspaceDir: params.workspaceDir,
			allowGatewaySubagentBinding: true,
			runtimePluginSelections: [{
				provider: params.provider,
				modelId: params.model,
				...sessionAgentId ? { agentId: sessionAgentId } : {},
				...nativeHarnessId ? { runtime: nativeHarnessId } : {}
			}]
		}, params.pluginGeneration ? { pluginGeneration: params.pluginGeneration } : {});
		try {
			const preparedModelRuntime = preparedRuntimeLease.snapshot;
			result = await withPluginRuntimeGenerationScope(preparedModelRuntime, async () => {
				await cliCompactionDeps.ensureSelectedAgentHarnessPlugin({
					provider: params.provider,
					modelId: params.model,
					config: params.cfg,
					sessionKey: params.sessionKey,
					workspaceDir: params.workspaceDir,
					...sessionAgentId ? { agentId: sessionAgentId } : {},
					...nativeHarnessId ? { agentHarnessRuntimeOverride: nativeHarnessId } : {},
					pluginRegistry: preparedModelRuntime.pluginRegistry
				});
				params.assertActive();
				return await cliCompactionDeps.maybeCompactAgentHarnessSession({
					sessionId: params.sessionId,
					sessionKey: params.sessionKey,
					sessionFile: params.sessionFile,
					workspaceDir: params.workspaceDir,
					cwd: params.cwd,
					agentDir: params.agentDir,
					config: params.cfg,
					skillsSnapshot: params.skillsSnapshot,
					provider: params.provider,
					model: params.model,
					authProfileId,
					contextTokenBudget: params.contextTokenBudget,
					currentTokenCount: params.currentTokenCount,
					trigger: "budget",
					force: true,
					messageChannel: params.messageChannel,
					agentAccountId: params.agentAccountId,
					senderIsOwner: params.senderIsOwner,
					thinkLevel: params.thinkLevel,
					extraSystemPrompt: params.extraSystemPrompt,
					modelSelectionLocked,
					allowGatewaySubagentBinding: true,
					...params.contextEngine ? {
						contextEngine: params.contextEngine,
						contextEngineRuntimeContext: buildCliCompactionRuntimeContext({
							sessionKey: params.sessionKey,
							messageChannel: params.messageChannel,
							agentAccountId: params.agentAccountId,
							authProfileId,
							workspaceDir: params.workspaceDir,
							cwd: params.cwd,
							agentDir: params.agentDir,
							cfg: params.cfg,
							skillsSnapshot: params.skillsSnapshot,
							senderIsOwner: params.senderIsOwner,
							provider: params.provider,
							model: params.model,
							harnessRuntime: nativeHarnessId,
							modelSelectionLocked,
							thinkLevel: params.thinkLevel,
							extraSystemPrompt: params.extraSystemPrompt,
							currentTokenCount: params.currentTokenCount,
							contextTokenBudget: params.contextTokenBudget,
							trigger: "cli_native_budget"
						})
					} : {},
					...nativeHarnessId ? { agentHarnessId: nativeHarnessId } : {},
					abortSignal: params.abortSignal
				}, { preparedModelRuntime });
			});
		} finally {
			preparedRuntimeLease.release();
		}
	} catch (error) {
		log.warn(`CLI native harness compaction failed for ${params.provider}/${params.model}: ${error instanceof Error ? error.message : String(error)}`);
		return {
			compacted: false,
			failureReason: error instanceof Error ? error.message : String(error)
		};
	}
	if (!result?.ok || !result.compacted) {
		const reason = result?.reason;
		if (result && isBenignCompactionSkipResult(result)) {
			log.info(`CLI native harness compaction skipped for ${params.provider}/${params.model}: ${reason}`);
			return { compacted: false };
		}
		if (isIntentionalNativeAutoCompactionSkip(result)) {
			log.info(`CLI native harness compaction skipped for ${params.provider}/${params.model}: ${CODEX_APP_SERVER_OWNS_AUTO_COMPACTION_REASON}`);
			return { compacted: false };
		}
		const recoverableBindingFailure = isRecoverableNativeHarnessBindingFailure(result);
		const fallbackToContextEngine = params.sessionEntry.modelSelectionLocked !== true && (isUnsupportedNativeHarnessCompaction(result) || recoverableBindingFailure);
		log.warn(`CLI native harness compaction did not reduce context for ${params.provider}/${params.model}: ${reason}`);
		return {
			compacted: false,
			fallbackToContextEngine,
			clearCliSessionBinding: params.sessionEntry.modelSelectionLocked !== true && recoverableBindingFailure,
			failureReason: result?.reason ?? "native harness compaction did not reduce context"
		};
	}
	return {
		compacted: true,
		result
	};
}
/** Runs pre-turn compaction for a CLI session and returns the updated session entry. */
async function runCliTurnCompactionLifecycle(params, host = {}) {
	const contextTokenBudget = normalizeSessionTokenCount(params.sessionEntry?.contextTokens);
	if (!params.storePath || !contextTokenBudget) return params.sessionEntry;
	const capturedEntry = loadSessionEntryReadOnly({
		agentId: params.sessionAgentId,
		sessionKey: params.sessionKey,
		storePath: params.storePath,
		readConsistency: "latest"
	});
	const expectedEntry = {
		sessionId: params.sessionId,
		lifecycleRevision: capturedEntry?.lifecycleRevision,
		activeWriterRunId: capturedEntry?.activeWriterRunId
	};
	const assertActive = () => {
		params.abortSignal?.throwIfAborted();
		host.assertActive?.();
	};
	assertActive();
	const onCommitted = (accepted) => {
		if (params.sessionStore) params.sessionStore[params.sessionKey] = accepted.entry;
		host.onCommitted?.(accepted);
	};
	const sessionManager = cliCompactionDeps.openSessionManager({
		agentId: params.sessionAgentId,
		sessionId: params.sessionId,
		sessionKey: params.sessionKey,
		storePath: params.storePath
	});
	const sessionFile = params.sessionKey;
	const settingsManager = await cliCompactionDeps.createPreparedEmbeddedAgentSettingsManager({
		cwd: params.cwd ?? params.workspaceDir,
		agentDir: params.agentDir,
		cfg: params.cfg,
		contextTokenBudget
	});
	const preemptiveCompaction = cliCompactionDeps.shouldPreemptivelyCompactBeforePrompt({
		messages: sessionManager.buildSessionContext().messages,
		prompt: "",
		contextTokenBudget,
		reserveTokens: settingsManager.getCompactionReserveTokens(),
		toolResultMaxChars: cliCompactionDeps.resolveLiveToolResultMaxChars({ contextWindowTokens: contextTokenBudget })
	});
	const tokenSnapshot = resolveSessionTokenSnapshot(params.sessionEntry);
	const currentTokenCount = Math.max(preemptiveCompaction.estimatedPromptTokens, tokenSnapshot ?? 0);
	if (!preemptiveCompaction.shouldCompact && currentTokenCount <= preemptiveCompaction.promptBudgetBeforeReserve) return params.sessionEntry;
	const resolvedBackend = cliCompactionDeps.resolveCliBackendConfig(params.provider, params.cfg);
	const lockedHarnessRuntime = normalizeOptionalAgentRuntimeId(params.sessionEntry?.agentHarnessId);
	if (params.sessionEntry?.modelSelectionLocked === true && lockedHarnessRuntime !== "openclaw" && !isNativeHarnessCompactionSession(params.sessionEntry, params.provider)) throw new Error("CLI compaction cannot replace a model-locked native harness runtime");
	if (resolvedBackend?.ownsNativeCompaction && !isNativeHarnessCompactionSession(params.sessionEntry, params.provider)) {
		log.info(`CLI backend "${params.provider}" owns native compaction — deferring to backend`);
		return params.sessionEntry;
	}
	let compactionKind;
	let contextCompactionOutcome;
	let nativeCompactionResult;
	let useContextEngineCompaction = true;
	let nativeFallbackToContextEngine = false;
	let nativeFallbackNeedsBindingClear = false;
	let resolvedContextEngine;
	let autoCompactionGuardApplied = false;
	const authProfileId = params.sessionEntry?.authProfileOverride?.trim() || void 0;
	const applyAutoCompactionGuard = async (contextEngine) => {
		if (autoCompactionGuardApplied) return;
		autoCompactionGuardApplied = true;
		await cliCompactionDeps.applyAgentAutoCompactionGuard({
			settingsManager,
			contextEngineInfo: contextEngine.info,
			compactionMode: resolveEffectiveCompactionMode(params.cfg)
		});
	};
	if (isNativeHarnessCompactionSession(params.sessionEntry, params.provider)) {
		cliCompactionDeps.ensureContextEnginesInitialized();
		resolvedContextEngine = await cliCompactionDeps.resolveContextEngine(params.cfg);
		await applyAutoCompactionGuard(resolvedContextEngine);
		const nativeOutcome = await compactNativeHarnessCliTranscript({
			cfg: params.cfg,
			sessionId: params.sessionId,
			sessionKey: params.sessionKey,
			sessionFile,
			sessionEntry: params.sessionEntry,
			workspaceDir: params.workspaceDir,
			cwd: params.cwd,
			agentDir: params.agentDir,
			provider: params.provider,
			model: params.model,
			contextTokenBudget,
			currentTokenCount,
			contextEngine: resolvedContextEngine,
			skillsSnapshot: params.skillsSnapshot,
			messageChannel: params.messageChannel,
			agentAccountId: params.agentAccountId,
			senderIsOwner: params.senderIsOwner,
			thinkLevel: params.thinkLevel,
			extraSystemPrompt: params.extraSystemPrompt,
			pluginGeneration: params.pluginGeneration,
			abortSignal: params.abortSignal,
			assertActive
		});
		if (nativeOutcome.compacted) {
			compactionKind = "native-harness";
			nativeCompactionResult = nativeOutcome.result;
			useContextEngineCompaction = false;
		} else if (nativeOutcome.fallbackToContextEngine) {
			nativeFallbackToContextEngine = true;
			nativeFallbackNeedsBindingClear = nativeOutcome.clearCliSessionBinding === true;
		} else if (nativeOutcome.failureReason) throw new Error(`CLI native harness compaction failed for ${params.provider}/${params.model}: ${nativeOutcome.failureReason}`);
		else useContextEngineCompaction = false;
	}
	if (useContextEngineCompaction) {
		assertActive();
		if (!resolvedContextEngine) {
			cliCompactionDeps.ensureContextEnginesInitialized();
			resolvedContextEngine = await cliCompactionDeps.resolveContextEngine(params.cfg);
		}
		const contextEngine = resolvedContextEngine;
		await applyAutoCompactionGuard(contextEngine);
		const contextOutcome = await compactCliTranscript({
			agentId: params.sessionAgentId,
			contextEngine,
			sessionId: params.sessionId,
			sessionKey: params.sessionKey,
			sessionFile,
			sessionManager,
			storePath: params.storePath,
			cfg: params.cfg,
			workspaceDir: params.workspaceDir,
			cwd: params.cwd,
			agentDir: params.agentDir,
			provider: params.provider,
			model: params.model,
			harnessRuntime: params.sessionEntry?.agentHarnessId,
			modelSelectionLocked: params.sessionEntry?.modelSelectionLocked,
			contextTokenBudget,
			currentTokenCount,
			skillsSnapshot: params.skillsSnapshot,
			messageChannel: params.messageChannel,
			agentAccountId: params.agentAccountId,
			authProfileId,
			senderIsOwner: params.senderIsOwner,
			thinkLevel: params.thinkLevel,
			extraSystemPrompt: params.extraSystemPrompt,
			bestEffortMaintenance: nativeFallbackToContextEngine,
			expectedEntry,
			assertActive,
			abortSignal: params.abortSignal,
			onCommitted
		});
		contextCompactionOutcome = contextOutcome;
		compactionKind = contextOutcome.compacted ? "context-engine" : void 0;
		if (!compactionKind && contextOutcome.failureReason) throw new Error(`CLI transcript compaction failed for ${params.provider}/${params.model}: ${contextOutcome.failureReason}`);
	}
	if (nativeFallbackNeedsBindingClear && !compactionKind && params.sessionStore) {
		assertActive();
		return await cliCompactionDeps.clearCliSessionInStore({
			provider: params.provider,
			sessionKey: params.sessionKey,
			sessionStore: params.sessionStore,
			storePath: params.storePath,
			expectedSessionId: params.sessionId
		}) ?? params.sessionEntry;
	}
	if (!compactionKind || !params.sessionStore) return params.sessionEntry;
	const recorded = await cliCompactionDeps.recordCliCompactionInStore({
		compactionKind,
		sessionKey: params.sessionKey,
		sessionStore: params.sessionStore,
		storePath: params.storePath,
		tokensAfter: nativeCompactionResult?.result?.tokensAfter ?? contextCompactionOutcome?.tokensAfter,
		expectedSession: contextCompactionOutcome?.accepted?.entry ?? expectedEntry
	});
	if (!recorded) throw new Error("Session changed before CLI compaction could be recorded");
	return recorded;
}
//#endregion
export { resetCliCompactionTestDeps, runCliTurnCompactionLifecycle, setCliCompactionTestDeps };
