2026-08-09 15:31:02 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* @fileoverview Pure working/idle heuristics for a Claude interactive pane.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Split out of `session.ts` so the thresholds and the state math are unit
|
|
|
|
|
|
* testable without a PTY (same reasoning as `session-order.ts` /
|
|
|
|
|
|
* `usage-limit-patterns.ts`).
|
|
|
|
|
|
*
|
|
|
|
|
|
* **Why activity and not the status line.** Claude Code's working indicator is
|
|
|
|
|
|
* `✻ Actualizing… (13m 23s · ↓ 47.5k tokens)`, where the glyph animates through
|
|
|
|
|
|
* `· ✢ ✳ ∗ ✻ ✽` and the gerund is randomized per turn. Neither the braille
|
|
|
|
|
|
* spinner (`SPINNER_PATTERN`) nor the old keyword list (`Thinking|Writing|
|
|
|
|
|
|
* Reading|Running`) matches any of that, so the pane looked idle for a whole
|
|
|
|
|
|
* turn. Matching the new line does not rescue the stream either: tmux ships
|
|
|
|
|
|
* PARTIAL repaints, so measured on a live worker the complete line reached the
|
|
|
|
|
|
* PTY roughly once every 20 seconds, while the composer's `❯` (which is what
|
|
|
|
|
|
* ARMS idle detection) arrived every single second.
|
|
|
|
|
|
*
|
|
|
|
|
|
* What is left is the one thing measured to separate the two states cleanly: a
|
|
|
|
|
|
* working pane repaints, an idle pane emits nothing at all. Sampled once per
|
|
|
|
|
|
* second for 12s across six live sessions, the two working ones produced output
|
|
|
|
|
|
* in 12/12 windows and the four idle ones in 0/12.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2026-09-22 08:10:05 +02:00
|
|
|
|
import { stripAnsi } from './utils/regex-patterns.js';
|
|
|
|
|
|
|
2026-08-09 15:31:02 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* A gap longer than this ends a run of continuous output. Claude repaints at
|
|
|
|
|
|
* least once a second while working, so this leaves generous headroom.
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const ACTIVITY_GAP_MS = 2000;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Continuous output for this long means the pane is working. Long enough that a
|
|
|
|
|
|
* one-off repaint (an update-check line, a rotating tip) cannot reach it.
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const WORKING_STREAK_MS = 2000;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Silence for this long is what confirms the pane really went idle. Must stay
|
|
|
|
|
|
* above ACTIVITY_GAP_MS, or a pause between two repaints of one turn would
|
|
|
|
|
|
* read as the end of the turn.
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const IDLE_SILENCE_MS = 2500;
|
|
|
|
|
|
|
|
|
|
|
|
/** How often a pending idle confirmation re-checks a pane that is still noisy. */
|
|
|
|
|
|
export const IDLE_RECHECK_MS = 500;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Floor between two pane probes for one session. The probe shells out to tmux,
|
|
|
|
|
|
* so this is what keeps a screenful of busy sessions from turning idle detection
|
|
|
|
|
|
* into a subprocess storm.
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const PANE_PROBE_MIN_INTERVAL_MS = 1500;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* How long to wait before looking again at a pane the probe just called working.
|
|
|
|
|
|
* Claude can sit silent for tens of seconds inside one tool call, so this is the
|
|
|
|
|
|
* cadence that carries a long quiet turn, so it is deliberately slow.
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const PANE_PROBE_RECHECK_MS = 5000;
|
|
|
|
|
|
|
|
|
|
|
|
/** An unbroken run of PTY output. */
|
|
|
|
|
|
export interface ActivityStreak {
|
|
|
|
|
|
/** When this run began. */
|
|
|
|
|
|
startedAt: number;
|
|
|
|
|
|
/** The most recent chunk in it. */
|
|
|
|
|
|
lastAt: number;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Fold one output chunk into the current streak, starting a new one when the
|
|
|
|
|
|
* pane has been quiet longer than `gapMs`.
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function trackActivityStreak(
|
|
|
|
|
|
streak: ActivityStreak | null,
|
|
|
|
|
|
now: number,
|
|
|
|
|
|
gapMs: number = ACTIVITY_GAP_MS
|
|
|
|
|
|
): ActivityStreak {
|
|
|
|
|
|
if (!streak || now - streak.lastAt > gapMs) return { startedAt: now, lastAt: now };
|
|
|
|
|
|
return { startedAt: streak.startedAt, lastAt: now };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* True once a streak has been running long enough to mean work rather than a
|
|
|
|
|
|
* single repaint. Measured on the streak's own span (`lastAt - startedAt`), not
|
|
|
|
|
|
* against the caller's clock, so a stale streak cannot age into a true.
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function isSustainedActivity(streak: ActivityStreak | null, streakMs: number = WORKING_STREAK_MS): boolean {
|
|
|
|
|
|
return !!streak && streak.lastAt - streak.startedAt >= streakMs;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** True when the pane has produced nothing for long enough to call it idle. */
|
|
|
|
|
|
export function isPaneQuiet(lastActivityAt: number, now: number, silenceMs: number = IDLE_SILENCE_MS): boolean {
|
|
|
|
|
|
return now - lastActivityAt >= silenceMs;
|
|
|
|
|
|
}
|
2026-09-21 18:00:30 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-22 08:58:52 +02:00
|
|
|
|
* How many rows at the foot of a pane capture may hold the background-work row, for a
|
|
|
|
|
|
* CLI that declares no number of its own (`capabilities.workDetect.watchingLines`).
|
2026-09-21 18:00:30 +02:00
|
|
|
|
*
|
2026-09-22 08:58:52 +02:00
|
|
|
|
* Claude Code draws its chip on the LAST row of the screen. The row above it is the
|
|
|
|
|
|
* status line, which a user's own `statusLine` command writes, and two rows is what
|
2026-09-22 08:10:05 +02:00
|
|
|
|
* covers the chip wherever a trailing blank or a one-line notice pushes it up by one.
|
2026-09-22 08:58:52 +02:00
|
|
|
|
* Codex pins its row above the composer instead and declares four.
|
2026-09-22 08:10:05 +02:00
|
|
|
|
*
|
|
|
|
|
|
* ⚠️ The ceiling is the security boundary, not a tidiness measure. The label is
|
2026-09-22 08:58:52 +02:00
|
|
|
|
* PANE-DERIVED, and everything on that screen above the CLI's own chrome is text the
|
|
|
|
|
|
* agent wrote itself, so an agent that printed `· 1 monitor ·` into its output would
|
|
|
|
|
|
* silence its own idle alert. Keep each CLI's window as small as its layout allows,
|
|
|
|
|
|
* keep its pattern anchored on chrome only that CLI can draw, and never widen either to
|
|
|
|
|
|
* a whole-pane search.
|
2026-09-21 18:00:30 +02:00
|
|
|
|
*/
|
2026-09-22 08:10:05 +02:00
|
|
|
|
export const WATCHING_TAIL_LINES = 2;
|
|
|
|
|
|
|
|
|
|
|
|
/** Longest label a badge will carry. A footer chip is a handful of words. */
|
|
|
|
|
|
export const MAX_WATCHING_LABEL_CHARS = 40;
|
2026-09-21 18:00:30 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* What a pane says is still running in the background, e.g. `1 monitor` or `2 shells`.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The CLI writes that chip while a monitor, a backgrounded shell or a cloud session it
|
|
|
|
|
|
* started is still going, which is exactly the case where the agent has ended its turn
|
|
|
|
|
|
* without wanting anything from the user. `pattern` comes from the CLI's own registry
|
|
|
|
|
|
* entry (`capabilities.workDetect.watchingLine`); group 1 is the label when the pattern
|
|
|
|
|
|
* declares one, and the whole match stands in when it does not.
|
|
|
|
|
|
*
|
2026-09-22 08:58:52 +02:00
|
|
|
|
* Each candidate row is tested on its own, bottom row first, so a pattern can anchor
|
|
|
|
|
|
* itself with `^` or `$` against a single row rather than against a joined block. Blank
|
|
|
|
|
|
* rows are dropped before the window is taken, because a CLI that leaves a blank line
|
|
|
|
|
|
* between its chrome rows would otherwise spend the window on nothing. The answer is
|
|
|
|
|
|
* stripped of ANSI and capped, because it ends up on a badge and in an approval card.
|
2026-09-22 08:10:05 +02:00
|
|
|
|
*
|
2026-09-22 08:58:52 +02:00
|
|
|
|
* @param tailLines how many non-blank rows from the bottom to look at, defaulting to
|
|
|
|
|
|
* `WATCHING_TAIL_LINES`; a CLI declares its own when its row is not the last one
|
2026-09-21 18:00:30 +02:00
|
|
|
|
* @returns the label, or null when the pane shows no background work
|
|
|
|
|
|
*/
|
2026-09-22 08:58:52 +02:00
|
|
|
|
export function watchingLabel(
|
|
|
|
|
|
paneText: string | null | undefined,
|
|
|
|
|
|
pattern: RegExp,
|
|
|
|
|
|
tailLines: number = WATCHING_TAIL_LINES
|
|
|
|
|
|
): string | null {
|
2026-09-21 18:00:30 +02:00
|
|
|
|
if (!paneText) return null;
|
2026-09-22 08:10:05 +02:00
|
|
|
|
const lines = stripAnsi(paneText)
|
2026-09-21 18:00:30 +02:00
|
|
|
|
.split('\n')
|
|
|
|
|
|
.map((line) => line.trimEnd())
|
|
|
|
|
|
.filter((line) => line !== '');
|
2026-09-22 08:58:52 +02:00
|
|
|
|
for (const line of lines.slice(-Math.max(1, tailLines)).reverse()) {
|
2026-09-22 08:10:05 +02:00
|
|
|
|
// A pattern compiled by compileVersionRegex() never carries the `g` flag, but a
|
|
|
|
|
|
// caller reaching in from a test or a config reload might, and a stale lastIndex
|
|
|
|
|
|
// would make the same screen match every other call.
|
|
|
|
|
|
pattern.lastIndex = 0;
|
|
|
|
|
|
const match = pattern.exec(line);
|
|
|
|
|
|
if (!match) continue;
|
|
|
|
|
|
const label = (match[1] ?? match[0]).trim().slice(0, MAX_WATCHING_LABEL_CHARS);
|
|
|
|
|
|
if (label) return label;
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
2026-09-21 18:00:30 +02:00
|
|
|
|
}
|