Skip to content
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
dcc9913
test(runtime): add a readiness census pinning every tui-idle verdict
Jinwoo-H Oct 1, 2026
d1003b1
test(runtime): pin the census quiet probes to literal windows
Jinwoo-H Oct 1, 2026
fa82161
test(runtime): say which census probe writes runtime state
Jinwoo-H Oct 1, 2026
26ceafd
test(runtime): observe the census through settled panes and caller-vi…
Jinwoo-H Oct 1, 2026
5b15a03
test(runtime): cross dialog-versus-ready-screen order with every titl…
Jinwoo-H Oct 1, 2026
9925794
Merge remote-tracking branch 'origin/main' into jinwoo/readiness-census
Jinwoo-H Oct 1, 2026
50038b0
test(runtime): read the census baseline field without Reflect.get
Jinwoo-H Oct 1, 2026
faa97a2
refactor(runtime): read Antigravity, Cline, Prime Agent and Cursor re…
Jinwoo-H Oct 1, 2026
eaa0706
test(runtime): cover the agent state rule engine's schema, priority, …
Jinwoo-H Oct 1, 2026
47f7fa2
fix(runtime): refuse rule patterns that repeat an optional or alterna…
Jinwoo-H Oct 1, 2026
7211af8
refactor(runtime): give agent state rules and text anchors one when/a…
Jinwoo-H Oct 1, 2026
a567b26
docs: point the readiness evidence docs at the agent state rule files
Jinwoo-H Oct 1, 2026
068ccee
refactor(runtime): read Codex, Claude, OpenCode, Pi, OMP and Gemini r…
Jinwoo-H Oct 1, 2026
ac81936
test(runtime): cover the rule engine's title, text and predicate regi…
Jinwoo-H Oct 1, 2026
a9a9512
fix(runtime): reject a rule file that repeats an anchor or rule id
Jinwoo-H Oct 1, 2026
147ee9b
refactor(runtime): fold the working anchor answer into live
Jinwoo-H Oct 1, 2026
bcf28a3
refactor(runtime): read the shared π title anchor from pi.json alone
Jinwoo-H Oct 1, 2026
16373df
refactor(runtime): key every rule file and read the trusted screen fr…
Jinwoo-H Oct 1, 2026
7c90a13
test(runtime): pin that a clocked Codex pane takes no other agent's r…
Jinwoo-H Oct 1, 2026
2813182
Merge remote-tracking branch 'origin/main' into jinwoo/agent-state-ru…
Jinwoo-H Oct 1, 2026
0dff1fb
Merge remote-tracking branch 'origin/jinwoo/agent-state-rules-engine'…
Jinwoo-H Oct 1, 2026
7d7d7ab
fix(runtime): refuse uppercase contains terms in text anchors, which …
Jinwoo-H Oct 1, 2026
dc74b46
refactor(runtime): state Codex's provisional startup and title anchor…
Jinwoo-H Oct 1, 2026
724c74f
Merge remote-tracking branch 'origin/jinwoo/agent-state-rules-engine'…
Jinwoo-H Oct 1, 2026
3050ce3
Merge remote-tracking branch 'origin/main' into jinwoo/agent-state-ru…
Jinwoo-H Oct 2, 2026
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
9 changes: 6 additions & 3 deletions docs/reference/antigravity-readiness-evidence.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Antigravity readiness: what the transcripts show

`findAntigravityReadyPromptIndex` in `src/main/runtime/terminal-wait-detection.ts` decides whether
an Antigravity pane is ready for a prompt. It has been written five times, each version tuned
Antigravity readiness lives in `src/main/runtime/agent-state-rules/antigravity.json`: a screen rule
over the trusted grid, and a text anchor that runs the named scan
`findAntigravityComposerIndex` (`agent-state-rules/antigravity-text-composer.ts`) over the
line-folded tail when no trusted grid exists. That text scan decides whether a pane is ready for a
prompt from its tail alone. It has been written five times, each version tuned
against a five-line screen typed from memory into a `.spec.ts` fixture. Three of the first four
were found worse than the bug they replaced, and the fifth was reverted.

Expand Down Expand Up @@ -41,7 +44,7 @@ replays them.
What they show:

- **The text tail misses three ready screens.** On `ready-accept-edits`, `ready-plan` and
`turn-ended` the line-folded tail never satisfies `findAntigravityReadyPromptIndex`; the screen
`turn-ended` the line-folded tail never satisfies `findAntigravityComposerIndex`; the screen
rule does. (`turn-ended` is the 1.2.14 form of the old `busy-turn-ended` known defect.)
- **A caret rule is wrong on the screen.** The grid keeps the bare `>` through a turn and behind
the picker. The text tail happened to lose it mid-turn (section 8); the screen does not.
Expand Down
5 changes: 3 additions & 2 deletions docs/reference/cline-and-prime-agent-readiness-evidence.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Cline and Prime Agent readiness: what the transcripts show

