Merge pull request #666: feat(sessions): keep an idle session's prompt cache warm with a guarded keep-alive turn

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-10-22 19:31:08 +02:00
commit f80f107445
45 files changed
+3017 -59

No files matched your search

+68 -7
View File
@@ -67,12 +67,15 @@ import { missingCliMessage, resolveCliBinDir } from './utils/cli-resolver.js';
import {
buildSpawnCommandFromRegistry,
configSetenvValues,
launchGatePasses,
legacyConfigForMode,
} from './session-cli-registry-bridge.js';
import { keepWarmOn } from './prompt-cache-keepwarm.js';
import type { CliEntry } from './config/cli-registry/types.js';
import {
resolveStatusLineCliCommand,
readPlanUsageTelemetryEnabled,
readKeepWarmSettings,
findEffectiveUserStatusLineCommand,
} from './hooks-config.js';
import {
@@ -1095,6 +1098,28 @@ export function buildCodexCommand(config?: CodexConfig): string {
return buildSpawnCommandFromRegistry(entry, { mode: 'codex', sessionId: '', codexConfig: config }) ?? 'codex';
}
/**
* Whether a launch carries `idleCompaction: false` in its one `--settings` JSON: keep-warm on
* at launch (`keepWarmOn`), the statusLine exporter injected (without its `prompt_cache` reports
* keep-warm cannot run, and the opt-out alone would only cost the user Claude Code's own idle
* compaction), and the CLI's `idleCompactionSetting` gate passed, fail-closed like `--name`:
* an unknown version means no key. Pure; `claudeCliVersion` as in buildSpawnCommand.
*/
export function decideIdleCompactionOptOut(o: {
mode: SessionMode;
statusLine: StatusLineExporter | undefined;
keepWarm: boolean | undefined;
keepWarmDefault: boolean;
spawnedByAgent: boolean | undefined;
claudeCliVersion?: string | null;
}): boolean {
return (
o.statusLine === 'injected' &&
keepWarmOn(o.keepWarm, o.keepWarmDefault, !!o.spawnedByAgent) &&
launchGatePasses(o.mode, 'idleCompactionSetting', o.claudeCliVersion)
);
}
export function buildSpawnCommand(options: {
mode: SessionMode;
sessionId: string;
@@ -1116,6 +1141,8 @@ export function buildSpawnCommand(options: {
advisorModel?: string;
/** Resolved by resolveStatusLineCliCommand (hooks-config.ts) — undefined skips the exporter. Claude only. */
statusLineCommand?: string;
/** Add `idleCompaction: false` to the launch's one `--settings` JSON (see decideIdleCompactionOptOut). */
idleCompactionOff?: boolean;
/** Name pinned on claude as `--name` (version-gated, sanitized; local spawns only). By default only a user-chosen name; `pinCliSessionName` pins every tab name: see `Session.cliPinnedName`. */
sessionName?: string;
/**
@@ -2291,6 +2318,28 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
return { command, state: command ? 'injected' : enabled ? 'workspace-statusline' : 'collection-off' };
}
/**
* Whether this launch carries `idleCompaction: false` (decideIdleCompactionOptOut), with the
* keep-warm app default read fresh like the plan-usage switch above. Logged when keep-warm is
* on but the version gate keeps the key off, so a session that may still idle-compact says why.
*/
private async _resolveIdleCompactionOptOut(
mode: SessionMode,
statusLine: StatusLineExporter | undefined,
keepWarm: boolean | undefined,
spawnedByAgent: boolean | undefined
): Promise<boolean> {
if (statusLine !== 'injected') return false; // no settings read for a launch that can never carry it
const { keepWarmDefault } = await readKeepWarmSettings();
const off = decideIdleCompactionOptOut({ mode, statusLine, keepWarm, keepWarmDefault, spawnedByAgent });
if (!off && keepWarmOn(keepWarm, keepWarmDefault, !!spawnedByAgent)) {
console.log(
`[TmuxManager] Keep-warm is on but Claude Code's version is unknown or predates the idleCompaction setting; launching without the opt-out`
);
}
return off;
}
/**
* Export the user's own REAL statusLine command (found by
* findEffectiveUserStatusLineCommand) via tmux setenv, so the shared
@@ -2351,6 +2400,8 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
envOverrides,
effort,
advisorModel,
keepWarm,
spawnedByAgent,
historyLimit = DEFAULT_TMUX_HISTORY_LIMIT,
remote,
docker,
@@ -2414,6 +2465,7 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
workingDir,
!!(remote || docker)
);
const idleCompactionOff = await this._resolveIdleCompactionOptOut(mode, statusLine, keepWarm, spawnedByAgent);
// The user's own REAL statusLine, if any (walked via Claude Code's own
// settings precedence) — exported below so the shared exporter script
// can wrap it. Only worth discovering when we're actually injecting.
@@ -2438,6 +2490,7 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
effort,
advisorModel,
statusLineCommand,
idleCompactionOff,
sessionName: cliName,
});
@@ -2583,6 +2636,7 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
attached: false,
name,
statusLine,
idleCompactionOff,
};
this.sessions.set(sessionId, session);
@@ -2693,6 +2747,8 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
unsetEnvKeys,
effort,
advisorModel,
keepWarm,
spawnedByAgent,
remote,
docker,
cliName,
@@ -2713,6 +2769,7 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
workingDir,
!!(remote || docker)
);
const idleCompactionOff = await this._resolveIdleCompactionOptOut(mode, statusLine, keepWarm, spawnedByAgent);
const userStatusLineCommand = statusLineCommand ? await findEffectiveUserStatusLineCommand(workingDir) : undefined;
const baseCmd = buildSpawnCommand({
@@ -2734,6 +2791,7 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
effort,
advisorModel,
statusLineCommand,
idleCompactionOff,
sessionName: cliName,
});
const config = niceConfig || DEFAULT_NICE_CONFIG;
@@ -2776,8 +2834,9 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
this.clearPaneExit(muxName);
const pid = this.getPanePid(muxName);
if (pid) session.pid = pid;
if (session.statusLine !== statusLine) {
if (session.statusLine !== statusLine || session.idleCompactionOff !== idleCompactionOff) {
session.statusLine = statusLine;
session.idleCompactionOff = idleCompactionOff;
this.saveSessions();
}
return pid;
@@ -4163,17 +4222,19 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
/**
* Plain visible-frame text for the working/idle probe (see `session.ts`).
*
* One `capture-pane` and nothing else: no `-e` styles, no `display-message`
* cursor query, no repaint reconstruction: this feeds a regex, not a
* terminal. Returns null in tests (no tmux) so callers fall back to their
* stream heuristics rather than reading an empty screen as "not working".
* One `capture-pane` and nothing else: no `display-message` cursor query, no
* repaint reconstruction: this feeds a regex, not a terminal. `styles` adds
* `-e` for a reader that must tell faint text from typed text (the keep-warm
* composer check); lines stay `\n`-separated either way. Returns null in tests
* (no tmux) so callers fall back to their stream heuristics rather than
* reading an empty screen as "not working".
*/
capturePaneText(muxName: string, paneTarget?: string): string | null {
capturePaneText(muxName: string, paneTarget?: string, opts?: { styles?: boolean }): string | null {
if (IS_TEST_MODE) return null;
const target = resolveTmuxPaneTarget(muxName, paneTarget);
if (!target) return null;
try {
return execSync(`${this.tmux()} capture-pane -p -t ${shellescape(target)}`, {
return execSync(`${this.tmux()} capture-pane -p${opts?.styles ? ' -e' : ''} -t ${shellescape(target)}`, {
encoding: 'utf-8',
timeout: EXEC_TIMEOUT_MS,
});