8000 fix(base): wake idle agy/Gemini panes by simon-inkie · Pull Request #41 · simon-inkie/inkie-deck · GitHub
[go: up one dir, main page]

Skip to content
8000
Merged
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
89 changes: 69 additions & 20 deletions packages/deck/app/lib/tmux.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export function buildCaptureViewportRawArgs(session: string, window: string): st
* line like "(3s ..." reads active and just SKIPS the wake - the safe
* direction; a wrong wake risks stray text in a live composer).
*/
export const ACTIVE_PANE_RE = /esc to interrupt|\((?:\d+h\s)?(?:\d+m\s)?\d+s\s/i;
export const ACTIVE_PANE_RE = /esc to (?:interrupt|cancel)|\((?:\d+h\s)?(?:\d+m\s)?\d+s\s/i;

/** True when a captured viewport shows the working spinner (turn in flight). */
export function paneTextIsActive(viewport: string): boolean {
Expand All @@ -308,30 +308,71 @@ export function paneTextIsActive(viewport: string): boolean {
/**
* The bare, EMPTY composer prompt: a lone prompt glyph on its own line, nothing
* typed after it. This is the POSITIVE idle signal - the one state where a wake
* is safe. Two glyphs are recognised: Claude Code's U+276F and Codex's U+203A
* (the runtimes MC live-wakes). Everything else fails it on purpose:
* - dirty composer ("U+276F git checkout main") -> text after the prompt, no match
* is safe. Three glyphs are recognised: Claude Code's U+276F, Codex's U+203A,
* and agy's ">". Everything else fails it on purpose:
* - dirty composer ("U+276F git checkout main" or "> git checkout main") -> text after the prompt, no match
* - permission dialog ("U+276F 1. Yes") -> text after the prompt, no match
* - ghost / auto-suggest ("U+276F /compact") -> text after the prompt, no match (benign skip)
* - exited agent at a bash shell ("user@host$") -> no prompt glyph at all, no match
* - agy / Gemini runtime (">") -> different glyph, no match (separate wake path)
* Validated against live panes: it matched ONLY the genuinely-idle agents and
* rejected every unsafe state. Multiline (`m`) so any viewport line can be the
* composer (the statusline + separator render BELOW the prompt in this TUI).
* Multiline (`m`) so any viewport line can be the composer (the statusline +
* separator render BELOW the prompt in this TUI).
*
* A bare ">" is matched here but is NOT sufficient on its own: it is a common
* character (Bash PS2 continuation, node REPL, diffs, redirects), so the caller
* additionally requires agy's frame border directly above it (isAgyPromptLine).
* U+276F and U+203A are near-unique and need no such corroboration.
*
* NB: Codex's idle composer is almost never genuinely bare - it shows a rotating
* hint placeholder after the glyph, which fails this bare check by design. That
* case is handled downstream by the ANSI dim-hint disambiguation (DIM_SGR); this
* regex only covers the truly-bare state (Claude's usual rest, Codex's rare one).
*/
export const IDLE_PROMPT_RE = /^\s*[\u276F\u203A]\s*$/m;
export const IDLE_PROMPT_RE = /^\s*[\u276F\u203A>]\s*$/m;

/**
* The prompt glyphs MC recognises as a live agent composer: Claude Code's U+276F,
* Codex's U+203A, and agy / Gemini's ">". Consumers MUST gate the bare ">" on
* agy's frame border (see isAgyPromptLine / lineHasPromptGlyph); a lone ">" is a
* Bash PS2 continuation prompt as readily as an agy composer.
*/
export const COMPOSER_PROMPT_GLYPHS = ["\u276F", "\u203A", ">"] as const;

/**
* agy / Gemini renders its composer inside a bordered frame:
* ───
* >
* ───
* so a genuine agy prompt line has a horizontal border directly ABOVE it. This
* is the corroboration that tells an agy composer apart from a Bash PS2
* continuation prompt or a node REPL, both of which also render a lone ">".
*/
const AGY_FRAME_BORDER_RE = /^\s*[\u2500\u2501\u2550]{3,}\s*$/u;

/**
* The prompt glyphs MC recognises as a live agent composer: Claude Code's U+276F
* and Codex's U+203A. agy / Gemini's ">" is deliberately excluded - it has its
* own separate wake path and never carries the dim-hint placeholder below.
* True when a bare ">" line is corroborated as an agy composer by a frame
* border directly above it. Used for the ">" glyph ONLY; the two TUI glyphs are
* near-unique and do not need it.
*/
export const COMPOSER_PROMPT_GLYPHS = ["\u276F", "\u203A"] as const;
function isAgyPromptLine(lines: string[], i: number): boolean {
const line = lines[i];
if (line === undefined || !/^\s*>\s*$/.test(line)) return false;
const above = lines[i - 1];
return above !== undefined && AGY_FRAME_BORDER_RE.test(above);
}

/**
* True when a line bears a composer prompt glyph, WITH the ">" glyph anchored to
* the start of the line. findPromptLineIndex uses this so a common ">" (a
* statusline arrow "main -> origin", a diff marker, "=>") can never be mistaken
* for a composer and hijack the last-glyph-line scan for Claude or Codex.
* U+276F and U+203A keep `includes`: they are near-unique, and a composer line
* may carry them after other viewport content.
*/
export function lineHasPromptGlyph(line: string): boolean {
return COMPOSER_PROMPT_GLYPHS.some((g) =>
g === ">" ? /^\s*>/.test(line) : line.includes(g)
);
}

/**
* The ANSI "dim" SGR marker (ESC[2m). Codex wraps every rotating hint placeholder
Expand All @@ -347,7 +388,7 @@ export const DIM_SGR = "\x1b[2m";
function findPromptLineIndex(lines: string[]): number {
for (let i = lines.length - 1; i >= 0; i--) {
const line = lines[i];
if (line !== undefined && COMPOSER_PROMPT_GLYPHS.some((g) => line.includes(g))) {
if (line !== undefined && lineHasPromptGlyph(line)) {
return i;
}
}
Expand Down Expand Up @@ -389,11 +430,17 @@ function composerIsClean(viewport: string, rawViewport?: string): boolean {
if (promptIndex === -1) return false;
const promptLine = lines[promptIndex];
if (promptLine === undefined) return false;
// A genuinely-bare prompt reads clean at once. Otherwise the composer has text
// after the glyph: real staged text (dirty) OR a Codex rotating hint. Codex
// renders every hint in the ANSI dim SGR (DIM_SGR); real typed text never is,
// so the raw capture disambiguates for ANY hint string, not a hardcoded one.
if (!IDLE_PROMPT_RE.test(promptLine) && !composerLineIsDimHint(rawViewport)) {
// A bare ">" is a common character, so it counts ONLY when corroborated as a
// genuine agy frame (a horizontal border directly above it). Without that
// border it may be a Bash PS2 continuation prompt or a node REPL, where a
// send-keys would type into unfinished shell input at the shell, so refuse.
if (/^\s*>\s*$/.test(promptLine)) {
if (!isAgyPromptLine(lines, promptIndex)) return false;
} else if (!IDLE_PROMPT_RE.test(promptLine) && !composerLineIsDimHint(rawViewport)) {
// A genuinely-bare TUI prompt reads clean at once. Otherwise the composer has
// text after the glyph: real staged text (dirty) OR a Codex rotating hint.
// Codex renders every hint in the ANSI dim SGR (DIM_SGR); real typed text
// never is, so the raw capture disambiguates for ANY hint string.
return false;
}
// Stale-frame guard: a shell prompt below the composer means the agent exited
Expand Down Expand Up @@ -453,7 +500,9 @@ export function paneTextIsIdle(viewport: string, rawViewport?: string): boolean
* CONFIRMED-idle agent composer: no working spinner AND a clean composer (see
* paneTextIsIdle / composerIsClean). Returns false on ANY uncertainty - invalid
* name, window not live, capture failure, an active spinner, a dirty composer,
* a permission dialog, a bash shell, or an agy (">") prompt. Conservative on
* a permission dialog, a bash shell, or an uncorroborated ">" (a bare ">" with
* no agy frame border above it, e.g. a Bash PS2 continuation or node REPL).
* Conservative on
* purpose: a missed wake just falls back to next-turn hook-inject, but a wrong
* wake could corrupt a live composer or execute text at a shell. "Absence of
* spinner" is NOT treated as idle.
Expand Down
89 changes: 86 additions & 3 deletions packages/deck/test/tmux.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
ACTIVE_PANE_RE,
paneTextIsActive,
IDLE_PROMPT_RE,
lineHasPromptGlyph,
paneTextIsIdle,
SHELL_PROMPT_RE,
isAgentPaneIdle,
Expand Down Expand Up @@ -258,15 +259,23 @@ describe("paneTextIsIdle - positive clean-prompt gate (#91 false-positive fix)",
},
{ label: "lone bare prompt with trailing space", text: "\u276F " },
{ label: "lone bare prompt no trailing space", text: "\u276F" },
{
label: "agy/Gemini runtime (bare > prompt)",
text: "───\n>\n───\n? for shortcuts Gemini 3.5 Flash (Medium) · 1 task(s)",
},
];

// NO WAKE: every unsafe state - dirty composer, permission dialog, bash shell, agy, ghost
// NO WAKE: every unsafe state - dirty composer, permission dialog, bash shell, ghost
const noWake = [
{
label: "dirty composer - st 10BC8 aged text",
text: "─── quill ──\n\u276F ping me when summit recommits\n───\n Opus 4.8 | quill 32%",
},
{ label: "dirty composer 2 - text after prompt", text: "\u276F do the folder reorg" },
{
label: "dirty agy composer - text after > prompt",
text: "───\n> git status\n───\n? for shortcuts Gemini 3.8 Flash",
},
{
label: "permission dialog",
text: "Do you want to proceed?\n\u276F 1. Yes\n 2. No",
Expand All @@ -276,8 +285,15 @@ describe("paneTextIsIdle - positive clean-prompt gate (#91 false-positive fix)",
text: "alex@demobox:~/agents/echo$ \nalex@demobox:~/agents/echo$ ",
},
{
label: "agy/Gemini runtime (different glyph)",
text: "───\n>\n───\n? for shortcuts Gemini 3.5 Flash (Medium) · 1 task(s)",
// Bash PS2 continuation: an unterminated quote renders a bare ">" with
// the shell prompt ABOVE it and no agy frame border. Must NOT be woken.
label: "bash PS2 continuation prompt (bare >)",
text: 'alex@demobox:~/agents/echo$ echo "foo\n>',
},
{
// node REPL left behind by an exited agent: a lone ">" prompt.
label: "node REPL prompt (bare >)",
text: "> \nundefined",
},
{ label: "ghost/auto-suggest text after prompt", text: "\u276F /compact" },
{
Expand Down Expand Up @@ -314,6 +330,43 @@ describe("paneTextIsIdle - positive clean-prompt gate (#91 false-positive fix)",
expect(IDLE_PROMPT_RE.test("\u203A do something real")).toBe(false);
});

it("IDLE_PROMPT_RE matches a bare agy prompt glyph (>)", () => {
expect(IDLE_PROMPT_RE.test("> ")).toBe(true);
expect(IDLE_PROMPT_RE.test(">")).toBe(true);
expect(IDLE_PROMPT_RE.test("> do something real")).toBe(false);
});

// M1: a bare ">" is only an agy composer when the frame border sits above it.
it("WAKE - agy frame: bare > with a border above is idle", () => {
expect(paneTextIsIdle("───────\n>\n───────\n? for shortcuts Gemini 3.8 Flash")).toBe(true);
});

it("NO WAKE - bare > with NO border above (PS2 continuation) is refused", () => {
expect(paneTextIsIdle('alex@demobox:~/agents/echo$ echo "foo\n>')).toBe(false);
});

it("NO WAKE - bare > with output (node REPL) is refused", () => {
expect(paneTextIsIdle("> \nundefined")).toBe(false);
});

// M2: a ">" in a statusline/footer must NOT hijack the last-glyph-line scan
// for a Claude composer. lineHasPromptGlyph anchors ">" to the line start.
it("WAKE - Claude composer with a > in the statusline below (must not hijack)", () => {
expect(
paneTextIsIdle(
"\u276F \n──────\n Opus 4.8 | main -> origin 32%"
)
).toBe(true);
});

it("NO WAKE - Claude composer with a > in a footer, composer still dirty", () => {
expect(
paneTextIsIdle(
"\u276F git checkout main\n main -> origin 32%"
)
).toBe(false);
});

// CRITICAL false-negative regression: idle agent with ctx-percent statusline
// must NOT be mistaken for a shell prompt (SHELL_PROMPT_RE requires user@host)
it("WAKE - idle with ctx:19% statusline below prompt (must not false-negative)", () => {
Expand Down Expand Up @@ -364,6 +417,36 @@ describe("paneTextIsIdle - positive clean-prompt gate (#91 false-positive fix)",
});

// SHELL_PROMPT_RE direct assertions
// Sonnet follow-up coverage: a ">" that appears in a Codex statusline must
// not hijack the dim-hint path, a dirty agy composer must not wake, and a
// narrow pane with a wrapped composer must stay conservative.
it("NO WAKE - dirty agy composer with text after the prompt glyph", () => {
expect(paneTextIsIdle("───────\n> refactor the parser\n───────\nGemini 3.8 Flash")).toBe(false);
});

it("WAKE - a > in a Codex statusline below the hint must not hijack detection", () => {
const plain = "transcript\n\u203A Implement {feature}\nmain -> origin 42%";
const raw = "transcript\n\x1b[2m\u203A Implement {feature}\x1b[0m\nmain -> origin 42%";
expect(paneTextIsIdle(plain, raw)).toBe(true);
});

it("NO WAKE - narrow pane: wrapped continuation row containing a glyph is not clean", () => {
// A narrow pane wraps the draft; the continuation row carries a stray glyph.
// The composer is not bare, so it must not read as a safe wake.
expect(
paneTextIsIdle("\u276F a long draft\n that wrapped \u276F here")
).toBe(false);
});

it("lineHasPromptGlyph anchors > to the line start, keeps includes for TUI glyphs", () => {
expect(lineHasPromptGlyph("> ")).toBe(true);
expect(lineHasPromptGlyph(" > ")).toBe(true);
expect(lineHasPromptGlyph("main -> origin")).toBe(false);
expect(lineHasPromptGlyph("a => b")).toBe(false);
expect(lineHasPromptGlyph("\u276F ")).toBe(true);
expect(lineHasPromptGlyph("prefix \u203A")).toBe(true);
});

it("SHELL_PROMPT_RE matches a user@host bash prompt", () => {
expect(SHELL_PROMPT_RE.test("alex@demobox:~/agents/echo$")).toBe(true);
});
Expand Down
Loading
0