Both agents paint their composer with cursor addressing on the alternate screen, so the line-folded
text tail cannot see it (#23268, #22153). Their readiness is read off the live screen by
`isClineComposerReadyScreen` and `isPrimeAgentComposerReadyScreen`, through the same tiering as
text tail cannot see it (#23268, #22153). Their readiness is read off the live screen by the
`composer_ready` rules in `src/main/runtime/agent-state-rules/cline.json` and `prime-agent.json`,
through the same tiering as
Antigravity ([`antigravity-readiness-evidence.md`](./antigravity-readiness-evidence.md)): a pane
with an output clock is believed only once quiet, and when a trustworthy screen exists it decides,
so the quiet-process lane cannot settle a dialog it cannot see. Without a readable screen (a
Expand Down
63 changes: 63 additions & 0 deletions src/main/runtime/agent-state-rules/agent-state-rule-matchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { ScreenCondition, TextTest } from './agent-state-rules-schema'

export type TextMatcher = (text: string) => boolean

type TextTerm = Extract<TextTest, { regex: string } | { contains: string }>

function compileTerm(term: TextTerm): TextMatcher {
if ('contains' in term) {
return (text) => text.includes(term.contains)
}
const pattern = new RegExp(term.regex, term.ignoreCase ? 'i' : '')
return (text) => pattern.test(text)
}

export function compileTextTest(test: TextTest): TextMatcher {
if ('regex' in test || 'contains' in test) {
return compileTerm(test)
}
const all = (test.all ?? []).map(compileTerm)
const any = (test.any ?? []).map(compileTerm)
const none = (test.none ?? []).map(compileTerm)
return (text) =>
all.every((matches) => matches(text)) &&
(any.length === 0 || any.some((matches) => matches(text))) &&
!none.some((matches) => matches(text))
}

export type ScreenMatcher = (screenLines: readonly string[]) => boolean

export function compileScreenCondition(screen: ScreenCondition): ScreenMatcher {
if (!screen.rows) {
return () => true
}
const rows = screen.rows.map((row) =>
'optional' in row
? { matches: compileTextTest(row.optional), optional: true }
: { matches: compileTextTest(row), optional: false }
)
const endsWithinBottom = screen.endsWithinBottom ?? 1
const noneAbove = screen.noneAbove ? compileTextTest(screen.noneAbove) : null
const lastRow = rows.at(-1)
const rowsUpward = rows.slice(0, -1).toReversed()
return (screenLines) => {
const lines = screenLines.map((line) => line.trim())
let end = lines.length - 1
const lowestEnd = Math.max(0, lines.length - endsWithinBottom)
while (end >= lowestEnd && !lastRow?.matches(lines[end])) {
end -= 1
}
if (end < lowestEnd) {
return false
}
let cursor = end - 1
for (const row of rowsUpward) {
if (row.matches(lines[cursor] ?? '')) {
cursor -= 1
} else if (!row.optional) {
return false
}
}
return noneAbove === null || !lines.slice(0, Math.max(0, cursor + 1)).some(noneAbove)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Why a subset: rule patterns run on every poll, and Node has no linear-time regex engine, so a
// pattern that can backtrack exponentially is refused when the file loads. Overlapping adjacent
// quantifiers (`\s*\s*x`) are polynomial and not detected.
export function findUnsafePatternReason(pattern: string): string | null {
try {
new RegExp(pattern)
} catch {
return 'does not compile'
}
if (/\\[1-9]|\\k</.test(pattern)) {
return 'uses a backreference'
}
if (/\(\?<[=!]/.test(pattern)) {
return 'uses a lookbehind'
}
return repeatsAVariableGroup(pattern)
? 'repeats a group that can match in more than one way'
: null
}

function isRepeatingQuantifier(char: string | undefined): boolean {
return char === '*' || char === '+' || char === '{'
}

// A repeated group whose body varies, by a quantifier or an alternation: `(a+)*`, `(a?)+`,
// `(a|aa)+`. Such a body can split one input many ways, each of which a failed match retries.
function repeatsAVariableGroup(pattern: string): boolean {
const groupVaries: boolean[] = []
let inClass = false
for (let index = 0; index < pattern.length; index += 1) {
const char = pattern[index]
if (char === '\\') {
index += 1
} else if (inClass) {
inClass = char !== ']'
} else if (char === '[') {
inClass = true
} else if (char === '(') {
groupVaries.push(false)
// Why skip: the `?` of `(?:`, `(?=` or `(?<name>` opens a group; it quantifies nothing.
if (pattern[index + 1] === '?') {
index += 1
}
} else if (char === ')') {
const bodyVaries = groupVaries.pop() ?? false
if (bodyVaries && isRepeatingQuantifier(pattern[index + 1])) {
return true
}
if (bodyVaries && groupVaries.length > 0) {
groupVaries[groupVaries.length - 1] = true
}
} else if (
(isRepeatingQuantifier(char) || char === '?' || char === '|') &&
groupVaries.length > 0
) {
groupVaries[groupVaries.length - 1] = true
}
}
return false
}
215 changes: 215 additions & 0 deletions src/main/runtime/agent-state-rules/agent-state-rule-regions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
import { describe, expect, it } from 'vitest'
import {
detectExplicitIdleStatusFromTitle,
detectTerminalWaitBlockedReason,
isKnownReadyPromptBody,
isKnownReadyPromptPreview,
isKnownReadyPromptSettled
} from '../terminal-wait-detection'
import { nameOnlyIdleNeedsCorroboration } from '../tui-idle-evidence'
import {
compileAgentRules,
evaluateCompiledRules,
readsTrustedScreen,
type AgentStateRegions
} from './agent-state-rules-engine'
import { parseAgentStateRuleFiles } from './agent-state-rules-catalog'
import { showsIdleTitleAnchor } from './agent-state-title-anchors'

const STRONG_QUIET = { state: 'idle', strength: 'strong', requiresQuiet: true }
const TITLE = { region: 'title', status: 'idle' }

function rule(id: string, when: Record<string, unknown>, answer: Record<string, unknown>) {
return { id, why: 'test', priority: 100, when, answer }
}

const READY_ANCHOR = {
id: 'ready',
why: 'test',
when: { region: 'text', find: { lastOf: 'ready>' } },
answer: { state: 'idle' }
}

function file(rules: unknown[], extra: Record<string, unknown> = {}): Record<string, unknown> {
return { id: 'codex', engineVersion: 1, anchors: [READY_ANCHOR], rules, ...extra }
}

function evaluate(rules: unknown[], regions: AgentStateRegions): string | null {
const [parsed] = parseAgentStateRuleFiles([file(rules, { profile: { screenSource: 'live' } })])
return evaluateCompiledRules(compileAgentRules(parsed), regions)?.ruleId ?? null
}

describe('region schema', () => {
const screen = { region: 'screen', predicate: 'codex-composer-ready' }

it.each([
[
'screen rows beside a predicate',
file([rule('a', { ...screen, rows: [{ contains: '>' }] }, STRONG_QUIET)], {
profile: { screenSource: 'live' }
})
],
['screen rules with no screenSource', file([rule('a', screen, STRONG_QUIET)])],
[
'withoutClock on a weak rule',
file([
rule(
'a',
{ region: 'title', status: 'idle' },
{ state: 'idle', strength: 'weak', requiresQuiet: true, withoutClock: 'skip' }
)
])
],
[
'a text rule naming no anchor of its file',
file([rule('a', { region: 'text', anchor: 'missing' }, STRONG_QUIET)])
],
[
'a text rule naming a non-idle anchor',
file([rule('a', { region: 'text', anchor: 'live' }, STRONG_QUIET)], {
anchors: [{ ...READY_ANCHOR, id: 'live', answer: { state: 'live' } }]
})
],
[
'a title anchor answering other than idle',
file([], {
anchors: [
{
id: 't',
why: 'test',
when: { region: 'title', match: { contains: '◇' } },
answer: { state: 'live' }
}
]
})
],
[
'a title rule on a status other than idle',
file([rule('a', { region: 'title', status: 'working' }, STRONG_QUIET)])
],
['an unknown pane id', { ...file([]), id: 'unknown' }],
['two anchors with one id', file([], { anchors: [READY_ANCHOR, READY_ANCHOR] })],
[
'two rules with one id',
file([rule('a', TITLE, STRONG_QUIET), rule('a', TITLE, STRONG_QUIET)])
]
])('rejects %s', (_label, candidate) => {
expect(() => parseAgentStateRuleFiles([candidate])).toThrow()
})

it('accepts the unknown-pane file', () => {
expect(() => parseAgentStateRuleFiles([{ ...file([]), id: 'unknown-pane' }])).not.toThrow()
})
})

describe('regions', () => {
const title = rule('title', { region: 'title', status: 'idle' }, STRONG_QUIET)
const text = rule('text', { region: 'text', anchor: 'ready' }, STRONG_QUIET)

it('skips a rule whose region the lane does not read', () => {
expect(evaluate([title], { readScreenLines: () => [] })).toBeNull()
expect(evaluate([title], { readTitleStatus: () => 'idle' })).toBe('title')
expect(evaluate([title], { readTitleStatus: () => 'working' })).toBeNull()
})

it('reads a named screen predicate over the lowercased screen', () => {
const composer = rule(
'c',
{ region: 'screen', predicate: 'codex-composer-ready' },
STRONG_QUIET
)
expect(evaluate([composer], { readScreenLines: () => ['› Ask Codex to do anything'] })).toBe(
'c'
)
expect(
evaluate([composer], {
readScreenLines: () => ['esc to interrupt)', '› Ask Codex to do anything']
})
).toBeNull()
})

it('needs its text anchor settled: no blocker painted after it', () => {
const readText = (value: string) => ({ readText: () => value })
expect(evaluate([text], readText('ready>'))).toBe('text')
expect(evaluate([text], readText('ready>\ndo you trust this folder?'))).toBeNull()
expect(evaluate([text], readText('do you trust this folder?\nready>'))).toBe('text')
})

it('skips a rule marked withoutClock skip only on a pane with no output clock', () => {
const skipped = rule(
's',
{ region: 'title', status: 'idle' },
{ ...STRONG_QUIET, withoutClock: 'skip' }
)
const regions = { readTitleStatus: () => 'idle' as const }
expect(evaluate([skipped, title], { ...regions, hasOutputClock: true })).toBe('s')
expect(evaluate([skipped, title], { ...regions, hasOutputClock: false })).toBe('title')
})
})

describe('the bundled Codex text anchors', () => {
const header =
'╭───╮\n│ >_ openai codex (v0.157.0) │\n│ model: gpt-5 │\n│ directory: ~/repo │\n╰───╯'

it('settles on the loaded header, and takes the header as proof a dialog was answered', () => {
expect(isKnownReadyPromptSettled(header)).toBe(true)
expect(
detectTerminalWaitBlockedReason(
'do you trust this folder?\n1. yes\n>_ openai codex (v0.158.0)'
)
).toBeNull()
})

it('holds a provisional header: present, but not yet taking input', () => {
const provisional = header.replace('gpt-5', 'loading')
expect(isKnownReadyPromptPreview(provisional)).toBe(true)
expect(isKnownReadyPromptSettled(provisional)).toBe(false)
expect(isKnownReadyPromptSettled(`${provisional}\n› hi\ngpt-5 · ~/repo`)).toBe(true)
})

it('holds its own ready text to quiet on a clocked Codex pane, and believes it clockless', () => {
expect(isKnownReadyPromptBody(header, 'codex', () => null, true)).toBe(false)
expect(isKnownReadyPromptBody(header, 'codex', () => null, false)).toBe(true)
expect(isKnownReadyPromptBody(header, 'claude', () => null, true)).toBe(true)
})

it("takes no other agent's ready text on a clocked Codex pane, as before the rule files", () => {
const cursorPrompt = '>_ openai codex (v0.158.0)\ncursor agent\n→'
expect(isKnownReadyPromptBody(cursorPrompt, 'codex', () => null, true)).toBe(false)
expect(isKnownReadyPromptBody(cursorPrompt, 'codex', () => null, false)).toBe(true)
expect(isKnownReadyPromptBody(cursorPrompt, 'claude', () => null, true)).toBe(true)
})

it('settles an unknown pane on the live-screen Codex header at once, even clocked', () => {
const screen = () => header.split('\n')
expect(isKnownReadyPromptBody('', null, screen, true)).toBe(true)
expect(isKnownReadyPromptBody('', 'claude', screen, true)).toBe(false)
})

it('reads the live screen for Codex, not the trusted grid', () => {
expect(readsTrustedScreen('codex')).toBe(false)
expect(readsTrustedScreen('cline')).toBe(true)
})
})

describe('the bundled title anchors', () => {
it.each(['✳ Claude Code', '* Claude Code', '◇ Ready (repo)', 'π - orca', 'OC | orca'])(
'reads %s as an explicit idle title',
(title) => {
expect(detectExplicitIdleStatusFromTitle(title)).toBe('idle')
}
)

it('marks an agent rest title only when an anchor matches it', () => {
expect(showsIdleTitleAnchor('✳ Claude Code')).toBe(true)
expect(showsIdleTitleAnchor('Claude Code')).toBe(false)
})

it('leaves a name-only title to the agent idle-title rule', () => {
expect(detectExplicitIdleStatusFromTitle('claude')).toBeNull()
expect(nameOnlyIdleNeedsCorroboration('pi')).toBe(true)
expect(nameOnlyIdleNeedsCorroboration('omp')).toBe(true)
expect(nameOnlyIdleNeedsCorroboration('gemini')).toBe(false)
expect(nameOnlyIdleNeedsCorroboration('opencode')).toBe(false)
})
})
Loading
Loading